-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
89 lines (77 loc) · 2.7 KB
/
main.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
from fastapi import FastAPI, HTTPException
import requests
app = FastAPI()
LEETCODE_GRAPHQL_URL = "https://leetcode.com/graphql"
def get_user_details(username: str):
query = {
"query": """
query userProfile($username: String!) {
matchedUser(username: $username) {
username
profile {
realName
aboutMe
school
websites
countryName
company
ranking
}
submitStats {
acSubmissionNum {
difficulty
count
submissions
}
}
}
}
""",
"variables": {"username": username}
}
response = requests.post(LEETCODE_GRAPHQL_URL, json=query)
if response.status_code != 200:
raise HTTPException(status_code=response.status_code, detail="Failed to fetch data from LeetCode")
data = response.json()
if "errors" in data:
raise HTTPException(status_code=404, detail="User not found or access denied")
return data["data"]["matchedUser"]
@app.get("/leetcode/{username}")
def read_user(username: str):
try:
user_details = get_user_details(username)
return user_details
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/leetcode/{username}/rank")
def userGloabalRank(username: str):
try:
user_details = get_user_details(username)
ranking = user_details["profile"]["ranking"]
return {"username":username,"globalRanking":ranking}
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/leetcode/{username}/totalsolved")
def userTotalSolved(username: str):
try:
userDetails = get_user_details(username)
dic = {}
res = []
for i in userDetails["submitStats"]["acSubmissionNum"]:
if i["difficulty"] == "All":
dic.update({"username":username,f"total questions solved by {username} is":i["count"]})
elif i["difficulty"] != "All":
res.append({"difficulty":i["difficulty"],"solved":i["count"],"submissions":i["submissions"]})
dic.update({"categories":res})
return dic
except HTTPException as e:
raise e
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)