-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleetcodeContest.py
87 lines (70 loc) · 2.98 KB
/
leetcodeContest.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
import requests
from datetime import datetime, timedelta, timezone
def leetcode_contestHistory(username):
query = """
query userContestRankingHistory($username: String!) {
userContestRankingHistory(username: $username) {
attended
rating
ranking
trendDirection
problemsSolved
totalProblems
finishTimeInSeconds
contest {
title
startTime
}
}
}
"""
url = 'https://leetcode.com/graphql'
variables = {
"username": username
}
rankingHistory = []
response = requests.post(
url, json={'query': query, 'variables': variables})
if response.status_code == 200:
data = response.json()
user_contest_ranking_history = data['data']['userContestRankingHistory']
if user_contest_ranking_history is None:
print("No contest history found for the user")
return rankingHistory
prevRating = 1500
for history in user_contest_ranking_history:
if history['attended']:
contestinfo = {}
contestinfo['user'] = username
contestinfo['rating_change'] = int(
(history['rating'])-prevRating)
prevRating = int(history['rating'])
contestinfo['final_rating'] = int(history['rating'])
contestinfo['number_of_problems_solved'] = history['problemsSolved']
contestinfo['rank'] = history['ranking']
time = history['contest']['startTime']
timestamp = int(time)
datetime_obj = datetime.fromtimestamp(timestamp)
# Convert the datetime object to the desired format
formatted_time = datetime_obj.strftime("%Y-%m-%dT%H:%M:%S%z")
# Adjust the timezone offset
formatted_time_with_timezone = datetime_obj.astimezone(
timezone(timedelta(hours=5, minutes=30))).strftime("%Y-%m-%dT%H:%M:%S%z")
contestinfo['contest'] = {
'title': history['contest']['title'], 'start_time': formatted_time_with_timezone, 'platform': 'Leetcode', 'url': f"https://leetcode.com/contest/{(history['contest']['title']).replace(' ', '-').lower()}", 'duration': '', 'total_questions': 4}
rankingHistory.append(contestinfo)
else:
# Print an error message if the request was unsuccessful
print("Error fetching data. Status code:", response.status_code)
return rankingHistory
# usernameV = 'coder_s_176' # valid username
# checking history for valid username
# contestHistory = leetcode_contestHistory(usernameV)
# print(contestHistory)
# usernameInv = 'shivamBedar' # invalid username
# # checking history for invalid username
# contestHistory = leetcode_contestHistory(usernameInv)
# print("Contest History:")
# for contest in contestHistory:
# print(contest)
# print("\n")