-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexportToJson.py
More file actions
104 lines (92 loc) · 2.77 KB
/
Copy pathexportToJson.py
File metadata and controls
104 lines (92 loc) · 2.77 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import requests
import json
from bs4 import BeautifulSoup # To clean HTML content
# LeetCode GraphQL API endpoint
url = "https://leetcode.com/graphql"
# GraphQL query to fetch problem details
query = """
query getQuestionDetail($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionId
title
content
difficulty
likes
dislikes
topicTags {
name
slug
}
}
}
"""
# List of problem slugs (replace with your own list)
# Path to your JSON file
file_path = "leetcode_problem_slugs.json"
# Read the JSON file and store it as a variable
with open(file_path, "r", encoding="utf-8") as file:
data = json.load(file)
problem_slugs = data
'''
[
"two-sum",
"add-two-numbers",
"longest-substring-without-repeating-characters",
"median-of-two-sorted-arrays",
"longest-palindromic-substring",
"zigzag-conversion",
"binary-tree-upside-down"
]
'''
# HTTP headers
headers = {
"Content-Type": "application/json",
"User-Agent": "Mozilla/5.0"
}
# Function to fetch problem details
def fetch_problem_details(slug):
response = requests.post(url, json={"query": query, "variables": {"titleSlug": slug}}, headers=headers)
if response.status_code == 200:
data = response.json()
return data.get("data", {}).get("question", {})
else:
print(f"Failed to fetch problem: {slug} (Status Code: {response.status_code})")
return None
# Function to clean HTML content
def clean_html(html_content):
if html_content:
soup = BeautifulSoup(html_content, "html.parser")
return soup.get_text()
return ""
# Fetch details for each problem in the list
problems = []
q = 1
for slug in problem_slugs:
problem_details = fetch_problem_details(slug)
if problem_details:
# Append accessible problem data
print(f"slug ({q}/{len(problem_slugs)}) {slug}")
problems.append({
"id": problem_details.get("questionId"),
"title": problem_details.get("title"),
"difficulty": problem_details.get("difficulty"),
"likes": problem_details.get("likes"),
"dislikes": problem_details.get("dislikes"),
"tags": [tag["name"] for tag in problem_details.get("topicTags", [])],
"description": clean_html(problem_details.get("content")),
"accessible": True,
"slug": slug
})
else:
# Append inaccessible problem data
problems.append({
"slug": slug,
"description": "",
"accessible": False
})
q = q + 1
# Save the collected data to a JSON file
output_file = "leetcode_problems_cleaned.json"
with open(output_file, "w", encoding="utf-8") as f:
json.dump(problems, f, indent=4)
print(f"Problem details saved to {output_file}")