-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.py
More file actions
executable file
·85 lines (71 loc) · 1.99 KB
/
update.py
File metadata and controls
executable file
·85 lines (71 loc) · 1.99 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
#!/usr/bin/env python3
import json
import os
import re
import sys
import html2text
import requests
filename = sys.argv[1]
print(f'dealing {filename}...')
name = re.match(r'src/\d*_(.*)\.cpp', filename)[1]
name = name.replace('_', '-')
resp = requests.post(
'https://leetcode.com/graphql',
headers={
'pragma': 'no-cache',
'cache-control': 'no-cache',
},
json={
'operationName': 'questionData',
'variables': {
'titleSlug': name,
},
'query': """
query questionData($titleSlug: String!) {
question(titleSlug: $titleSlug) {
questionId
questionFrontendId
title
titleSlug
content
difficulty
topicTags {
name
slug
}
codeSnippets {
lang
langSlug
code
}
hints
sampleTestCase
}
}
"""
},
)
assert resp.status_code >= 200 and resp.status_code < 300
question = resp.json()['data']['question']
h2t = html2text.HTML2Text()
h2t.strong_mark = ''
h2t.emphasis_mark = ''
content = h2t.handle(question['content']).strip()
content = re.sub(r'(Example(?: \d)?):\s*\n', '\g<1>:\n\n', content)
template = f"""/*
{question['title']}
URL: {f"https://leetcode.com/problems/{question['titleSlug']}"}
Tags: {[t['slug'] for t in question['topicTags']]}
___
{content}
*/"""
file = open(filename, 'r+')
text = file.read()
text = re.sub(r'/\*(?:\n|.)*?\*/', template, text, count=1)
#text = re.sub(r'TEST_CASE\(.*\)', f"TEST_CASE(\"{question['title']}\")", text)
file.seek(0)
file.write(text)
# truncate 的作用就是在当前位置截断文件, 这样以前旧的内容(还没有被覆盖的部分)就删除了
file.truncate()
file.close()
print(f'done {filename}')