Skip to content

Commit 40e90f3

Browse files
committed
feat: update github actions for the project
1 parent f271d6e commit 40e90f3

File tree

5 files changed

+68
-54
lines changed

5 files changed

+68
-54
lines changed

.github/workflows/branch-merge.yml

-17
This file was deleted.

.github/workflows/cas.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Compress And Sync
2+
3+
on:
4+
push:
5+
branches: [main]
6+
paths:
7+
- "**.jpg"
8+
- "**.jpeg"
9+
- "**.png"
10+
- "**.webp"
11+
12+
jobs:
13+
compress:
14+
runs-on: ubuntu-latest
15+
if: github.repository == 'doocs/leetcode'
16+
steps:
17+
- name: Checkout Branch
18+
uses: actions/checkout@v2
19+
20+
- name: Compress Images
21+
uses: calibreapp/image-actions@master
22+
with:
23+
githubToken: ${{ secrets.GITHUB_TOKEN }}
24+
compressOnly: true
25+
26+
- name: Commit Files
27+
run: |
28+
git config --local user.email "action@github.com"
29+
git config --local user.name "GitHub Action"
30+
git commit -m "[Automated] Optimize images" -a
31+
32+
- name: Push Changes
33+
uses: ad-m/github-push-action@master
34+
with:
35+
github_token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/rebase.yml

-20
This file was deleted.

.github/workflows/release.yml

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
steps:
1414
- name: Checkout code
1515
uses: actions/checkout@v2
16+
1617
- name: Create Release
1718
id: create_release
1819
uses: actions/create-release@v1

solution/main.py

+32-17
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class LCSpider:
1414
def __init__(self):
1515
self.session = requests.session()
1616

17-
# mapper
18-
self.number_mapper = [str(i * 100).zfill(4) + '-' + str(i * 100 + 99).zfill(4) for i in range(100)]
17+
# ['0000-0099', '0100-0199', ..., '9900-9999']
18+
self.sub_folders = [str(i * 100).zfill(4) + '-' + str(i * 100 + 99).zfill(4) for i in range(100)]
1919
self.difficulty_mapper = dict(Easy='简单', Medium='中等', Hard='困难')
2020

2121
# result
@@ -24,7 +24,7 @@ def __init__(self):
2424
self.md_table_en = []
2525

2626
def get_question_detail(self, question_title_slug):
27-
"""fetch question detail by leetcode's api"""
27+
"""fetch question detail by lc's api"""
2828
form_data = {
2929
'operationName': 'globalData',
3030
'query': 'query globalData {\n feature {\n questionTranslation\n subscription\n signUp\n '
@@ -48,7 +48,10 @@ def get_question_detail(self, question_title_slug):
4848
'Content-Type': 'application/json',
4949
'Referer': 'https://leetcode-cn.com/problems/' + question_title_slug
5050
}
51-
self.session.post(LCSpider.graph_url, data=json.dumps(form_data), headers=headers, timeout=10)
51+
self.session.post(url=LCSpider.graph_url,
52+
data=json.dumps(form_data),
53+
headers=headers,
54+
timeout=10)
5255

5356
form_data = {
5457
'operationName': 'questionData',
@@ -71,21 +74,26 @@ def get_question_detail(self, question_title_slug):
7174
}
7275

