-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
150 lines (117 loc) · 4.04 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import requests
from bs4 import BeautifulSoup
import threading
import re
import json
# https://codeforces.com/contests/page/2?complete=true
url = "https://codeforces.com/contests/page/{}?complete=true"
data = []
def get_upcomming_contests():
print("Getting data from {}".format(url))
r = requests.get(url.format(1))
soup = BeautifulSoup(r.text, 'html.parser')
tables = soup.find_all('table')
table = tables[0]
rows = table.find_all('tr')
print("Found {} rows".format(len(rows)))
for row in rows:
# check if the row is a header
th = row.find('th')
if th:
continue
cols = row.find_all('td')
name = cols[0].text
contest_url = url.format(1)
time = cols[2].text.strip()
length = cols[3].text.strip()
time_left = cols[4].text.strip()
time_left = time_left.replace('\n', ' ').replace(
'\r', '').replace('Before start', '').strip()
participants = "0"
participants_url = contest_url
atags = cols[5].find_all('a')
if len(atags) > 0:
participants = atags[1].text.strip()
participants = re.findall(r'\d+', participants)[0]
participants_url = atags[1]['href']
nremove = ["Virtual participation »", "Enter »", "\n", "\r"]
for s in nremove:
name = name.replace(s, "")
name = name.strip()
name = name + f" [Upcoming ({time_left})]"
contest = {
"name": name,
"url": contest_url,
"time": time.replace('\n', ' ').replace('\r', '')[:17],
"length": length,
"participants": int(participants),
"participants_url": participants_url
}
data.append(contest)
def get_data(url):
print("Getting data from {}".format(url))
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
tables = soup.find_all('table')
table = tables[1]
rows = table.find_all('tr')
print("Found {} rows".format(len(rows)))
for row in rows:
# check if the row is a header
th = row.find('th')
if th:
continue
cols = row.find_all('td')
name = cols[0].text
contest_url = cols[0].find('a')['href']
# <td class="dark">Nov/21/2022<br>
# 20:35<sup title="timezone offset" style="font-size:8px;">
# UTC+6</sup>
# </td>
time = cols[2].text.strip()
length = cols[3].text.strip()
standings_url = cols[4].find('a')['href']
participants = cols[5].text.strip()
participants = re.findall(r'\d+', participants)[0]
participants_url = cols[5].find('a')['href']
nremove = ["Virtual participation »", "Enter »", "\n", "\r"]
for s in nremove:
name = name.replace(s, "")
name = name.strip()
contest = {
"name": name,
"url": contest_url,
"time": time.replace('\n', ' ').replace('\r', '')[:17],
"length": length,
"standings_url": standings_url,
"participants": int(participants),
"participants_url": participants_url
}
data.append(contest)
def get_last_page():
# .pagination li a text
r = requests.get(url.format(1))
soup = BeautifulSoup(r.text, 'html.parser')
pagination = soup.find(class_="pagination")
li = pagination.find_all('li')
last = li[-2].find('a').text
return int(last)
def main():
threads = []
last = get_last_page()
print("Last page is {}".format(last))
for i in range(0, last+1):
if i == 0:
t = threading.Thread(target=get_upcomming_contests)
else:
t = threading.Thread(target=get_data, args=(url.format(i),))
t.start()
threads.append(t)
for t in threads:
t.join()
with open('data.js', 'w', encoding='utf-8') as f:
f.write("const data = ")
json.dump(data, f, ensure_ascii=False, indent=4)
f.write(";")
if __name__ == "__main__":
main()