-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcodeForces.py
127 lines (93 loc) · 3.07 KB
/
codeForces.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
#!/usr/bin/env python
# coding: utf-8
import mechanicalsoup
import json
import os
import getpass
import time
import pickle
delay_in_seconds= 0.5
browser = mechanicalsoup.StatefulBrowser()
#inputing handle and opening it's submission page
handle = input("Enter handle name:")
homeUrl = "https://codeforces.com/submissions/"+handle
browser.open(homeUrl)
first_time_flag = False
#changing the directory
path_to_download_directory = f'{os.getcwd()}/{handle}'
if not os.path.exists(path_to_download_directory):
os.makedirs(path_to_download_directory)
first_time_flag = True
os.chdir(path_to_download_directory)
succesID=[] #holds successful submissions ID
links=[] #holds successful submissions links
end_search = False
if (first_time_flag == False):
idFile = open('succesfulIdRecord','rb')
savedID = pickle.load(idFile)
idFile.close()
else:
savedID=[]
#counting total number of pages
pageIndex = browser.get_current_page().select(".pagination")
pageNumberList=[]
for row in pageIndex:
cells = row.find_all("span",attrs = {"class": "page-index"})
for x in cells:
pageNumberList.append(x['pageindex'])
if not pageNumberList:
totalNumberOfPages = 2
else:
totalNumberOfPages = int(max(pageNumberList))
pageBaseLink = f'https://codeforces.com/submissions/{handle}/page/'
for i in range(1,totalNumberOfPages):
pageLink = pageBaseLink + str(i)
browser.open(pageLink)
# selecting table for getting successful submitted question list
table = browser.get_current_page().select("table.status-frame-datatable")
for row in table:
if (end_search == True):
break
col = row.find_all("span",attrs = {"class": "submissionVerdictWrapper"})
for x in col:
subid=x['submissionid']
if (subid in savedID):
end_search = True
break
verdict=x['submissionverdict']
if(verdict=='OK'):
succesID.append(subid)
if(end_search==True):
break
with open('succesfulIdRecord','wb') as idFile:
pickle.dump(succesID,idFile)
# saving list of succesful questions link
for row in table:
col = row.find_all("a",attrs = {"class": "view-source"})
for x in col:
subid=x['submissionid']
links.append(x['href'])
baseurl="https://codeforces.com"
# Generating link for getting solution
for link in links:
time.sleep(delay_in_seconds)
finalurl = baseurl+link
print(finalurl)
browser.open(finalurl)
#saving code in a string
codestr=''
codebox = browser.get_current_page().select('#program-source-text')
for x in codebox:
codestr+=x.text
datatable =browser.get_current_page().select(".datatable")
#getting name of Question number
textlist=[]
for row in datatable:
anchors = row.find_all('a')
for x in anchors:
textlist.append(x.text)
filename=textlist[1]
#saving code with appropriate file name
if(os.path.isfile('./'+filename)==False):
with open(filename,'w') as f:
f.write(codestr)