35 lines
1.1 KiB
Python
Executable File
35 lines
1.1 KiB
Python
Executable File
import requests
|
|
import re
|
|
import os
|
|
import shutil
|
|
from typing import Union
|
|
|
|
def download_beatmap(beatmap_id:Union[int,str], output_dir:str,session:str):
|
|
"""Download a beatmap from osu! website
|
|
|
|
Args:
|
|
beatmap_id (int,str): the beatmap id
|
|
output_dir (str): the output directory
|
|
session (str): the session token
|
|
|
|
Raises:
|
|
Exception: if the request failed
|
|
"""
|
|
headers = {
|
|
"referer": f"https://osu.ppy.sh/beatmapsets/{beatmap_id}"
|
|
}
|
|
cookie = {
|
|
"osu_session": session
|
|
}
|
|
link = f"https://osu.ppy.sh/beatmapsets/{beatmap_id}/download"
|
|
response = requests.get(link, cookies = cookie ,headers=headers, allow_redirects=True)
|
|
if response.status_code != 200:
|
|
raise Exception(f"Request failed with status code {response.status_code}")
|
|
fname = ''
|
|
if "Content-Disposition" in response.headers.keys():
|
|
fname = re.findall("filename=(.+)", response.headers["Content-Disposition"])[0].strip('"')
|
|
else:
|
|
fname = str(beatmap_id) + ".osz"
|
|
fname = re.sub(r'[^\w_. -]', '_', fname)
|
|
with open(f"./{output_dir}/{fname}", "wb") as f:
|
|
f.write(response.content) |