Skip to content

Instantly share code, notes, and snippets.

@saeedsajadi
Last active September 5, 2023 18:44
Show Gist options
  • Save saeedsajadi/33068a6005acec74783fde6ce71c9021 to your computer and use it in GitHub Desktop.
Save saeedsajadi/33068a6005acec74783fde6ce71c9021 to your computer and use it in GitHub Desktop.
MrTehran downloader cli
#!/usr/bin/env python3
import os
import requests
import argparse
from tqdm import tqdm
# 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"
}
def download_playlist(playlist_id):
# Parameters for the API request
params = {
"playlist_id": playlist_id,
"sort_id": 0,
"page": 1
}
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 = get_list_folder(data["playlist_name"])
# URL of the API endpoint
url = "https://api.mrtehran.com/web/v1/playlist_data"
download_tracks(url, params, playlist_folder)
def download_list(type):
# Parameters for the API request
params = {
"page": 1
}
list_folder = get_list_folder(type)
# URL of the API endpoint
url = "https://api.mrtehran.com/web/v1/browse_" + type
download_tracks(url, params, list_folder)
def download_single_song(track_id):
# Parameters for the API request
params = {
"track_id": track_id
}
track_info_url = "https://api.mrtehran.com/web/v1/track_info"
response = requests.get(track_info_url, params=params, headers=headers)
data = response.json()
download_track(data)
def get_list_folder(path):
# Path of the downloaded folder
list_folder = os.getcwd() + "/" + path
# Check if the downloaded folder exists
if not os.path.isdir(list_folder):
# If the downloaded folder doesn't exist, create it
os.mkdir(list_folder)
return list_folder
def download_tracks(url, params, list_folder):
# 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)
if response.status_code == 200:
data = response.json()
# Extract the links to the music files and download them
for track in data["tracks"]:
download_track(track, list_folder)
# Check if we need to continue to the next page
if data["end"]:
break
else:
params["page"] += 1
else:
print("Not found")
def download_track(track, dir_path = os.getcwd() + "/"):
audio_url = "https://cdnmrtehran.ir/media/" + track["track_audio"]
filename = track["track_artist"] + " - " + track["track_title"] + ".mp3"
file_full_name = os.path.join(dir_path, filename)
if not os.path.exists(file_full_name):
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(file_full_name, "wb") as f:
for data in response.iter_content(block_size):
progress_bar.update(len(data))
f.write(data)
progress_bar.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="MrTehran Downloader")
parser.add_argument("--playlist", default="", help="playlist id")
parser.add_argument("--track", default="", help="track id")
parser.add_argument("--list", default="", help="list type")
args = parser.parse_args()
if args.playlist:
download_playlist(args.playlist)
if args.track:
download_single_song(args.track)
if args.list:
download_list(args.list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment