-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleetcode.py
36 lines (33 loc) · 1.4 KB
/
leetcode.py
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
import os
import re
import sys
import json
data = open('result.json').read()
data = json.loads(data)
text = ' '.join(sys.argv[1:])
text = '.*' + '.*'.join(text.split()) + '.*'
result = []
for item in data:
# 匹配中文或者英文标题
if not re.search(text.lower(), item['titleCN'] + item['titleUS'].lower()):
continue
# lc 和 sxl 公共字段提取,遇到 lcm 关键词再覆盖
item['icon'] = {'path': 'icon.png'}
item['title'], item['subtitle'] = item['titleCN'], item['subtitleCN']
# lc 和 sxl 公共字段提取,遇到 lcm 关键词再覆盖
item['icon'] = {'path': 'icon.png'}
item['title'], item['subtitle'] = item['titleCN'], item['subtitleCN']
keyword = os.environ['alfred_workflow_keyword'].lower()
if keyword == 'lc':
item['arg'] = f"https://leetcode.cn/problems/{item['arg']}/description/"
if keyword == 'lcm':
item['arg'] = f"https://leetcode.com/problems/{item['arg']}/description/"
item['title'], item['subtitle'] = item['titleUS'], item['subtitleUS']
if keyword == 'sxl':
parts = item['titleCN'].split(' ')
text = ''.join(parts[1:]).replace(' ', '')
formatted_title = '{:0>4}.{}.html'.format(parts[0], text)
item['arg'] = f"https://programmercarl.com/{formatted_title}"
result.append(item)
alfredJSON = json.dumps({'items': result}, indent=2, ensure_ascii=False)
sys.stdout.write(alfredJSON)