-
Notifications
You must be signed in to change notification settings - Fork 5
/
update-freewvsdb
executable file
·51 lines (41 loc) · 1.26 KB
/
update-freewvsdb
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
#!/usr/bin/python3
import argparse
import io
import os
import pathlib
import sys
import tarfile
import urllib.request
DBURL = "https://freewvsdb.schokokeks.org/"
dbpaths = ["/var/lib/freewvs/", str(pathlib.Path.home()) + "/.cache/freewvs/"]
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--force", action="store_true", help="Force update")
args = ap.parse_args()
target = False
for dbpath in dbpaths:
if not os.path.isdir(dbpath):
try:
os.makedirs(dbpath)
except PermissionError:
continue
if os.access(dbpath, os.W_OK):
target = dbpath
break
if not target:
sys.exit("Can't write to " + " or ".join(dbpaths))
old = 0
if os.path.isfile(target + "timestamp"):
with open(target + "timestamp", encoding="ascii") as f:
old = int(f.read())
new = int(urllib.request.urlopen(DBURL + "freewvsdb.timestamp").read())
if new == old and not args.force:
# nothing to do
sys.exit()
tarball = urllib.request.urlopen(DBURL + str(new) + ".tar.xz").read()
tf = tarfile.open(fileobj=io.BytesIO(tarball))
if sys.version_info < (3, 11, 4):
tf.extractall(path=target)
else:
tf.extractall(path=target, filter="data")
with open(target + "timestamp", "w", encoding="ascii") as f:
f.write(str(new))