11from gql import gql , Client
22from gql .transport .requests import RequestsHTTPTransport
33
4- transport = RequestsHTTPTransport (
5- url = 'https://leetcode.com/graphql' ,
6- headers = {'Content-Type' : 'application/json' },
7- )
8-
9- client = Client (
10- transport = transport ,
11- fetch_schema_from_transport = False
12- )
13-
14- def fetch_problem_list (username : str = "yuvrajsinh5252" ):
15- variables = {"username" : username }
16- query = gql (
17- """
18- query userProblemsSolved($username: String!) {
19- allQuestionsCount {
20- difficulty
21- count
22- }
23- matchedUser(username: $username) {
24- problemsSolvedBeatsStats {
25- difficulty
26- percentage
27- }
28- submitStatsGlobal {
29- acSubmissionNum {
30- difficulty
31- count
32- }
33- }
34- }
35- }
36- """
37- )
38-
39- try :
40- result = client .execute (query , variable_values = variables )
41- return result
42- except Exception as e :
43- print (f"Error fetching data: { str (e )} " )
44- return None
4+ def create_leetcode_client (csrf_token : str , session_id : str ):
5+ headers = {
6+ 'Content-Type' : 'application/json' ,
7+ 'x-csrftoken' : csrf_token ,
8+ 'cookie' : f'csrftoken={ csrf_token } ; LEETCODE_SESSION={ session_id } ' ,
9+ 'referer' : 'https://leetcode.com' ,
10+ }
11+
12+ transport = RequestsHTTPTransport (
13+ url = 'https://leetcode.com/graphql' ,
14+ headers = headers ,
15+ )
16+
17+ return Client (
18+ transport = transport ,
19+ fetch_schema_from_transport = False
20+ )
21+
22+ def fetch_user_data (username : str = "yuvrajsinh5252" ):
23+ client = create_leetcode_client ("csrf_token" , "session_id" )
24+ variables = {"username" : username }
25+ query = gql (
26+ """
27+ query userProblemsSolved($username: String!) {
28+ allQuestionsCount {
29+ difficulty
30+ count
31+ }
32+ matchedUser(username: $username) {
33+ problemsSolvedBeatsStats {
34+ difficulty
35+ percentage
36+ }
37+ submitStatsGlobal {
38+ acSubmissionNum {
39+ difficulty
40+ count
41+ }
42+ }
43+ }
44+ }
45+ """
46+ )
47+
48+ try :
49+ result = client .execute (query , variable_values = variables )
50+ return result
51+ except Exception as e :
52+ print (f"Error fetching data: { str (e )} " )
53+ return None
54+
55+ def fetch_problem_list (
56+ csrf_token : str ,
57+ session_id : str ,
58+ categorySlug : str ,
59+ limit : int = 20 ,
60+ skip : int = 0 ,
61+ filters : dict = {}
62+ ):
63+ client = create_leetcode_client (csrf_token , session_id )
64+ variables = {
65+ "categorySlug" : categorySlug ,
66+ "limit" : limit ,
67+ "skip" : skip ,
68+ "filters" : filters
69+ }
70+
71+ query = gql (
72+ """
73+ query problemsetQuestionList($categorySlug: String, $limit: Int, $skip: Int, $filters: QuestionListFilterInput) {
74+ problemsetQuestionList: questionList(
75+ categorySlug: $categorySlug
76+ limit: $limit
77+ skip: $skip
78+ filters: $filters
79+ ) {
80+ total: totalNum
81+ questions: data {
82+ acRate
83+ difficulty
84+ freqBar
85+ frontendQuestionId: questionFrontendId
86+ isFavor
87+ paidOnly: isPaidOnly
88+ status
89+ title
90+ titleSlug
91+ topicTags {
92+ name
93+ id
94+ slug
95+ }
96+ hasSolution
97+ hasVideoSolution
98+ }
99+ }
100+ }
101+ """
102+ )
103+
104+ try :
105+ result = client .execute (query , variable_values = variables )
106+ return result
107+ except Exception as e :
108+ print (f"Error fetching data: { str (e )} " )
109+ return None
0 commit comments