-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathextractor.py
60 lines (54 loc) · 1.77 KB
/
extractor.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
from bs4 import BeautifulSoup
import requests
def getSolution(ques, sol):
r = session.get(sol).content
soup = BeautifulSoup(r, 'html.parser')
code = soup.find('pre', class_ = "linenums").get_text()
return code
def findCorrectSolution(ques):
r = session.get(r"https://cses.fi/problemset/view/"+ques+"/").content
soup = BeautifulSoup(r,'html.parser')
if int(soup.find('p').string.split()[-1]) == 0: # No of submissions
return None
for link in soup.find_all('a', attrs = {'class':'details-link'}):
sol = link['href']
res = soup.find('a', href = sol, class_ ='').span['class'][2] #get status of a solution
if res == 'full':
return 'https://cses.fi'+sol
return None
def createSession(username, password):
loginData = {
'nick': username,
'pass': password
}
with requests.Session() as s:
url = 'https://cses.fi/login'
soup = BeautifulSoup(s.get(url).content, 'html.parser')
loginData['csrf_token'] = soup.find('input', attrs = {'name':'csrf_token'})['value']
r = s.post(url, data = loginData)
return s
def getQuestions():
ques = dict()
r = session.get(r'https://cses.fi/problemset/').content
soup = BeautifulSoup(r, 'html.parser')
for t in soup.find_all('li', class_ = 'task'):
quesID = t.a['href'].split('/')[-1]
ques[quesID] = t.a.string
return ques
if __name__ == "__main__":
r = requests.get("https://cses.fi/problemset/").content
soup = BeautifulSoup(r,'html.parser')
username = input('username: ')
password = input('passowrd: ')
session = createSession(username, password)
questions = getQuestions()
for _ in questions:
sol = findCorrectSolution(_);
if not sol:
continue
code = getSolution(_, sol)
ext = '.cpp'
path = 'cses/' + _ + ' - ' + questions[_] + ext
file = open(path, 'w')
file.writelines(code.split('\n'))
file.close()