-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
128 lines (112 loc) · 5.13 KB
/
main.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from re import findall
from time import time
from curl_cffi import requests
from urllib.parse import urlencode
class Downloadable:
def __init__(self, stream_url: str, session: requests.Session):
self.stream_url = stream_url
self.session = session
def get_download_link(self):
response = self.session.get(
url = self.stream_url,
headers = {
"host": "api-v2.soundcloud.com",
"connection": "keep-alive",
"sec-ch-ua-platform": "\"Windows\"",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"sec-ch-ua": "\"Not(A:Brand\";v=\"99\", \"Google Chrome\";v=\"133\", \"Chromium\";v=\"133\"",
"sec-ch-ua-mobile": "?0",
"accept": "*/*",
"origin": "https://soundcloud.com",
"sec-fetch-site": "same-site",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://soundcloud.com/",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "en-US,en;q=0.9"
},
)
return response.json()["url"]
class Track:
def __init__(self, track_data: dict, session: requests.Session):
self.id = track_data.get("id")
self.title = track_data.get("title")
self.track_authorization = track_data.get("track_authorization")
self.media = track_data.get("media", {}).get("transcodings", [])
self.session = session
def get_stream_link(self):
preferred_protocols = [
"hls",
"progressive"
]
for protocol in preferred_protocols:
for transcoding in self.media:
if transcoding["format"].get("protocol") == protocol:
params = {
"client_id": "YHtBnq6bxM7DhJkIfzrGq3gYrueyLDMM",
"track_authorization": self.track_authorization
}
url = transcoding["url"]
return Downloadable(f"{url}?{urlencode(params)}", self.session)
return None
class Soundcloud:
def __init__(self):
self.session = requests.Session(
impersonate = "chrome120"
)
self.user_id = self.get_user_id()
def get_user_id(self):
response = self.session.get(
url = "https://soundcloud.com/",
headers = {
"host": "soundcloud.com",
"connection": "keep-alive",
"upgrade-insecure-requests": "1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"sec-fetch-site": "none",
"sec-fetch-mode": "navigate",
"sec-fetch-user": "?1",
"sec-fetch-dest": "document",
"sec-ch-ua": "\"Not(A:Brand\";v=\"99\", \"Google Chrome\";v=\"133\", \"Chromium\";v=\"133\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "en-US,en;q=0.9"
}
)
return findall(r'"data":"(.*?)"', response.text)[0]
def search(self, terms: str):
response = requests.get(
url = "https://api-v2.soundcloud.com/search",
headers = {
"host": "api-v2.soundcloud.com",
"connection": "keep-alive",
"sec-ch-ua-platform": "\"Windows\"",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
"accept": "application/json, text/javascript, */*; q=0.01",
"sec-ch-ua": "\"Not(A:Brand\";v=\"99\", \"Google Chrome\";v=\"133\", \"Chromium\";v=\"133\"",
"sec-ch-ua-mobile": "?0",
"origin": "https://soundcloud.com",
"sec-fetch-site": "same-site",
"sec-fetch-mode": "cors",
"sec-fetch-dest": "empty",
"referer": "https://soundcloud.com/",
"accept-encoding": "gzip, deflate, br, zstd",
"accept-language": "en-US,en;q=0.9"
}, params={
"q": terms,
"variant_ids": "",
"facet": "model",
"user_id": self.user_id,
"client_id": "YHtBnq6bxM7DhJkIfzrGq3gYrueyLDMM",
"limit": "20",
"offset": "0",
"linked_partitioning": "1",
"app_version": int(time()),
"app_locale": "en"
}
)
return [Track(track, self.session) for track in response.json().get("collection", [])]
sc = Soundcloud()
print(sc.search("usquad")[0].get_stream_link().get_download_link())