-
Notifications
You must be signed in to change notification settings - Fork 1
/
JOJCodeDownloader_v1.0.py
102 lines (79 loc) · 3.56 KB
/
JOJCodeDownloader_v1.0.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
import os
import requests
from bs4 import BeautifulSoup
import wget
import zipfile
import rarfile
import csv
import shutil
import json
# py -m py_compile trial.py
def main():
while True:
global interval, cookie, url
config = json.load(open('./config.json'))
url = config[config['useWhich']]
section = int(config['section']) # "0" for no limit, "1" for S2-s1, "2" for S2-s2, "3" for 9/27's lab
sid = "" # your cookie
cookie = {"save": "1", "sid": sid}
if section > 0:
with open('section.csv', 'r') as csvfile:
rdr = csv.reader(csvfile)
studentID = [row[section] for row in rdr]
pre_page = requests.get(url, cookies=cookie).text
pre_soup = BeautifulSoup(pre_page, 'html.parser')
dir_ = './' + pre_soup.find("h1", class_="location-current").get_text().replace("\n", "").replace(" ", "").replace(
":", "").replace("'", "") + '/'
if os.path.exists(dir_):
shutil.rmtree(dir_)
if os.path.exists('./zips/'):
shutil.rmtree('./zips/')
os.mkdir(dir_)
os.mkdir(r'./zips')
page = requests.get(url + "/scoreboard", cookies=cookie).text
soup = BeautifulSoup(page, 'html.parser')
post_list = soup.find_all("tr")
for post in post_list:
if not post.find_all("td"):
continue
if not post.find("td", class_="col--problem_detail").find("a"):
continue
uid = post.find("td", class_="col--uid").get_text().replace("\n", "").replace(" ", "")
if section > 0:
if not (uid in studentID):
continue
rank = post.find("td", class_="col--rank").get_text().replace("\n", "").replace(" ", "")
score = post.find("td", class_="col--total_score").get_text().replace("\n", "").replace(" ", "")
name = post.find('a').get_text().replace("\n", "").replace(" ", "")
link_list = post.find_all("td", class_="col--problem_detail")
print('\n', rank, uid, score, name)
info = rank + '-' + score + '-' + name + '-' + uid
os.mkdir(dir_ + info + '/')
it = 0
if len(link_list) == 1:
it = -1
for raw_link in link_list:
it = it + 1
multi_p = ""
if it > 0:
multi_p = "p" + str(it) + '/'
os.mkdir(dir_ + info + "/" + multi_p)
if not raw_link.find("a"):
continue
link = raw_link.find("a").get("href")
url = "https://joj.sjtu.edu.cn" + link + "/code"
r = requests.get(url, cookies=cookie)
wget.download(r.url, out='./zips/' + info + "_" + str(it))
if zipfile.is_zipfile('./zips/' + info + "_" + str(it)):
f = zipfile.ZipFile('./zips/' + info + "_" + str(it), 'r')
f.extractall(dir_ + info + '/' + multi_p)
f.close()
elif rarfile.is_rarfile('./zips/' + info + "_" + str(it)):
z = rarfile.RarFile('./zips/' + info + "_" + str(it), 'r')
z.extractall(dir_ + info + '/' + multi_p)
z.close()
else:
print(info + "_" + str(it) + " is not a zip/rar file")
input("\nagain?")
if __name__ == '__main__':
main()