-
Notifications
You must be signed in to change notification settings - Fork 2
/
user.py
98 lines (75 loc) · 3.25 KB
/
user.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import requests
import errno
import os
import sys
import urllib.request
# Gets the Score Ids from Ripple's API then downloads the corresponding replays
def getReplays(username, mode):
url = "https://ripple.moe/api/v1/users/scores/best?name=" + username + "&mode=" + str(mode)
data = getJSON(url)
# Check if username directory exists, create if it doesn't.
newpath = os.getcwd() + "/" + username
if not os.path.exists(newpath):
os.makedirs(newpath)
# Download each score and store inside the username's folder
try:
for score in data['scores']:
songName = score['beatmap']['song_name']
scoreId = score['id']
# Replace any nasty characters in the file name
nastyCharacters = ["\/", "\\", "<", ">", "?", ":", "*", "|", "\""]
for char in nastyCharacters:
songName = songName.replace(char, " ")
# Specify file path
directory = os.path.join(os.getcwd() + "/" + username)
fullfilename = directory + "/" + username + " - " + songName + '.osr'
# Download Replay
try:
# Create Opener w/ headers
opener=urllib.request.build_opener()
opener.addheaders=[('User-Agent','Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1941.0 Safari/537.36')]
urllib.request.install_opener(opener)
# URL & File path
url = 'https://ripple.moe/web/replays/' + str(scoreId)
local = str(fullfilename)
# Download
urllib.request.urlretrieve(url, local)
print("Downloading Replay: " + songName + ".osr...")
except Exception as e:
print("ERROR: Could not download file: " + songName + ".osr", e)
sys.exit(1)
print("Download Complete.")
return
except Exception as e:
print("\nCan't download replays because the user doesn't have any scores for this mode.", e)
sys.exit(1)
# Get Game Mode from the user
def getMode():
mode = input("\nSelect the game mode you'd like to download replays for\n1. osu!\n2. Taiko\n3. CTB\n4. Mania\n\nGame Mode: ")
mode = int(mode)
# Check for invalid mode
if mode < 1 or mode > 4:
print("\nInvalid choice given, please try again.")
getMode()
# Mode number for the Ripple API is 1 less than the options given in the mode input
return getReplays(username, mode - 1)
# Gets JSON then calls a given function afterwards
def getJSON(url):
try:
data = requests.get(url=url).json()
if data['code'] and data['code'] != 200:
print("Invalid request given, please try again\n")
sys.exit(1)
return data
except requests.exceptions.Timeout:
data = requests.get(url=url).json()
except requests.exceptions.TooManyRedirects:
print("Invalid link given")
except requests.exceptions.RequestException as e:
print (e)
sys.exit(1)
# Main Execution
username = input("Enter a Ripple username to start downloading replays: ")
url = 'https://ripple.moe/api/v1/users?name=' + username
userStats = getJSON(url)
getMode()