forked from santosguerra/Photopea-Offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Updater.py
executable file
·118 lines (97 loc) · 2.97 KB
/
Updater.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
import requests
import os
import re
import json
from tqdm import tqdm
from dataclasses import dataclass
import glob
import pprint
root = "www.photopea.com/"
website = "https://photopea.com/"
urls = [
"index.html",
"style/all.css",
"code/ext/ext.js",
"promo/thumb256.png",
"code/pp/pp.js",
"code/dbs/DBS.js",
"rsrc/basic/basic.zip",
"plugins/gallery.json",
"code/ext/hb.wasm",
"code/ext/fribidi.wasm",
"plugins/tpls/index.html",
"plugins/tpls/templates.css",
"plugins/tpls/templates.js",
"papi/tpls.json",
]
#Update files
def dl_file(path):
with tqdm(desc=path, unit="B", unit_scale=True) as progress_bar:
r = requests.get(website + path, stream=True)
progress_bar.total = int(r.headers.get("Content-Length", 0))
if r.status_code != 200:
progress_bar.desc += "ERROR: HTTP Status %d" % r.status_code
return
outfn = root + path
os.makedirs(os.path.dirname(outfn), exist_ok=True)
with open(outfn, "wb") as outf:
for chunk in r.iter_content(chunk_size=1024):
progress_bar.update(len(chunk))
outf.write(chunk)
for url in urls:
dl_file(url)
db_data = open(root + "code/dbs/DBS.js").read()
db_vars = re.findall(r"var (\w+)\s*=\s*(\{[\w\W]+?\n\s*\})\s*(?=;|/\*|var)", db_data)
db = {}
for varname, vardata in db_vars:
print(varname)
try:
db[varname] = json.loads(vardata)
except Exception as e:
print("Unable to load DBS variable %s: %s" % (varname, e))
#Update fonts
@dataclass
class Font:
ff: str
fsf: str
psn: str
flg: int
cat: int
url: str
def decompress_font_list(flist):
prev_ff, prev_fsf, prev_flg, prev_cat = "", "", "0", "0"
for font in flist:
ff, fsf, psn, flg, cat, url = font.split(",")
if not ff:
ff = prev_ff
if not fsf:
fsf = prev_fsf
if not flg:
flg = prev_flg
if not cat:
cat = prev_cat
if not psn:
psn = (ff + "-" + fsf).replace(" ", "")
elif psn == "a":
psn = ff.replace(" ", "")
if not url:
url = "fs/" + psn + ".otf"
elif url == "a":
url = "gf/" + psn + ".otf"
yield Font(ff, fsf, psn, int(flg), int(cat), url)
prev_ff, prev_fsf, prev_flg, prev_cat = ff, fsf, flg, cat
for font in decompress_font_list(db["FNTS"]["list"]):
path = "rsrc/fonts/" + font.url
if not os.path.isfile(root + path):
dl_file(path)
#Delete any unused fonts
fonts_db=[root+'rsrc/fonts/'+font.url for font in decompress_font_list(db["FNTS"]["list"])]
fonts_local=glob.glob(root + 'rsrc/fonts/'+'/**/*.{otf,ttf,ttc}', recursive=True)
for font_file in list(set(fonts_local)-set(fonts_db)):
os.remove(font_file)
#Allow any port to be used
with open(root+'/code/pp/pp.js','r') as pp:
file= pp.read()
file= file.replace('8887','')
with open(root+'/code/pp/pp.js','w') as pp:
pp.write(file)