fspv / leetcode-anki

Anki cards generator for Leetcode
MIT License
308 stars 43 forks source link

Add frequency #3

Closed gh4n closed 3 years ago

gh4n commented 3 years ago

Hey @prius would it be possible to get the frequency of a problem as a tag in anki? This would make it a lot easier to prioritise canonical problems.

Thanks I really love this tool!

fspv commented 3 years ago

Hello @gh4n

Unfortunately looks like it is not possible. Leetcode API returns 0.0 for all the questions.

Check this one for example https://leetcode.com/problems/perfect-squares/

It returns zero frequency:

{'difficulty': {'level': 2},                                                                                                                                                                                                                 
 'frequency': 0.0,                                                                                                                                                                                                                           
 'is_favor': False,                                                                                                                                                                                                                          
 'paid_only': False,                                                                                                                                                                                                                         
 'progress': 0.0,                                                                                                                                                                                                                            
 'stat': {'frontend_question_id': 279,                                                                                                                                                                                                       
          'is_new_question': False,                                                                                                                                                                                                          
          'question__article__has_video_solution': 'False',                                                                                                                                                                                  
          'question__article__live': 'True',                                                                                                                                                                                                 
          'question__article__slug': 'perfect-squares',                                                                                                                                                                                      
          'question__hide': False,                                                                                                                                                                                                           
          'question__title': 'Perfect Squares',                                                                                                                                                                                              
          'question__title_slug': 'perfect-squares',                                                                                                                                                                                         
          'question_id': 279,                                                                                                                                                                                                                
          'total_acs': 443520,                                                                                                                                                                                                               
          'total_submitted': 878622},                                                                                                                                                                                                        
 'status': None}   

Even though the real frequency is 35%

Selection_324

I'll check if it is possible to fetch this infromation via graphql API.

fspv commented 3 years ago

Graphql returns zero as well.

send: b'{"query": "\\n            query getQuestionDetail($titleSlug: String!) {\\n              question(titleSlug: $titleSlug) {\\n                frequency\\n                topicTags {\\n                  name\\n                  slug\\n                }\\n              }\\n            }\\n        ", "variables": {"titleSlug": "two-sum"}, "operationName": "getQuestionDetail"}'
reply: 'HTTP/1.1 200 OK\r\n'
<...>
2021-10-14 15:47:25,827 DEBUG https://leetcode.com:443 "POST /graphql HTTP/1.1" 200 127
2021-10-14 15:47:25,828 DEBUG response body: {"data":{"question":{"frequency":0.0,"topicTags":[{"name":"Array","slug":"array"},{"name":"Hash Table","slug":"hash-table"}]}}}

Looks like this information is not available publicitly. Let me know if you find other ways to acquire it. The code change to add it to the card is simple once I have the information. Here is a PR for that https://github.com/prius/leetcode-anki/pull/4 (so I can reuse it later in case it is needed)

fspv commented 3 years ago

Found out that GraphQL request problemsetQuestionList returns correct frequency. Will try again later.

gh4n commented 3 years ago

Thanks if I get a chance I’ll have a look too!

fspv commented 3 years ago

One more observation. The frequency field is null when you remove csrf cookie from the request. Need to check if the propagation of the cookie works correctly within the leetcode client.

fspv commented 3 years ago

Okay. It's confirmed. LC client with expired csrf cookies returns null frequency. Running it with up to date cookie returns an actual value.

fspv commented 3 years ago

Frequency added https://github.com/prius/leetcode-anki/pull/4

The sorting by this field doesn't work though. I've created a separate issue for that https://github.com/prius/leetcode-anki/issues/10