This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathdeploy.py
75 lines (61 loc) · 2.25 KB
/
deploy.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
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
import os
from zipfile import ZipFile
from bs4 import BeautifulSoup
from requests.sessions import Session
from addon.constants import MODEL_NAME
username = os.environ.get('ANKI_USERNAME')
password = os.environ.get('ANKI_PASSWORD')
addon_id = os.environ.get('ANKI_ADDON_ID')
print(username)
print(password)
print(addon_id)
def create_zip():
file_paths = []
exclude_dirs = ['test', '__pycache__', '.git', '.idea', '.pytest_cache', 'screenshots', 'venv']
exclude_files = ['README.md', '.gitignore', '.travis.yml', 'deploy.py', 'requirements.txt', '.DS_Store',
'meta.json']
exclude_ext = ['.png', '.ui', '.qrc', '.log', '.zip', '.tpl']
for dirname, sub_dirs, files in os.walk("."):
for d in exclude_dirs:
if d in sub_dirs:
sub_dirs.remove(d)
for f in exclude_files:
if f in files:
files.remove(f)
for ext in exclude_ext:
for f in files[:]:
if f.endswith(ext):
files.remove(f)
for filename in files:
file_paths.append(os.path.join(dirname, filename))
with ZipFile(f'{MODEL_NAME}.zip', 'w') as zf:
for file in file_paths:
zf.write(file)
def update(title, tags, desc):
s = Session()
URL = 'https://ankiweb.net/account/login'
rsp = s.get(URL)
soup = BeautifulSoup(rsp.text, features="html.parser")
csrf_token = soup.find('input', {'name': 'csrf_token'}).get('value')
s.post(URL, data={'submit': 1, 'csrf_token': csrf_token, 'username': username, 'password': password})
URL = 'https://ankiweb.net/shared/upload'
file = {'v21file': open(f'{MODEL_NAME}.zip', 'rb')}
rsp = s.post(URL, files=file, data={
'title': title,
'tags': tags,
'desc': desc,
'id': addon_id,
'submit': 'Update',
'v21file': file,
'v20file': '',
})
if rsp.url == f'https://ankiweb.net/shared/info/{addon_id}':
return True
else:
return False
def main():
create_zip()
# with open('anki_addon_page.tpl', encoding='utf-8') as tpl:
# return update('Dict2Anki(有道,欧陆词典单词本同步工具)', '有道 欧陆 导入 同步', tpl.read())
if __name__ == '__main__':
main()