-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathscraper.py
154 lines (116 loc) · 4.62 KB
/
scraper.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
151
152
153
154
import requests
from bs4 import BeautifulSoup
from utils.exception import ValidationException
from problem.models import *
from codechef.models import *
def divisionScraper(contest_id):
contest_url = f"https://www.codechef.com/api/contests/{contest_id}"
contest_req = requests.get(contest_url)
if contest_req.status_code != 200:
raise ValidationException(
'Failed Scrapping Codechef Contest Divisions')
contest_req = contest_req.json()
return contest_req
def contestScraper(offset, contest_type):
query_contest_url = f"https://www.codechef.com/api/list/contests/{contest_type}?sort_by=START&sorting_order=desc&offset={offset}&mode=premium"
# Query URL might change in future.
contest_data = requests.get(query_contest_url)
if contest_data.status_code != 200:
raise ValidationException('Failed Scrapping Codechef Contests')
contest_data = contest_data.json()
return contest_data
def problemScraper(contest_code):
query_problem_url = f"https://www.codechef.com/api/contests/{contest_code}"
# Query URL might change inuserid future.
problem_data = requests.get(query_problem_url)
if problem_data.status_code != 200:
raise ValidationException('Failed Scrapping Codechef Problems')
problem_data = problem_data.json()
return problem_data
def UserSubmissionDetail(problemcode, contest, user):
URL = f"https://www.codechef.com/{contest}/status/{problemcode},{user}"
r = requests.get(URL)
soup = BeautifulSoup(r.content, 'html5lib')
problemTable = soup.findAll('table', class_="dataTable")
problemRow = problemTable[0].findAll('tr')
problemRow.pop(0)
submissionlist = []
if len(problemRow) == 0 or problemRow[0].text == 'No Recent Activity':
return submissionlist
for problem in problemRow:
baseurl = "https://www.codechef.com"
problemDetails = problem.findAll('td')
subid = problemDetails[0].text
subtime = problemDetails[1].text
verdict = problemDetails[3].find('span').get('title')
if len(verdict) == 0:
verdict = (problemDetails[3].find('span').text)
verdict = verdict[:verdict.index('[')]
if int(verdict) == 100:
verdict = "accepted [100/100]"
else:
verdict = "partially accepted [" + verdict + "/100]"
lang = problemDetails[6].text
link = baseurl + problemDetails[7].find('a').get('href')
subformat = {
'subid': subid,
'subtime': subtime,
'verdict': verdict,
'lang': lang,
'link': link,
}
submissionlist.append(subformat)
return submissionlist
def recentSubmissions(userid):
URL = f"https://www.codechef.com/recent/user?user_handle={userid}"
r = requests.get(URL)
r = BeautifulSoup(r.content, 'html5lib')
recentSubs = r.findAll('tbody')
recentlist = []
for sub in recentSubs:
subd = sub.findAll('tr')
subd.pop(-1)
try:
query = subd[0].text[:18]
except:
query = "Profile found successfully"
if query == 'No Recent Activity':
break
for prob in subd:
baseurl = "https://www.codechef.com"
det = prob.findAll('td')
probid = det[1].find('a').text
probid = probid[:probid.index('<')]
link = det[1].find('a').get('href')
link = link.replace("\\", "")
link = baseurl + link
subtime = prob.find('span', class_="tooltiptext")
try:
subtime = subtime.text
subtime = subtime[:subtime.index('<')]
except:
break
verdict = det[2].find('span').get('title')
if len(verdict) == 0:
verdict = (det[2].find('span').text)
verdict = verdict[:verdict.index('[')]
if int(verdict) == 100:
verdict = "accepted [100/100]"
else:
verdict = "partially accepted [" + verdict + "/100]"
lang = det[3].text
lang = lang[:lang.index('<')]
subformat = {
'probid': probid,
'subtime': subtime,
'verdict': verdict,
'lang': lang,
'link': link,
}
recentlist.append(subformat)
return recentlist
def profilePageScraper(user_handle):
query_user_profile_url = f"https://www.codechef.com/users/{user_handle}"
r = requests.get(query_user_profile_url)
soup = BeautifulSoup(r.text, 'html.parser')
return soup