Last active
September 5, 2023 08:10
-
-
Save saeedsajadi/6001a7328aa29b4a23ef03e5abee394f to your computer and use it in GitHub Desktop.
MrTehran playlist Downloader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import requests | |
from tqdm import tqdm | |
# Parameters for the API request | |
params = { | |
"playlist_id": "1GKM6Q2Bn82Dl0zVZrxew9", | |
"sort_id": 0, | |
"page": 1 | |
} | |
# Headers for the API request | |
headers = { | |
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/117.0", | |
"Accept": "application/json, text/plain, */*", | |
"Accept-Language": "en-US,en;q=0.5", | |
"Accept-Encoding": "gzip, deflate, br", | |
"x-api-key": "E3qfMUMiF68amHBoSScO832ypA6GgfEGVNoHhuxtfPNXPSCFueR8cNX1rgcoHqdG", | |
"Origin": "https://mrtehran.com", | |
"DNT": "1", | |
"Connection": "keep-alive", | |
"Referer": "https://mrtehran.com/", | |
"Sec-Fetch-Dest": "empty", | |
"Sec-Fetch-Mode": "cors", | |
"Sec-Fetch-Site": "same-site" | |
} | |
playlist_info_url = "https://api.mrtehran.com/web/v1/playlist_info" | |
response = requests.get(playlist_info_url, params=params, headers=headers) | |
data = response.json() | |
# Path of the downloaded folder | |
playlist_folder = "./" + data["playlist_name"] | |
# Check if the downloaded folder exists | |
if not os.path.isdir(playlist_folder): | |
# If the downloaded folder doesn't exist, create it | |
os.mkdir(playlist_folder) | |
# URL of the API endpoint | |
url = "https://api.mrtehran.com/web/v1/playlist_data" | |
# Loop until the end of the playlist is reached | |
while True: | |
# Send the API request and get the JSON response | |
response = requests.get(url, params=params, headers=headers) | |
data = response.json() | |
# Extract the links to the music files and download them | |
for track in data["tracks"]: | |
audio_url = "https://cdnmrtehran.ir/media/" + track["track_audio"] | |
filename = os.path.join(playlist_folder, track["track_artist"] + " - " + track["track_title"] + ".mp3") | |
if not os.path.exists(filename): | |
print(f"Downloading : {filename}") | |
response = requests.get(audio_url, stream=True) | |
total_size = int(response.headers.get("content-length", 0)) | |
block_size = 1024 | |
progress_bar = tqdm(total=total_size, unit="iB", unit_scale=True) | |
with open(filename, "wb") as f: | |
for data in response.iter_content(block_size): | |
progress_bar.update(len(data)) | |
f.write(data) | |
progress_bar.close() | |
# Check if we need to continue to the next page | |
if data["end"]: | |
break | |
else: | |
params["page"] += 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment