forked from lifegpc/bili
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare.py
202 lines (185 loc) · 7.44 KB
/
prepare.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
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
# (C) 2019-2020 lifegpc
# This file is part of bili.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from getopt import getopt
from lang import lan, getlan, getdict
from JSONParser import loadset
import sys
from typing import List
from os.path import exists
from os import listdir, remove, system
from requests import Session
from file import mkdir
from platform import system as systemname
from re import search
la = None
se = loadset()
if se == -1 or se == -2:
se = {}
la = getdict('command', getlan(se, {}))
def ph():
h = f'''{la['O1']}
prepare.py [-h/-?/--help] {la['O2']}
prepare.py [--lan <LANGUAGECODE>] [-u] [-c] [-j PATH] [filelist]
--lan <LANGUAGECODE> {la['O55']}
-u {la['O73']}
-c {la['O75']}
-j PATH {la['O76']}
filelist {la['O74']}'''
print(h)
def gopt(args: List[str]):
re = getopt(args, 'h?ucj:', ['help', 'lan='])
rr = re[0]
r = {}
h = False
for i in rr:
if i[0] == '-h' or i[0] == '-?' or i[0] == '--help':
h = True
if i[0] == '--lan' and 'lan' not in r and (i[1] == 'null' or i[1] in lan):
r['lan'] = i[1]
if i[0] == '-u':
r['u'] = True
if i[0] == '-c':
r['c'] = True
if i[0] == '-j' and 'j' not in r:
r['j'] = i[1]
if h:
global la
la = getdict('command', getlan(se, r))
ph()
sys.exit(0)
return r, re[1]
class main:
_r: Session = None
_upa: bool = False
_onlyc: bool = False
_java: str = "java"
def __init__(self, ip: dict, fl: List[str]):
if 'u' in ip:
self._upa = True
if 'c' in ip:
self._onlyc = True
if 'j' in ip:
self._java = ip['j']
if not exists('webuihtml/js(origin)/'):
raise FileNotFoundError('webuihtml/js(origin)/')
if len(fl) == 0:
fl = listdir('webuihtml/js(origin)/')
self._r = Session()
if not self._onlyc:
tag = self._get_tag(
'https://api.github.com/repos/jquery/jquery/tags')
if not exists('webuihtml/jso/'):
mkdir('webuihtml/jso/')
self._check('webuihtml/jso/jquery.js',
f"https://code.jquery.com/jquery-{tag}.min.js", tag)
self._check('webuihtml/jso/qrcode.min.js',
"https://github.com/davidshimjs/qrcodejs/raw/master/qrcode.min.js")
tag = self._get_tag(
'https://api.github.com/repos/emn178/js-sha256/tags')
self._check('webuihtml/jso/sha256.min.js',
"https://github.com/emn178/js-sha256/raw/master/build/sha256.min.js", tag)
tag = self._get_tag(
'https://api.github.com/repos/fengyuanchen/viewerjs/tags')
self._check('webuihtml/jso/viewer.min.js',
'https://github.com/fengyuanchen/viewerjs/raw/master/dist/viewer.min.js', tag)
if not exists('webuihtml/csso/'):
mkdir('webuihtml/csso/')
self._check('webuihtml/csso/viewer.min.css',
'https://github.com/fengyuanchen/viewerjs/raw/master/dist/viewer.min.css', tag)
tag = self._get_tag(
'https://api.github.com/repos/zenorocha/clipboard.js/tags')
self._check('webuihtml/jso/clipboard.min.js',
'https://github.com/zenorocha/clipboard.js/raw/master/dist/clipboard.min.js', tag)
tag = self._get_tag(
'https://api.github.com/repos/eligrey/FileSaver.js/tags')
self._check('webuihtml/jso/FileSaver.min.js',
"https://github.com/eligrey/FileSaver.js/raw/master/dist/FileSaver.min.js", tag)
if not self._check_java():
raise FileNotFoundError('Can not find java.')
tag = self._get_compiler_tag()
self._check(
'compiler.jar', f"https://repo1.maven.org/maven2/com/google/javascript/closure-compiler/{tag}/closure-compiler-{tag}.jar", tag)
tag = self._get_tag(
'https://api.github.com/repos/dankogai/js-base64/tags')
self._check_with_com('webuihtml/jso/base64.min.js',
"https://github.com/dankogai/js-base64/raw/main/base64.js", tag)
else:
if not self._check_java():
raise FileNotFoundError('Can not find java.')
if not exists('compiler.jar'):
raise FileNotFoundError('compiler.jar')
for fn in fl:
fn2 = f'webuihtml/js(origin)/{fn}'
if not exists(fn2):
raise FileNotFoundError(fn2)
self._com_javascript(fn)
def _check(self, fn: str, uri: str, tag: str = ""):
if exists(fn) and self._upa:
remove(fn)
if not exists(fn):
if tag == "":
print(f'INFO: {uri} -> {fn}')
else:
print(f'INFO: {uri} -> {fn} (Tag: {tag})')
self._get_file(uri, fn)
def _check_with_com(self, fn: str, uri: str, tag: str):
if exists(fn) and self._upa:
remove(fn)
if not exists(fn):
if tag == "":
print(f'INFO: {uri} -> {fn}')
else:
print(f'INFO: {uri} -> {fn} (Tag: {tag})')
fn2 = f"{fn}.tmp"
self._get_file(uri, fn2)
if system(f'{self._java} -jar compiler.jar --js "{fn2}" --js_output_file "{fn}"') != 0:
raise Exception('Error in compiler.')
remove(fn2)
def _get_tag(self, uri: str) -> str:
re = self._r.get(uri)
re = re.json()
return re[0]['name']
def _get_compiler_tag(self) -> str:
re = self._r.head(
'https://mvnrepository.com/artifact/com.google.javascript/closure-compiler/latest')
uri = re.headers['Location']
rs = search(
r'^https://mvnrepository\.com/artifact/com\.google\.javascript/closure-compiler/(.+)', uri)
return rs.groups()[0]
def _get_file(self, uri: str, fn: str):
re = self._r.get(uri, stream=True)
with open(fn, 'ab') as f:
for c in re.iter_content(1024):
if c:
f.write(c)
def _check_java(self) -> bool:
sn = systemname()
s = " 2>&0 1>&0"
if sn == "Linux":
s = " > /dev/null 2>&1"
if system(f"{self._java} -h{s}") == 0:
return True
return False
def _com_javascript(self, fn: str):
print(f'INFO: webuihtml/js(origin)/{fn} -> webuihtml/js/{fn}')
if system(f'{self._java} -jar compiler.jar --js "webuihtml/js(origin)/{fn}" --js_output_file "webuihtml/js/{fn}"') != 0:
raise Exception('Error in compiler.')
if __name__ == "__main__":
if len(sys.argv) == 1:
main({}, [])
else:
ip, fl = gopt(sys.argv[1:])
main(ip, fl)