Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 62fb7ee

Browse files
committed
first touch
0 parents  commit 62fb7ee

8 files changed

+82207
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea
2+
*.iml
3+
out
4+
gen
5+
config.toml
6+
__pycache__/
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def getConcatenation(self, nums: List[int]) -> List[int]:
3+
return nums + nums
4+

color.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Colors:
2+
GREEN = "\033[1;32m" # Bold green
3+
ORANGE = "\033[1;33m" # Bold orange
4+
RED = "\033[1;31m" # Bold red
5+
RESET = "\033[0m"

config_setup.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import toml
2+
import os
3+
4+
CONFIG_FILE_PATH = "config.toml"
5+
6+
7+
def save_credentials_to_config(leetcode_session, csrf_token):
8+
config_data = {"LEETCODE_SESSION": leetcode_session, "CSRF_TOKEN": csrf_token}
9+
with open(CONFIG_FILE_PATH, "w") as config_file:
10+
toml.dump(config_data, config_file)
11+
12+
13+
def load_credentials_from_config():
14+
if os.path.exists(CONFIG_FILE_PATH):
15+
with open(CONFIG_FILE_PATH, "r") as config_file:
16+
config_data = toml.load(config_file)
17+
return config_data.get("LEETCODE_SESSION"), config_data.get("CSRF_TOKEN")
18+
return None, None
19+
20+
21+
def create_python_code_edit_file(question_id, title_slug, python3_code):
22+
if python3_code is None:
23+
print(f"No Python3 code snippet found for question '{question_id}'.")
24+
return
25+
26+
file_name = f"code_editor/{question_id}_{title_slug}.py"
27+
with open(file_name, "w") as file:
28+
file.write(python3_code.code)

function.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
import json
2+
import leetcode
3+
from bs4 import BeautifulSoup
4+
5+
from color import Colors
6+
7+
8+
# configuration
9+
def configure_api_instance(csrf_token, leetcode_session):
10+
configuration = leetcode.Configuration()
11+
configuration.api_key["x-csrftoken"] = csrf_token
12+
configuration.api_key["csrftoken"] = csrf_token
13+
configuration.api_key["LEETCODE_SESSION"] = leetcode_session
14+
configuration.api_key["Referer"] = "https://leetcode.com"
15+
configuration.debug = False
16+
17+
api_instance = leetcode.DefaultApi(leetcode.ApiClient(configuration))
18+
return api_instance
19+
20+
21+
def process_api_response(api_response):
22+
if api_response.data:
23+
pass
24+
else:
25+
print("No data found in the API response.")
26+
27+
28+
def read_json_file(file_path):
29+
with open(file_path, "r") as json_file:
30+
return json.load(json_file)
31+
32+
33+
# for get question data by question id
34+
def get_question_data_by_id(json_data, q):
35+
questions = (
36+
json_data.get("data", {}).get("problemsetQuestionList", {}).get("questions", [])
37+
)
38+
39+
for question in questions:
40+
if q == question.get("frontendQuestionId"):
41+
return question
42+
return None
43+
44+
45+
# for graphql excution
46+
def execute_graphql_query(api_instance, graphql_query, query_variables):
47+
graphql_request = leetcode.GraphqlQuery(query=graphql_query, variables=query_variables)
48+
api_response = api_instance.graphql_post(body=graphql_request)
49+
return api_response
50+
51+
52+
# this is for print the question list in order
53+
def print_question_data(question):
54+
question_id = question.get("frontendQuestionId")
55+
title = question.get("title")
56+
difficulty = question.get("difficulty")
57+
ac_rate = question.get("acRate")
58+
59+
# Fix ac_rate position regardless of the length of difficulty
60+
difficulty_color = ""
61+
if difficulty == "Easy":
62+
difficulty_color = Colors.GREEN
63+
elif difficulty == "Medium":
64+
difficulty_color = Colors.ORANGE
65+
elif difficulty == "Hard":
66+
difficulty_color = Colors.RED
67+
68+
# Set fixed widths for the title and difficulty columns
69+
title_width = 50
70+
difficulty_width = 10
71+
72+
# Align and pad the title and difficulty columns
73+
title_formatted = title.ljust(title_width)[:title_width]
74+
difficulty_formatted = (
75+
f"{difficulty_color}{difficulty.ljust(difficulty_width)}{Colors.RESET}"
76+
)
77+
78+
print(
79+
f"[{str(question_id).rjust(3)}] {title_formatted} {difficulty_formatted} ({ac_rate:.2f}%)"
80+
)
81+
82+
83+
# for --solve
84+
def get_question_content(api_instance, title_slug):
85+
graphql_query = "query questionContent($titleSlug: String!) { question(titleSlug: $titleSlug) { content } }"
86+
query_variables = {"titleSlug": title_slug}
87+
api_response = execute_graphql_query(api_instance, graphql_query, query_variables)
88+
content = api_response.data.question.content
89+
return content
90+
91+
92+
def html_to_text(html_content):
93+
soup = BeautifulSoup(html_content, 'html.parser')
94+
plain_text = soup.get_text()
95+
return plain_text
96+
97+
98+
def fetch_code_content(api_instance, title_slug):
99+
graphql_query = """
100+
query questionEditorData($titleSlug: String!) {
101+
question(titleSlug: $titleSlug) {
102+
questionId
103+
questionFrontendId
104+
codeSnippets {
105+
lang
106+
langSlug
107+
code
108+
}
109+
}
110+
}
111+
"""
112+
113+
query_variables = {
114+
"titleSlug": title_slug
115+
}
116+
117+
api_response = execute_graphql_query(api_instance, graphql_query, query_variables)
118+
119+
# if api_response and api_response.data and api_response.data.question:
120+
# question_data = api_response.data.question
121+
#
122+
# if question_data.code_snippets:
123+
# code_snippets = question_data.code_snippets
124+
# python3_snippet = next(
125+
# (snippet for snippet in code_snippets if snippet.lang_slug == "python3"),
126+
# None
127+
# )
128+
#
129+
# if python3_snippet:
130+
# return python3_snippet
131+
132+
python3_snippet = next(
133+
(snippet for snippet in api_response.data.question.code_snippets if snippet.lang_slug == "python3"),
134+
None
135+
)
136+
if python3_snippet:
137+
return python3_snippet
138+
139+
return None

0 commit comments

Comments
 (0)