nix-crx/update.py

49 lines
1.9 KiB
Python
Executable file

#!/usr/bin/env nix-shell
#! nix-shell -i python -p python3
import json
import xml.etree.ElementTree as ET
import hashlib
from urllib.request import urlopen
from os.path import abspath, dirname
JSON_PATH = dirname(abspath(__file__)) + '/extensions.json'
JSON_OUT_PATH = dirname(abspath(__file__)) + '/extensions/extensions-generated.json'
with open(JSON_PATH, 'r') as f:
exts = json.load(f)
for ext in exts:
print(f"updating {ext['name']}")
if ext["updateMethod"] == "github":
with urlopen(ext["updateUrl"]) as r:
latest = json.load(r)[0]
for asset in latest["assets"]:
if asset["browser_download_url"].endswith(".crx"):
ext["url"] = asset["browser_download_url"]
break
with urlopen(ext["url"]) as blob:
sha256 = hashlib.new("SHA256")
if blob.status == 200:
size, n = 0, 16384
buf = bytearray(n)
while n != 0:
n = blob.readinto(buf)
size += n
if n>0:
sha256.update(buf[:n])
ext["sha256"] = sha256.hexdigest().lower()
ext["version"] = latest["tag_name"].lstrip("v")
elif ext["updateMethod"] == "google":
resp = urlopen(f"https://clients2.google.com/service/update2/crx?response=updatecheck&acceptformat=crx2,crx3&prodversion=89.0.0.0&x=id%3D{ext['id']}%26uc")
xml = resp.read()
xmltree = ET.fromstring(xml)
app = xmltree.find("{http://www.google.com/update2/response}app").find("{http://www.google.com/update2/response}updatecheck")
latest = app.attrib
ext["url"] = latest["codebase"]
ext["sha256"] = latest["hash_sha256"]
ext["version"] = latest["version"]
with open(JSON_OUT_PATH, 'w') as out:
json.dump(exts, out, indent=2)