-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
157 lines (122 loc) · 5.25 KB
/
server.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
from bs4 import BeautifulSoup
import requests
from flask import *
import json
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
@app.route("/", methods=["GET"]) # creating an endpoint for GET request
def home_page():
return "<html><body style='margin: 1rem; padding: 1rem; font-family: monospace; font-size: 1.2rem; height: 100vh; background-color: #cdf4df; display: flex; flex-direction: column; justify-content: center; text-align: center; align-items: center'><h1 style='color: green'>Hello, Welcome to LEETCODE SCRAPPER's HOMEPAGE</h1> <h2>To get someone's LeetCode Account Details send GET request to the below URL <p style='color: blue'>https://subrata-rudra-leetcode-scrapper.onrender.com/user/?user=LEETCODE_USERNAME_OF_USER</p></h2></body></html>"
@app.route("/user/", methods=["GET"])
def request_data():
username = str(request.args.get("user"))
base_url = "https://leetcode.com/"
final_url = base_url + username
html_text = requests.get(final_url).text
soup = BeautifulSoup(html_text, "lxml")
username = soup.find(
"div", class_="text-label-3 dark:text-dark-label-3 text-xs"
).text
candidate_name = soup.find(
"div",
class_="text-label-1 dark:text-dark-label-1 break-all text-base font-semibold",
).text
candidate_rank = soup.find(
"span", class_="ttext-label-1 dark:text-dark-label-1 font-medium"
).text
contest_attended = soup.find_all(
"div", class_="text-label-1 dark:text-dark-label-1 font-medium leading-[22px]"
)[1].text
contest_rating = soup.find(
"div", class_="text-label-1 dark:text-dark-label-1 flex items-center text-2xl"
).text
contest_global_ranking = soup.find(
"div", class_="text-label-1 dark:text-dark-label-1 font-medium leading-[22px]"
).text
total_problem_solved = soup.find(
"div", class_="text-[24px] font-medium text-label-1 dark:text-dark-label-1"
).text
problems_solved = soup.find_all(
"span",
class_="mr-[5px] text-base font-medium leading-[20px] text-label-1 dark:text-dark-label-1",
)
# total_submissions = soup.find(
# "span", class_="mr-[5px] text-base font-medium lc-md:text-xl"
# ).text
language_used = soup.find_all(
"span",
class_="inline-flex items-center px-2 whitespace-nowrap text-xs leading-6 rounded-full text-label-3 dark:text-dark-label-3 bg-fill-3 dark:bg-dark-fill-3 notranslate",
)
# total_active_days = soup.find_all(
# "span", class_="font-medium text-label-2 dark:text-dark-label-2"
# )[3].text
# max_streak = soup.find_all(
# "span", class_="font-medium text-label-2 dark:text-dark-label-2"
# )[4].text
solved_problem = soup.find_all(
"span", class_="text-label-1 dark:text-dark-label-1 font-medium line-clamp-1"
)
topics_covered = soup.find_all(
"span",
class_="inline-flex items-center px-2 whitespace-nowrap text-xs leading-6 rounded-full bg-fill-3 dark:bg-dark-fill-3 cursor-pointer transition-all hover:bg-fill-2 dark:hover:bg-dark-fill-2 text-label-2 dark:text-dark-label-2",
)
badges_earned = soup.find_all(
"img", class_="h-full w-full cursor-pointer object-contain"
)
most_recent_badge = soup.find(
"div", class_="text-label-1 dark:text-dark-label-1 text-base"
).text
# last_solved = soup.find(
# "span",
# class_="text-label-3 dark:text-dark-label-3 hidden whitespace-nowrap lc-md:inline",
# ).text
language_used_list = []
ind = 0
for language in language_used:
language_used_list.insert(ind, language.text)
ind = ind + 1
solved_problem_list = []
ind = 0
for problem in solved_problem:
solved_problem_list.insert(ind, problem.text)
ind = ind + 1
topics_covered_list = []
ind = 0
for topic in topics_covered:
topics_covered_list.insert(ind, topic.text)
ind = ind + 1
badges_earned_list = []
ind = 0
for badge in badges_earned:
badges_earned_list.insert(ind, badge["alt"])
ind = ind + 1
solved_problem_json = json.dumps(solved_problem_list)
topics_covered_json = json.dumps(topics_covered_list)
badges_earned_json = json.dumps(badges_earned_list)
language_used_json = json.dumps(language_used_list)
data_set = {
"LeetCodeUsername": username,
"CandidateName": candidate_name,
"CandidateRank": candidate_rank,
"ContestAttended": contest_attended,
"ContestRating": contest_rating,
"ContestGlobalRanking": contest_global_ranking,
"TotalProblemsSolved": total_problem_solved,
"EasyProblem": problems_solved[0].text,
"MediumProblem": problems_solved[1].text,
"HardProblem": problems_solved[2].text,
# "TotalSubmissions": total_submissions,
# "TotalActiveDays": total_active_days,
# "MaxStreak": max_streak,
"MostRecentlyEarnedBadge": most_recent_badge,
"Last15SolvedProblems": solved_problem_json,
"TopicsCovered": topics_covered_json,
"BadgesEarned": badges_earned_json,
"LanguageUsed": language_used_json,
# "LastSolved": last_solved,
}
json_dump = json.dumps(data_set)
return json_dump
if __name__ == "__main__":
app.run()