7376
# get question detail
74-
resp = self.session.post(url=LCSpider.graph_url, data=json.dumps(form_data).encode('utf-8'), headers=headers,
77+
resp = self.session.post(url=LCSpider.graph_url,
78+
data=json.dumps(form_data).encode('utf-8'),
79+
headers=headers,
7580
timeout=10)
7681
res = resp.json()
7782
return res['data']['question']
7883

7984
def get_all_questions(self):
80-
"""fetch all question by leetcode's api"""
85+
"""fetch all question by lc's api"""
8186
headers = {
8287
'accept': 'application/json, text/javascript, */*; q=0.01',
8388
'content-type': 'application/json',
8489
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
8590
'Chrome/77.0.3865.120 Safari/537.36',
8691
'x-requested-with': 'XMLHttpRequest'
8792
}
88-
resp = self.session.get(url='https://leetcode.com/api/problems/all/', headers=headers, allow_redirects=False)
93+
resp = self.session.get(url='https://leetcode.com/api/problems/all/',
94+
headers=headers,
95+
allow_redirects=False,
96+
timeout=10)
8997
questions = resp.json()['stat_status_pairs']
9098

9199
for question in questions:
@@ -96,16 +104,17 @@ def get_all_questions(self):
96104

97105
question_title_en = question['stat']['question__title']
98106
question_title_en = re.sub(r'[\\/:*?"<>|]', '', question_title_en).strip()
107+
99108
question_title_slug = question['stat']['question__title_slug']
100109

101-
url_cn = 'https://leetcode-cn.com/problems/' + question_title_slug
102-
url_en = 'https://leetcode.com/problems/' + question_title_slug
110+
url_cn = f'https://leetcode-cn.com/problems/{question_title_slug}'
111+
url_en = f'https://leetcode.com/problems/{question_title_slug}'
112+
113+
path_cn = f'/solution/{self.sub_folders[no]}/{frontend_question_id}.{quote(question_title_en)}/README.md'
114+
path_en = f'/solution/{self.sub_folders[no]}/{frontend_question_id}.{quote(question_title_en)}/README_EN.md'
103115

104-
path_cn = '/solution/{}/{}.{}/README.md'.format(self.number_mapper[no], frontend_question_id,
105-
quote(question_title_en))
106-
path_en = '/solution/{}/{}.{}/README_EN.md'.format(self.number_mapper[no], frontend_question_id,
107-
quote(question_title_en))
108116
print(frontend_question_id)
117+
109118
item = {
110119
'question_id': question_id,
111120
'frontend_question_id': frontend_question_id,
@@ -164,7 +173,8 @@ def save_result(self):
164173
f.write(json.dumps(self.result))
165174

166175
def generate_readme(self):
167-
"""generate README.md and README_EN.md files"""
176+
"""generate readme files"""
177+
# generate README.md
168178
items = []
169179
table_cn = "\n| 题号 | 题解 | 标签 | 难度 | 备注 |\n| --- | --- | --- | --- | --- |"
170180
for item in sorted(self.md_table_cn, key=lambda x: x[0]):
@@ -176,6 +186,7 @@ def generate_readme(self):
176186
with open('./README.md', 'w', encoding='utf-8') as f:
177187
f.write(readme_cn.format(table_cn))
178188

189+
# generate README_EN.md
179190
items = []
180191
table_en = "\n| # | Solution | Tags | Difficulty | Remark |\n| --- | --- | --- | --- | --- |"
181192
for item in sorted(self.md_table_en, key=lambda x: x[0]):
@@ -197,22 +208,22 @@ def generate_question_readme(self):
197208
result = json.loads(result)
198209
for item in result:
199210
no = int(item['frontend_question_id']) // 100
200-
path = f'./{self.number_mapper[no]}/{item["frontend_question_id"]}.{item["title_en"]}'
211+
path = f'./{self.sub_folders[no]}/{item["frontend_question_id"]}.{item["title_en"]}'
201212
path = path.replace(":", " ")
202213

203214
if os.path.isdir(path):
204215
continue
205216
os.makedirs(path)
206217

207-
# generate readme cn
218+
# generate lc problem readme cn
208219
with open(f'{path}/README.md', 'w', encoding='utf-8') as f1:
209220
f1.write(readme_cn.format(int(item['frontend_question_id']),
210221
item["title_cn"],
211222
item['url_cn'],
212223
item['relative_path_en'],
213224
item['content_cn']))
214225

215-
# generate readme en
226+
# generate lc problem readme en
216227
with open(f'{path}/README_EN.md', 'w', encoding='utf-8') as f2:
217228
f2.write(readme_en.format(int(item['frontend_question_id']),
218229
item["title_en"],
@@ -222,6 +233,7 @@ def generate_question_readme(self):
222233

223234
@staticmethod
224235
def generate_summary():
236+
"""generate summary files"""
225237
summary_cn = ""
226238
summary_en = ""
227239
for file in os.listdir("./"):
@@ -233,8 +245,11 @@ def generate_summary():
233245
summary_cn += f'\t- [{sub}](/solution/{file}/{enc}/README.md)\n'
234246
summary_en += f'\t- [{sub}](/solution/{file}/{enc}/README_EN.md)\n'
235247

248+
# generate summary.md
236249
with open('./summary.md', 'w', encoding='utf-8') as f:
237250
f.write(summary_cn)
251+
252+
# generate summary_en.md
238253
with open('./summary_en.md', 'w', encoding='utf-8') as f:
239254
f.write(summary_en)
240255

0 commit comments

Comments
 (0)