-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScrapeCodechef.py
177 lines (137 loc) · 6.64 KB
/
ScrapeCodechef.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from selenium import webdriver
from bs4 import BeautifulSoup
import pandas as pd
import time
from selenium.webdriver.chrome.options import Options
import requests
#To store color code for different stars
coderDiv = ["Grey", "Green", "Blue", "Violet", "Yellow", "Orange", "Red"]
#Searches for information in the user's profile page
def FindProfile(username: str):
#For storing the final search result
description = ""
try:
# options = Options()
# options.headless = True
# browser= webdriver.Chrome(executable_path='C:\chromedriver_win32\chromedriver.exe', chrome_options=options)
# base_url = "http://www.codechef.com/users/"
# # username = "arun_adithya"
# final_url = base_url + username
# browser.get(final_url)
# soup = BeautifulSoup(browser.page_source,features="html.parser")
url = "https://www.codechef.com/users/" + username
website_html = requests.get(url).text
soup = BeautifulSoup(website_html, 'lxml')
ProfileDetails = soup.find('section', class_="user-details")
moreinfo = ProfileDetails.find_all('li')
for details in moreinfo:
infoTitle = details.label.text[:-1]
if infoTitle == "Teams List":
break
# if infoTitle == "Username":
# description += infoTitle + " : " + username + ','
# continue
infoValue = details.span.text.strip()
description += f"'{infoTitle}':'{infoValue}'"+','
CurrentRating = soup.find('div', class_="rating-number").text.strip()
CurrentRatingNumber = str(int(CurrentRating))
# currentStars = FindStars(CurrentRatingNumber)
# CurrentCoderType = coderDiv[currentStars - 1] + " coder"
# HeighestRating = soup.find(
# 'div', class_="rating-header text-center").small.text
# HeighestRating = HeighestRating.split(' ')[-1][:-1]
# HeighestRatingNumber = int(HeighestRating)
# HeighestStars = FindStars(HeighestRatingNumber)
# HeighestCoderType = coderDiv[HeighestStars - 1] + " coder"
description += "'Current'" + ":" + f"'{CurrentRatingNumber}',"
# description += f"Current : {CurrentCoderType} {currentStars}star {CurrentRatingNumber} | Heighest : {HeighestCoderType} {HeighestStars}star {HeighestRatingNumber}\n"
# GCRanks = soup.find('div', class_='rating-ranks').ul.find_all('li')
# GlobalRank = GCRanks[0].text.split('\n')
# # description += f"{GlobalRank[2].strip()} : {GlobalRank[1].strip()} | "
# CountryRank = GCRanks[1].text.split('\n')
# # description += f"{CountryRank[2].strip()} : {CountryRank[1].strip()}\n"
# RankStats = soup.find('div', class_='rank-stats')
# LastContestInfo = RankStats.find('div', class_='contest-name')
# # ContestName = LastContestInfo.a.text.strip()
# if ContestName == 'None':
# description += "Last Rated Contest : " + ContestName + '\n'
# else:
# ContestLink = LastContestInfo.a['href']
# LastRatingInfo = RankStats.find('div', class_='rating-container')
# RatingChanged = LastRatingInfo.find('span').text.strip()
# description += "Last Rated Contest : " + ContestName + '\n'
# description += "Rating Changed : " + RatingChanged + '\n'
# description += "Link : " + ContestLink + '\n'
SolvedProblemsTable = soup.find(
'section', {'class': 'rating-data-section problems-solved'})
FullySolved, PartiallySolved = SolvedProblemsTable.find_all('h5')
FullySolved = FullySolved.text.split('(')[-1][:-1]
PartiallySolved = PartiallySolved.text.split('(')[-1][:-1]
description += f"'Fully Solved' : '{FullySolved}','Partially Solved' : '{PartiallySolved}',"
ContestProblems = {"Practice": 0}
ProblemsSolved = SolvedProblemsTable.find_all('article')
MaxProblemSolved = -1
MaxProblemSolvedContest = ""
Contests = ProblemsSolved[0].find_all('p')
for contest in Contests:
contestName = contest.strong.text[:-1]
if(contestName == "Practice"):
ContestProblems["Practice"] += len(contest.span.find_all('a'))
continue
ContestProblems[contestName] = len(contest.span.find_all('a'))
if(ContestProblems[contestName] >= MaxProblemSolved):
MaxProblemSolved = ContestProblems[contestName]
# MaxProblemSolvedContest = contestName
#print(contestName + f" {ContestProblems[contestName]}")
if("Practice" in ContestProblems.keys()):
description += f"'Practice Problems' : '{ContestProblems['Practice']}',"
if((len(Contests) > 1) or (len(Contests) == 1 and ("Practice" in ContestProblems.keys()))):
description += f"'Most Problem Solved in Contests' : '{MaxProblemSolved}'"
# description += f"Contests Code : {MaxProblemSolvedContest}\n"
# description += f"Link : https://www.codechef.com/{MaxProblemSolvedContest}\n"
# flag = False
# try:
# Contests = ProblemsSolved[1].find_all('p')
# flag = True
# except:
# pass
# if(flag):
# for contest in Contests:
# contestName = contest.strong.text[:-1]
# ContestProblems[contestName] = 1
# ContestsParticipated = 0
# if("Practice" in ContestProblems.keys()):
# ContestsParticipated = len(ContestProblems.keys()) - 1
# else:
# ContestsParticipated = len(ContestProblems.keys())
# description += f"Contests Participated : {ContestsParticipated}\n"
# if("Practice" in ContestProblems.keys()):
# del ContestProblems["Practice"]
# description += "Contest List : " + \
# str(list(ContestProblems.keys())) + '\n'
except:
description = f"'Username': 'invalid username'"
description = '{' + description + '}'
s = description.replace("'", "\"")
return s
#writes the info of the user into the output file
# f = open('output.txt', 'a')
# f.write(description)
# f.close()
#For finding out the stars from rating of the user
def FindStars(rating: str):
rating = int(rating)
if(rating >= 2500):
return 7
if(rating < 2500 and rating >= 2200):
return 6
if(rating < 2200 and rating >= 2000):
return 5
if(rating < 2000 and rating >= 1800):
return 4
if(rating < 1800 and rating >= 1600):
return 3
if(rating < 1600 and rating >= 1400):
return 2
else:
return 1