Last active
December 23, 2024 09:09
-
-
Save pandamoon21/998238286e965b7724ef56aa3624e30c to your computer and use it in GitHub Desktop.
Bugs.co.kr MV 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
""" | |
A simple script to download music videos from bugs.co.kr | |
author: pandamoon21 | |
requirements: requests, rich, tqdm | |
Usage: | |
bugs_mv_dl.py {url/videoid} {quality} | |
notes: | |
- quality is optional, default set to FHD | |
- SD (< 720p), HD (720p), FHD (1080p) | |
example: | |
-> bugs_mv_dl.py https://music.bugs.co.kr/mv/624404 FHD | |
-> bugs_mv_dl.py https://music.bugs.co.kr/mv/624404 | |
-> bugs_mv_dl.py 624404 | |
last update: 20/04/2024 | |
""" | |
import re | |
import requests | |
import sys | |
import warnings | |
from pathlib import Path | |
from rich import print | |
from tqdm import TqdmExperimentalWarning | |
from tqdm.rich import tqdm | |
warnings.filterwarnings("ignore", category=TqdmExperimentalWarning) | |
session = requests.Session() | |
headers = { | |
'Accept': 'application/json, text/javascript, */*; q=0.01', | |
'Accept-Language': 'en-US,en;q=0.9', | |
'Connection': 'keep-alive', | |
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', | |
'DNT': '1', | |
'Origin': 'https://music.bugs.co.kr', | |
'Sec-Fetch-Dest': 'empty', | |
'Sec-Fetch-Mode': 'cors', | |
'Sec-Fetch-Site': 'same-origin', | |
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0', | |
'X-Requested-With': 'XMLHttpRequest', | |
'sec-ch-ua': '"Microsoft Edge";v="119", "Chromium";v="119", "Not?A_Brand";v="24"', | |
'sec-ch-ua-mobile': '?0', | |
'sec-ch-ua-platform': '"Windows"', | |
} | |
def get_playback(videoid, quality): | |
res = session.post( | |
url="https://music.bugs.co.kr/mvPlayer/lightSecureUrl", | |
data={ | |
"mvId": videoid, | |
"pl_type": "html5", | |
"quality": quality | |
}, | |
headers={ | |
'Referer': f'https://music.bugs.co.kr/mv/{videoid}', | |
**headers | |
} | |
) | |
return res.json() | |
def download_file(input_url, videoid, output): | |
res = session.get( | |
url=input_url, | |
headers={ | |
'Referer': f'https://music.bugs.co.kr/mv/{videoid}', | |
**headers | |
}, | |
allow_redirects=True, | |
stream=True | |
) | |
chunk_size = 1 | |
TOTAL_SIZE = int(res.headers.get("Content-Length", 0)) | |
CHUNK_SIZE = chunk_size * 10**6 | |
output = Path(f"{output}.mp4") | |
with ( | |
open(str(output), "wb") as video, | |
tqdm(total=TOTAL_SIZE, desc=f"Download {output}", unit="B", unit_scale=True) as bar | |
): | |
for chunk in res.iter_content(chunk_size=CHUNK_SIZE): | |
# writing one chunk at a time to the file | |
if chunk: | |
size = video.write(chunk) | |
bar.update(size) | |
try: | |
videoid = sys.argv[1] | |
except Exception: | |
# using custom videoid from this py file instead | |
videoid = "input_video_id_here" | |
try: | |
quality = sys.argv[2] | |
except Exception: | |
# using FHD as default -> SD, HD, FHD | |
quality = "FHD" | |
if "http" in videoid: | |
# parse videoid | |
videoid = re.search(r"\d{4,6}", videoid)[0] | |
data = get_playback(videoid, quality) | |
artist_name = data["mv"]["artist_nm"] | |
mv_title = data["mv"]["mv_title"] | |
album_title = data["mv"]["album_title"] | |
title = "{} - {}".format(artist_name, mv_title) | |
print(f" + Artist Name : {artist_name}") | |
print(f" + MV Title : {mv_title}") | |
print(f" + Album Title : {album_title}") | |
lsu_url = data["url"] | |
print("\nDownloading...") | |
print(f" + Filename : {title}") | |
# Download the file | |
url = f"https://music.bugs.co.kr/lsu_vurl/{lsu_url}" | |
download_file(url, videoid, title) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment