-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmusic.py
144 lines (117 loc) · 5.37 KB
/
xmusic.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import requests
from json import JSONDecodeError
import os
RED = "\033[91m"
END = "\033[0m"
BLUE = "\033[94m"
GREEN = "\033[92m"
# Definitions
def slugify(value: str):
forbidden = "/><:\"\\|?*"
for each in forbidden: value: str = value.replace(each, " ")
value: str = value.title()
value: str = value.replace(" ", "")
return f"{value}"
def ok(query_response: dict, quality_param: str):
if "vid" not in query_response \
or "token" not in query_response \
or "timeExpires" not in query_response \
or "links" not in query_response \
or "title" not in query_response:
return False
_links: dict = query_response.get("links", None)
_mp3: dict = _links.get("mp3", None)
if _mp3 is None: return False
_quality: dict = _mp3.get(quality_param, None)
return "k" in _quality
# Headers
query_headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"x-requested-with": "XMLHttpRequest",
"Referer": "https://x2download.app/en105/download-youtube-to-mp3",
"Referrer-Policy": "strict-origin-when-cross-origin"
}
convert_headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36",
"accept-language": "en-US,en;q=0.9",
"content-type": "application/x-www-form-urlencoded; charset=UTF-8",
"sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"Windows\"",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "cross-site",
"x-requested-key": "de0cfuirtgf67a",
"Referer": "https://x2download.app/",
"Referrer-Policy": "strict-origin-when-cross-origin"
}
os.system("cls")
download_directory: str = input("Directory: ")
print("\n")
downloaded_count = 0
while True:
context: str = f"[{downloaded_count}]"
url: str = input(f"{BLUE}{context} Identifier{END}: ")
quality = "4"
ft: str = "mp3"
# Initiate Query Request
query: requests.Response = requests.post("https://x2download.app/api/ajaxSearch", headers=query_headers,
data={
"q": url,
"vt": ft
})
# Validate Query Response
try:
json_res: dict = query.json()
if not ok(json_res, quality):
print(f"{RED}[0] Failed: Please check your internet connection.{END}")
continue
# Initiate Conversion Request
conversion: requests.Response = requests.post("https://backend.svcenter.xyz/api/convert-by-45fc4be8916916ba3b8d61dd6e0d6994", headers=convert_headers,
data={
'v_id': json_res['vid'],
'ftype': ft,
'fquality': json_res['links']['mp3'][quality]['k'],
'token': json_res['token'],
'timeExpire': json_res['timeExpires'],
'client': 'X2Download.app',
})
# Validate Conversion Status
con_json: dict = conversion.json()
# Failure
if con_json.get("c_status", "fail") != "ok" or con_json.get("d_url", "") == "":
print(f"{RED}[1] Failed: Please check your internet connection.{END}")
continue
# Pre Generate Filename
filename = slugify(json_res["title"])
extra_n = 0
final_filename: str = f"{filename}.{ft}"
# Generate Filename
while True:
if not os.path.exists(f"{download_directory}/{final_filename}"): break
extra_n += 1
if extra_n != 0: final_filename = f"{filename} [{extra_n}].{ft}"
# Directory Creation
if not os.path.exists(download_directory) or not os.path.isdir(download_directory): os.mkdir(download_directory)
# Output Response
download_response = requests.get(con_json.get("d_url"), stream=True)
print(con_json.get("d_url"))
# Output Stream
with open(f"{download_directory}/{final_filename}", "wb") as file:
for chunk in download_response.iter_content(chunk_size=1024):
if chunk: file.write(chunk)
# Increment Downloaded Count
if extra_n != 0: print(f"{GREEN}Downloaded{END}: \"{filename} [{extra_n}]\"\n")
else: print(f"{GREEN}Downloaded{END}: \"{filename}\"\n")
downloaded_count += 1
except JSONDecodeError:
print(f"{RED}[2] Failed: Please check your internet connection.{END}")
continue