-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_leetcode.py
More file actions
340 lines (291 loc) · 12.7 KB
/
sync_leetcode.py
File metadata and controls
340 lines (291 loc) · 12.7 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import os
import json
import requests
from github import Github
from datetime import datetime, timezone
import logging
from typing import Dict, List, Optional
import time
from functools import wraps
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
class LeetCodeGitHubSync:
"""Class to handle synchronization of LeetCode solutions to GitHub."""
# Language to file extension mapping
EXTENSIONS = {
'python': 'py', 'python3': 'py', 'java': 'java',
'c': 'c', 'cpp': 'cpp', 'c++': 'cpp', 'javascript': 'js',
'typescript': 'ts', 'golang': 'go', 'ruby': 'rb',
'swift': 'swift', 'kotlin': 'kt', 'rust': 'rs',
'scala': 'scala', 'php': 'php'
}
# API endpoints
LEETCODE_GRAPHQL_URL = "https://leetcode.com/graphql"
LEETCODE_SUBMISSIONS_URL = "https://leetcode.com/api/submissions/"
CACHE_FILE = "solutions_cache.json"
def __init__(self, github_token: str, github_repo: str, leetcode_session: str):
"""Initialize with required credentials."""
if not all([github_token, github_repo, leetcode_session]):
raise ValueError("Missing required credentials")
self.github = Github(github_token)
self.repo = self.github.get_repo(github_repo)
self.headers = {
'Cookie': f'LEETCODE_SESSION={leetcode_session}',
'User-Agent': 'Mozilla/5.0',
'Referer': 'https://leetcode.com'
}
self.solutions_cache = self.load_cache()
def load_cache(self) -> Dict:
"""Load the solutions cache from the repository."""
try:
contents = self.repo.get_contents(self.CACHE_FILE)
cache_content = contents.decoded_content.decode('utf-8')
logger.info("Loaded existing cache file")
return json.loads(cache_content)
except:
logger.info("No cache file found, creating new cache")
return {}
def save_cache(self):
"""Save the solutions cache to the repository."""
try:
cache_content = json.dumps(self.solutions_cache, indent=2, sort_keys=True)
try:
contents = self.repo.get_contents(self.CACHE_FILE)
current_content = contents.decoded_content.decode('utf-8')
if current_content.strip() != cache_content.strip():
self.repo.update_file(
self.CACHE_FILE,
"chore: Update solutions cache",
cache_content,
contents.sha
)
logger.info("Cache file updated")
except:
self.repo.create_file(
self.CACHE_FILE,
"chore: Create solutions cache",
cache_content
)
logger.info("Cache file created")
except Exception as e:
logger.error(f"Error saving cache: {str(e)}")
raise
@staticmethod
def retry_with_backoff(retries=3, backoff_in_seconds=1):
"""Decorator for implementing retry logic with exponential backoff."""
def decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
for i in range(retries):
try:
return func(*args, **kwargs)
except Exception as e:
if i == retries - 1: # Last attempt
raise
wait_time = (backoff_in_seconds * 2 ** i)
logger.warning(f"Attempt {i + 1} failed: {str(e)}. Retrying in {wait_time} seconds...")
time.sleep(wait_time)
return wrapper
return decorator
@retry_with_backoff()
def get_problem_details(self, title_slug: str) -> Dict:
"""Fetch problem details from LeetCode."""
query = """
query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionId
title
content
difficulty
topicTags {
name
}
}
}
"""
try:
response = requests.post(
self.LEETCODE_GRAPHQL_URL,
json={'query': query, 'variables': {'titleSlug': title_slug}},
headers=self.headers
)
response.raise_for_status()
data = response.json()
return data['data']['question']
except Exception as e:
logger.error(f"Error fetching problem details for {title_slug}: {str(e)}")
raise
@retry_with_backoff()
def get_submissions(self) -> List[Dict]:
"""Fetch recent accepted submissions."""
try:
url = f"{self.LEETCODE_SUBMISSIONS_URL}?offset=0&limit=20"
response = requests.get(url, headers=self.headers)
response.raise_for_status()
# Get submissions from response
submissions_data = response.json()
submissions = submissions_data.get('submissions_dump', [])
# Filter for accepted submissions
accepted_submissions = [s for s in submissions if s['status_display'] == 'Accepted']
# Deduplicate by problem + language
unique_submissions = {}
for submission in accepted_submissions:
key = (
submission['title_slug'],
submission['lang'].lower()
)
# Keep newest submission
if key not in unique_submissions:
unique_submissions[key] = submission
deduplicated_submissions = list(unique_submissions.values())
logger.info(
f"Fetched:{len(submissions)} submissions|"
f"Accepted:{len(accepted_submissions)}|"
f"Unique:{len(deduplicated_submissions)}"
)
return deduplicated_submissions
except Exception as e:
logger.error(f"Error fetching submissions: {str(e)}")
raise
#Network error logging
except requests.exceptions.RequestException as e:
logger.error(f"Network/API error fetching submissions: {str(e)}")
raise
def get_file_extension(self, lang: str) -> str:
"""Get file extension for a given programming language."""
return self.EXTENSIONS.get(lang.lower(), 'txt')
def create_problem_readme(self, problem_data: Dict) -> str:
"""Create README.md content for a new problem."""
return f"""# {problem_data['questionId']}. {problem_data['title']}
## Difficulty: {problem_data['difficulty']}
## Topics: {', '.join(tag['name'] for tag in problem_data['topicTags'])}
## Problem
{problem_data['content']}
[View on LeetCode](https://leetcode.com/problems/{problem_data['title'].lower().replace(' ', '-')})
"""
def process_submission(self, submission: Dict) -> bool:
"""
Process a single submission.
Returns True if changes were made, False otherwise.
"""
try:
problem_id = submission['title_slug']
lang = submission['lang'].lower()
cache_key = f"{problem_id}_{lang}"
# Check if solution exists and has changed
if cache_key in self.solutions_cache:
if self.solutions_cache[cache_key].strip() == submission['code'].strip():
logger.debug(f"Solution unchanged for {problem_id} in {lang}")
return False
# Get problem details
problem_data = self.get_problem_details(submission['title_slug'])
# Create folder structure
difficulty = problem_data['difficulty'].lower()
folder_name = f"{int(problem_data['questionId']):04d}-{submission['title_slug']}"
base_path = f"{difficulty}/{folder_name}"
# Create README for new problems
readme_path = f"{base_path}/README.md"
try:
self.repo.get_contents(readme_path)
except:
logger.info(f"Creating README for {problem_id}")
readme_content = self.create_problem_readme(problem_data)
self.repo.create_file(
readme_path,
f"docs: Add README for {problem_data['title']}",
readme_content
)
# Update solution file
extension = self.get_file_extension(lang)
file_path = f"{base_path}/solution.{extension}"
try:
contents = self.repo.get_contents(file_path)
current_content = contents.decoded_content.decode('utf-8').strip()
if current_content != submission['code'].strip():
self.repo.update_file(
file_path,
f"feat: Update {lang} solution for {problem_data['title']}",
submission['code'],
contents.sha
)
logger.info(f"Updated solution for {problem_id} in {lang}")
else:
logger.debug(f"Solution content unchanged for {problem_id} in {lang}")
return False
except:
self.repo.create_file(
file_path,
f"feat: Add {lang} solution for {problem_data['title']}",
submission['code']
)
logger.info(f"Created new solution for {problem_id} in {lang}")
# Update cache
self.solutions_cache[cache_key] = submission['code'].strip()
return True
except Exception as e:
logger.error(f"Error processing submission {submission['title_slug']}: {str(e)}")
raise
def sync_solutions(self):
"""Main sync function."""
logger.info("Starting LeetCode solutions sync...")
try:
submissions = self.get_submissions()
changes_made = False
for submission in submissions:
if self.process_submission(submission):
changes_made = True
# Only save cache if changes were made
if changes_made:
self.save_cache()
logger.info("Changes detected and saved to cache")
else:
logger.info("No changes detected")
logger.info("Sync completed successfully!")
except Exception as e:
logger.error(f"Sync failed: {str(e)}")
raise
def main():
"""Main entry point."""
try:
# Get environment variables
github_token = os.getenv('GH_PAT')
github_repo = os.getenv('GITHUB_REPO')
leetcode_session = os.getenv('LEETCODE_SESSION')
# Validate environment variables
if not all([github_token, github_repo, leetcode_session]):
raise ValueError("Missing required environment variables")
logger.info(f"Initializing sync for repository: {github_repo}")
# Initialize syncer
syncer = LeetCodeGitHubSync(
github_token=github_token,
github_repo=github_repo,
leetcode_session=leetcode_session
)
# Test LeetCode connection
logger.info("Testing LeetCode connection...")
test_response = requests.get(
"https://leetcode.com/api/problems/all/",
headers=syncer.headers
)
test_response.raise_for_status()
logger.info("LeetCode connection successful")
# Run sync
syncer.sync_solutions()
except requests.exceptions.RequestException as e:
logger.error(f"API request failed: {str(e)}")
raise
except ValueError as e:
logger.error(f"Configuration error: {str(e)}")
raise
except Exception as e:
logger.error(f"Sync failed: {str(e)}")
raise
if __name__ == "__main__":
main()