Last active
August 29, 2015 14:05
-
-
Save mzhang28/d36caee2433656251287 to your computer and use it in GitHub Desktop.
Python - Copy osu! data
This file contains 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 urllib.request; | |
import shutil; | |
import struct; | |
modes = [ | |
"osu!", | |
"Taiko", | |
"CtB", | |
"osu!mania"]; | |
osu_dir = "C:/Program Files (x86)/osu!"; | |
out_dir = "C:/Users/Michael/Documents/ReplayExport"; | |
if not os.path.exists(out_dir): | |
os.makedirs(out_dir); | |
replay_dir = osu_dir + "/Data/r"; | |
replays = [r for r in os.listdir(replay_dir) if r.split(".")[1] == "osr"]; | |
total = len(replays); | |
print ("Total: " + str(total) + " replays."); | |
c = 0; | |
for replay in replays: | |
# print(replay); | |
full_path = replay_dir + "/" + replay; | |
file = open(full_path, "rb"); | |
try: | |
mode = int(repr(file.read(1))[-2]); | |
# print("\t"+modes[mode]); | |
file.read(4); # version | |
file.read(1); # continue | |
beatmap_hash_len = ord(repr(file.read(1))[2:-1]); | |
beatmap_hash = repr(file.read(beatmap_hash_len))[2:-1]; | |
# print("\t"+beatmap_hash); | |
file.read(1); | |
q = file.read(1); | |
# print(struct.unpack("b", q)[0]); | |
username_len = struct.unpack("b", q)[0]; #int(repr(q)[4:-1],16); | |
username = repr(file.read(username_len))[2:-1]; | |
# print("\t"+str(username)); | |
file.read(1); #continue | |
replay_hash_len = ord(repr(file.read(1))[2:-1]); | |
replay_hash = repr(file.read(replay_hash_len))[2:-1]; | |
# print("\t"+replay_hash); | |
file.read(2); #300 | |
file.read(2); #100 | |
file.read(2); #50 | |
file.read(2); #geki | |
file.read(2); #katu | |
file.read(2); #miss | |
score = list(struct.unpack("i", file.read(4)))[0]; | |
# print(score); | |
api_url = "http://api.osu.miz.hexide.com/metadata/" + beatmap_hash; | |
try: | |
data = urllib.request.urlopen(api_url); | |
for line in data: | |
l = repr(line)[2:-1].strip(); | |
if l.find(":") > 0: | |
a = l.split(":")[0].strip("\""); | |
b = l.split(":")[1].strip(" \"")[:-4]; | |
b = b.replace("\\'","'").replace("\\\"","\""); | |
if a == "name": | |
# print ("\t" + b); | |
res = username + " (" + modes[mode] + ") - " + b + " (" + str(score) + ").osr"; | |
full_dest = out_dir + "/" + res; | |
print (res); | |
shutil.copyfile(full_path, full_dest); | |
print (str(c) + " / " + str(total)); | |
# print ("Done.\n"); | |
break; | |
data.close(); | |
except Exception as e: | |
print ("ERROR on URL " + api_url + ": " + repr(e)); | |
print ("ERROR on replay " + replay); | |
except Exception as e: | |
print ("ERROR on replay " + replay + ": " + repr(e)); | |
finally: | |
file.close(); | |
c += 1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment