-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.py
executable file
·132 lines (89 loc) · 3.67 KB
/
main.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python3
# coding: utf-8
import os
import threading
from getpass import getpass
import extensions
from network_tools import down_file, get_soup, login
from submit_record import SubmitRecord
__author__ = 'isac322'
def get_solved_problems(sp):
problems = set()
"""
2021-03-02 아래 DOM 읽어오는 2줄 수정
"""
for tag in sp.find(class_='panel-body').findAll('a'):
problems.add(tag.text)
return problems
working_dir = ''
def make_code_file(problem_num, language):
extension = extensions.extension_name[language]
file_name = problem_num + '.' + extension
directory = os.path.join(working_dir, "{:05d}".format(int(problem_num)))
return open(os.path.join(directory, file_name), 'w+', encoding='utf-8')
screenLock = threading.Lock()
def analyze_problem(problem_num):
url = 'https://www.acmicpc.net/status?'
query = {'problem_id': problem_num, 'user_id': user_id, 'result_id': '4', 'language_id': '-1', 'from_mine': '1'}
for k, v in query.items():
url += k + '=' + v + '&'
table = dict()
page = get_soup(url, query)
rows = page.find('tbody').find_all('tr')
if len(rows) is 0:
return table
for row in rows:
column = row.find_all('td')
language = column[6].text.strip()
length = None
length_text = column[7].text.strip()
if len(length_text) != 0:
length = length_text.split()[0]
element = SubmitRecord(judge_id=column[0].text,
mem=column[4].contents[0],
time=column[5].contents[0],
code_len=length)
if table.get(language) is None:
table[language] = element
else:
table[language] = min(table[language], element)
problem_name = rows[0].find_all('td')[2].a['title']
screenLock.acquire()
print('{:5s} {}'.format(problem_num, problem_name))
for l, e in table.items():
print('\t{:10s} {}'.format(l, e))
screenLock.release()
return table
def analyze_and_make(problem_num):
submitted_codes = analyze_problem(problem_num)
for language, source_code in submitted_codes.items():
with make_code_file(problem_num, language) as file:
downloaded = down_file(source_code.judge_id, full_cookie)
file.write(downloaded.decode())
"""
Unrated 문제 추가 요망
"""
ignore_list = frozenset(('10947', '9999', '13757'))
def get_submitted_files(problems):
for problem_num in problems:
directory = os.path.join(working_dir, "{:05d}".format(int(problem_num)))
if not os.path.exists(directory):
os.makedirs(directory)
thread_file_maker = threading.Thread(target=analyze_and_make, args=(problem_num,), daemon=False)
thread_file_maker.start()
if __name__ == '__main__':
user_id = input('enter nickname : ')
"""
2021-03-02 아래 5줄 수정
"""
# user_pw = getpass('enter password : ')
# full_cookie = login(user_id, user_pw)
# print(full_cookie)
full_cookie = input("Please Copy&Paste BOJ Cookie Here. Example: 'a=1; b=2; c=3; d=4;' : ")
print("오래된 언어의 경우 extensions.py를 적절히 수정해 주지 않으면 다운로드 도중 오류가 날 수도 있습니다. Java 8, C++98에서 오류 나는 것을 확인했습니다. 해당 소스코드 외에는 정상적으로 다운로드 잘 됩니다.")
soup = get_soup('https://acmicpc.net/user/' + user_id)
working_dir = os.path.join(os.getcwd(), user_id)
if not os.path.exists(working_dir):
os.makedirs(working_dir)
problem_set = get_solved_problems(soup)
get_submitted_files(problem_set)