Skip to content

Instantly share code, notes, and snippets.

@raghavrambo007
Last active August 29, 2023 16:19
Show Gist options
  • Save raghavrambo007/70b92ab3837cfa39aca78912b314497b to your computer and use it in GitHub Desktop.
Save raghavrambo007/70b92ab3837cfa39aca78912b314497b to your computer and use it in GitHub Desktop.
Python Script to download all the Leetcode editorial solution codes.
import requests
import json
import re
import time
import csv
CSRFTOKEN='--LEETCODE_CSRFTOKEN--' # also part of the leetcode cookies, can also paste the value from there.
COOKIE='--LEETCODE_COOKIES--'
url = "https://leetcode.com/graphql"
REGEX=r'https:\/\/leetcode\.com\/playground\/[A-Z0-9a-z]{6,12}\/shared'
PROBLEMS_CSV='Downloads/problem_data.csv'
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:107.0) Gecko/20100101 Firefox/107.0',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'utf-8',
'content-type': 'application/json',
'x-csrftoken': CSRFTOKEN,
'Origin': 'https://leetcode.com',
'Connection': 'keep-alive',
'Cookie': COOKIE,
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'TE': 'trailers'
}
payload1="{\"query\":\"\\n query officialSolution($titleSlug: String!) {\\n question(titleSlug: $titleSlug) {\\n solution {\\n content\\n }\\n }\\n}\\n \",\"variables\":{\"titleSlug\":\"$$$\"}}"
def getExt(lang):
if lang=='cpp':
return 'cpp'
elif lang=='python3' or lang=='python':
return 'py'
elif lang=='java':
return 'java'
def getName(base, i, lang):
return ''+base+'-'+lang+'-'+str(i)+'.'+getExt(lang);
with open(PROBLEMS_CSV, 'r') as file:
csv_reader = csv.reader(file)
# Convert the CSV reader object to a list of rows
rows = list(csv_reader)
# Access specific rows by index
for row in rows[1:]:
number = row[0]
problem_title = row[1]
titleSlug = row[2].split('/')[-2]
print('titleSlug')
print(titleSlug)
payload1Cp=payload1.replace('$$$', titleSlug)
try:
response1 = requests.request("POST", url, headers=headers, data=payload1Cp)
print("response1")
print(response1.text)
response1 = json.loads(response1.text)
urls = re.findall(REGEX, response1['data']['question']['solution']['content'])
# print("urls")
# print(urls)
payload2 = "{\"query\":\"query fetchPlayground {\\n allPlaygroundCodes(uuid: \\\"$$$\\\") {\\n code\\n langSlug\\n __typename\\n }\\n}\\n\",\"variables\":{}}"
# headers['Referer']='https://leetcode.com/playground/FcDpFu3c/shared'
# print(payload2)
# response = requests.request("POST", url, headers=headers, data=payload2)
# time.sleep(1)
# print("codes")
# print(response)
# print(response.text)
for i, u in enumerate(urls):
uuid=u.split('/')[-2]
# print("uuid")
# print(uuid)
payload=payload2.replace('$$$', uuid)
# print("payload")
# print(payload)
response = requests.request("POST", url, headers=headers, data=payload)
time.sleep(1)
# print("codes")
# print(response)
# print(response.text)
response=json.loads(response.text);
for code in response['data']['allPlaygroundCodes']:
# print("fileName")
# print(getName(i, code['langSlug']))
with open(getName(number.zfill(3) + ". " +problem_title, i, code['langSlug']), 'w') as f:
f.write(code['code']);
except Exception:
print('cant download: ' +problem_title)
# response = requests.request("POST", url, headers=headers, data=payload2)
# print(response.text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment