-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathppeval.py
107 lines (92 loc) · 4.41 KB
/
ppeval.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import requests
import ast
import psycopg2
__author__ = 'Existanza'
key_file = open('/home/mz/PycharmProjects/keys/osu.txt', 'r')
api_key = key_file.read()
api_key = api_key[:-1]
user = '2063607'
limit = '3' # 1-50
game_mode = '1' # 0 = osu!, 1 = Taiko, 2 = CtB, 3 = osu!mania
convert = '0' # specify whether converted beatmaps are included (0 = not included, 1 = included).
# Only has an effect if m is chosen and not 0. Optional, default is 0.
db_name = 'db6'
file = open('/home/mz/PycharmProjects/keys/postgres.txt', 'r')
pwd = file.read()
pwd = pwd[:-1]
conn = psycopg2.connect(database=db_name, user='postgres', password=pwd)
c = conn.cursor()
def beatmap_info(map_id, user_score):
map_url = 'https://osu.ppy.sh/api/get_beatmaps' + '?k=' + api_key + '&b=' + map_id + '&m=' + game_mode + '&a=' + convert
print('Getting map info: ' + map_id)
rc_map = requests.get(map_url)
rc_map_text = rc_map.text.replace(":null", ":\"null\"")
map_info = ast.literal_eval(rc_map_text)
if map_info:
for i in range(len(map_info)):
dict = ast.literal_eval(str(map_info[i]))
if dict['approved'] == '1':
ret = get_scores(map_id, dict['title'], dict['version'], dict['approved_date'], dict['difficultyrating'],
dict['bpm'], dict['hit_length'], user_score)
return ret
def get_scores(key, title, version, approved_date, stars, bpm, hit_length, user_score):
scores_url = 'https://osu.ppy.sh/api/get_scores' + '?k=' + api_key + '&b=' + key + '&m=1'
print('Getting scores: ' + key)
rc2 = requests.get(scores_url)
res2 = ast.literal_eval(rc2.text)
hr_found, dt_found, hrdt_found, no_mod_found = (False,)*4
for i in range(len(res2)):
dic = ast.literal_eval(str(res2[i]))
score = dic['pp']
mode = dic['enabled_mods']
mode_dic = {'0': '', '16': 'HR', '64': 'DT', '80': 'HRDT', '576': 'DT', '592': 'HRDT'}
if mode in mode_dic:
if mode_dic[mode] == 'HR' and not hr_found:
c.execute('''INSERT INTO maps VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''',
(key, title, version, approved_date, stars, score, mode_dic[mode], bpm, hit_length, user_score))
hr_found = True
elif mode_dic[mode] == 'DT' and not dt_found:
c.execute('''INSERT INTO maps VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''',
(key, title, version, approved_date, stars, score, mode_dic[mode], bpm, hit_length, user_score))
dt_found = True
elif mode_dic[mode] == 'HRDT' and not hrdt_found:
c.execute('''INSERT INTO maps VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''',
(key, title, version, approved_date, stars, score, mode_dic[mode], bpm, hit_length, user_score))
hrdt_found = True
elif mode_dic[mode] == '' and not no_mod_found:
c.execute('''INSERT INTO maps VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)''',
(key, title, version, approved_date, stars, score, mode_dic[mode], bpm, hit_length, user_score))
no_mod_found = True
return approved_date
def user_best(user, game_mode, limit):
best_url = 'https://osu.ppy.sh/api/get_user_best' + '?k=' + api_key + '&u=' + user + '&m=' + game_mode + '&limit=' + limit
rc = requests.get(best_url)
res = ast.literal_eval(rc.text)
for i in range(len(res)):
di = ast.literal_eval(str(res[i]))
beatmap_info(di['beatmap_id'], di['pp'])
def get_all(game_mode):
date = '2005-01-01'
total = 0
while date is not None:
maps_url = 'https://osu.ppy.sh/api/get_beatmaps' + '?k=' + api_key + '&m=' + game_mode + \
'&a=' + convert + '&since=' + date + '&limit=5'
rc_maps = requests.get(maps_url)
rc_maps_text = rc_maps.text.replace(":null", ":\"null\"")
maps_info = ast.literal_eval(rc_maps_text)
for i in range(len(maps_info)):
print(str(total + i) + '/' + str(total + len(maps_info)))
di = ast.literal_eval(str(maps_info[i]))
date = beatmap_info(di['beatmap_id'], '0')
if date is None:
break
print(date)
total += len(maps_info)
user_best(user, game_mode, limit)
# get_all(game_mode)
conn.commit()
print('k')
c.execute('''SELECT * FROM maps ORDER BY score''')
rows = c.fetchall()
print(len(rows))
conn.close()