Database and Queries

Leetcode provides a GraphQL API and a listing of all their problems

The GraphQL routes use a $titleSlug for querying problems, and a $questionId for the synced code (separate from the num aka questionFrontendId).

Instead of searching that list every time problem information is needed, a local database is created from update_problem_listing() that stores a lookup table indexed to the problem number.

That is then used to quickly find the $titleSlug and $questionId, which can be inserted into the following queries

Problem Info:

query questionCustom($titleSlug: String!) {
    question(titleSlug: $titleSlug) {
        questionId
        questionFrontendId
        title
        titleSlug
        difficulty
        content
        codeSnippets {
            lang
            langSlug
            code
        }
    }
}
Synced Code:

query SyncedCode($questionId: Int!, $lang: Int!) {
  syncedCode(questionId: $questionId, lang: $lang) {
    code
  }
}

The language ID can be found with this query:

query languageList {
  languageList {
    id
    name
  }
}

Python 2 is 2, and Python 3 is 11. This package currently defaults to Python 3.

Deleting database

The database is located as a .db file in the src

package_location=$(pip show rossmassey.fetch-leetcode-problem | grep Location)
package_path=$(echo $package_location | cut -d ' ' -f2)
rm -f $package_path/fetch_leetcode_problem/problems.db