-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawl.py
42 lines (32 loc) · 1.19 KB
/
crawl.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
import requests
from bs4 import BeautifulSoup as BS
import json
import threading
def getData(tier, p):
val = requests.get(f'https://api.solved.ac/problem_level.php?id={p.text}').text
d = json.loads(val)
tier[d['level']] = tier.get(d['level'], 0) + 1
user = input('user: ')
res = requests.get(f'https://www.acmicpc.net/user/{user}').text
soup = BS(res, 'lxml')
problems = soup.select('body > div.wrapper > div.container.content > div.row > div:nth-child(2) > div:nth-child(3) > div.col-md-9 > div:nth-child(1) > div.panel-body > span')[::2]
tier = {}
for p in problems:
t = threading.Thread(target=getData, args=[tier, p])
t.start()
header = ['Bronze', 'Silver', 'Gold', 'Platinum', 'Diamond', 'Ruby']
for thread in threading.enumerate():
if thread.daemon:
continue
try:
thread.join()
except RuntimeError as err:
if 'cannot join current thread' in err.args[0]:
# catchs main thread
continue
else:
raise
print('Unranked', tier.get(0, 0))
for i in range(1, 30, 5):
print(header[(i-1)//5], sum([tier.get(j, 0) for j in range(i, i+5)]))
print('Total', sum(tier.values()))