-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLCMostActiveTime.py
53 lines (47 loc) · 1.83 KB
/
LCMostActiveTime.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
import requests
import datetime
import json
# Replace these with your actual LeetCode username and session cookie
username = "shivamanandbansal"
# GraphQL query to fetch user's most active day and month
query = """
query getUserProfileCalendar($username: String!) {
matchedUser(username: $username) {
userCalendar(year: 2023) {
submissionCalendar
}
}
}
"""
# URL for the LeetCode GraphQL API endpoint
url = "https://leetcode.com/graphql"
# Define the variables for the GraphQL query
variables = {'username': username}
# Make the POST request to the GraphQL API
response = requests.post(url, json={'query': query, 'variables': variables})
# Check if the request was successful
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Extract the submission calendar
submission_calendar_str = data['data']['matchedUser']['userCalendar']['submissionCalendar']
# Parse the submission calendar string to a dictionary
submission_calendar = json.loads(submission_calendar_str)
# Convert the submission calendar to a list of (timestamp, submission count) tuples
submission_list = [(int(timestamp), count) for timestamp, count in submission_calendar.items()]
# Find the timestamp corresponding to the maximum submission count
max_timestamp = None
max_count = 0
for timestamp, count in submission_list:
if count > max_count:
max_count = count
max_timestamp = timestamp
if max_timestamp is not None:
max_date = datetime.datetime.fromtimestamp(max_timestamp)
# Print the result
print("Timestamp with maximum submission count:", max_date)
print("Maximum submission count:", max_count)
else:
print("No data found in submission list.")
else:
print("Failed to fetch data. Status code:", response.status_code)