Upgrade pp calculator to use /osu endpoint

This commit is contained in:
newt 2021-06-12 22:42:32 +01:00
parent 71f39a971d
commit 99a524120e
No known key found for this signature in database
GPG key ID: 8F525002794C8168

View file

@ -1,29 +1,14 @@
import oppaipy import oppaipy
import requests import requests
import zipfile
import os import os
import io
# Request information about the beatmap # Request information about the beatmap
id = input('Please enter a beatmap ID: ').strip() id = input('Please enter a beatmap ID: ').strip()
r = requests.get('https://osu.ppy.sh/b/{0}'.format(id)) # Fetch the diff page - redirects to the mapset beatmap = requests.get('https://osu.ppy.sh/osu/{0}'.format(id)) # Fetch the osu file
setId = r.url[31:len(r.url)-(5+len(id))] # Figure out the ID of the mapset open('{0}.osu'.format(id), 'w').write(beatmap.text.replace('\n', '')) # Write this difficulty to the disk
d = requests.get('https://api.chimu.moe/v1/download/{0}?n=0'.format(setId)) # Try and find the beatmap on chimu - an osu beatmap mirror as the osu API does not have beatmap download support lines = open('{0}.osu'.format(id), 'r').readlines() # Read the lines of the osu file
# Read all of the .osu files and find the correct one for the difficulty
name = ''
with zipfile.ZipFile(io.BytesIO(d.content)) as zip: # Open the fetched osz (beatmap) as a zip
for zipinfo in zip.infolist(): # For each file
if zipinfo.filename.endswith('.osu'): # If the file is a .osu (difficulty)
file = zip.read(zipinfo) # Read the file
file = file.decode('UTF-8') # Decode it to what it usually would be like
lines = file.splitlines() # Split the lines
diffId = [x[10:len(x)].strip() for x in lines if x.startswith('BeatmapID:')][0] # Find the ID of this difficulty
if diffId == id:
open('{0}.osu'.format(id), 'w').write(file) # Write this difficulty to the disk
artist = [x[7:len(x)].strip() for x in lines if x.startswith('Artist:')][0] # Figure out the artist of the song artist = [x[7:len(x)].strip() for x in lines if x.startswith('Artist:')][0] # Figure out the artist of the song
name = [x[13:len(x)].strip() for x in lines if x.startswith('TitleUnicode:')][0] # Figure out the name of the song name = [x[6:len(x)].strip() for x in lines if x.startswith('Title:')][0] # Figure out the name of the song
diff = [x[8:len(x)].strip() for x in lines if x.startswith('Version:')][0] # Figure out the difficulty name diff = [x[8:len(x)].strip() for x in lines if x.startswith('Version:')][0] # Figure out the difficulty name
name = '{0} - {1} [{2}]'.format(artist, name, diff) # Put these three together into a parseable format by osu name = '{0} - {1} [{2}]'.format(artist, name, diff) # Put these three together into a parseable format by osu