-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathsoundcloud.py
More file actions
96 lines (75 loc) · 2.87 KB
/
Copy pathsoundcloud.py
File metadata and controls
96 lines (75 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
SoundCloud module for downloading and searching songs.
"""
import logging
import re
from itertools import islice
from typing import Any, Dict, List
from soundcloud import SoundCloud as SoundCloudClient
from soundcloud.resource.track import Track
from spotdl.providers.audio.base import AudioProvider
from spotdl.types.result import Result
__all__ = ["SoundCloud"]
logger = logging.getLogger(__name__)
class SoundCloud(AudioProvider):
"""
SoundCloud audio provider class
"""
SUPPORTS_ISRC = False
GET_RESULTS_OPTS: List[Dict[str, Any]] = [{}]
def __init__(self, *args: Any, **kwargs: Any) -> None:
"""
Initialize the SoundCloud API
### Arguments
- args: Arguments passed to the `AudioProvider` class.
- kwargs: Keyword arguments passed to the `AudioProvider` class.
"""
super().__init__(*args, **kwargs)
self.client = SoundCloudClient()
def get_results(self, search_term: str, *_args, **_kwargs) -> List[Result]:
"""
Get results from SoundCloud
### Arguments
- search_term: The search term to search for.
- args: Unused.
- kwargs: Unused.
### Returns
- A list of SoundCloud results if found, None otherwise.
"""
results = list(islice(self.client.search(search_term), 20))
regex = r"^(.+?)-|(\(\w+[\s\S]*\))"
# Because anyone can post on soundcloud, we do another search with an edited search
# The regex removes anything in brackets and the artist(s)'s name(s) if in the name
edited_search_term = re.sub(regex, "", search_term)
results.extend(list(islice(self.client.search(edited_search_term), 20)))
# Simplify results
simplified_results = []
for result in results:
if not isinstance(result, Track):
continue
# Ignore results that are not playable
if "/preview/" in result.media.transcodings[0].url:
continue
album = self.client.get_track_albums(result.id)
try:
album_name = next(album).title
except StopIteration:
album_name = None
simplified_results.append(
Result(
source="soundcloud",
url=result.permalink_url,
name=result.title,
verified=result.user.verified,
duration=result.full_duration,
author=result.user.username,
artists=(result.user.username,),
result_id=str(result.id),
isrc_search=False,
search_query=search_term,
views=result.playback_count,
explicit=False,
album=album_name,
)
)
return simplified_results