Update getcrx.

This commit is contained in:
voussoir 2021-09-22 20:28:18 -07:00
parent 4ba9d9dabe
commit 993367f6ba
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -8,12 +8,16 @@ import time
import traceback
import zipfile
from voussoirkit import pipeable
from voussoirkit import interactive
from voussoirkit import operatornotify
from voussoirkit import pipeable
from voussoirkit import vlogging
log = vlogging.getLogger(__name__, 'getcrx')
FILENAME_BADCHARS = '\\/:*?<>|"'
WEBSTORE_URL = 'https://chrome.google.com/webstore/detail/x/{extension_id}'
WEBSTORE_URL = 'https://chrome.google.com/webstore/detail/{extension_id}'
CRX_URL = 'https://clients2.google.com/service/update2/crx?response=redirect&prodversion=83.0.4103.116&acceptformat=crx2,crx3&x=id%3D{extension_id}%26uc'
session = requests.Session()
@ -26,6 +30,8 @@ def sanitize_filename(name):
def get_webstore_name_version(extension_id):
url = WEBSTORE_URL.format(extension_id=extension_id)
response = session.get(url)
response.raise_for_status()
try:
name = response.text
name = name.split('meta property="og:title" content="')[1]
@ -50,21 +56,25 @@ def get_crx_name_version(crx_bytes):
version = manifest.get('version', None)
return (name, version)
def getcrx(extension_id, auto_overwrite=None):
def get_crx(extension_id):
url = CRX_URL.format(extension_id=extension_id)
response = session.get(url)
response.raise_for_status()
return response.content
def download_crx(extension_id, auto_overwrite=None):
log.info('Checking %s.', extension_id)
(name, version) = get_webstore_name_version(extension_id)
crx_bytes = get_crx(extension_id)
if name is None or version is None:
(crx_name, crx_ver) = get_crx_name_version(response.content)
(crx_name, crx_ver) = get_crx_name_version(crx_bytes)
name = name or crx_name
version = version or crx_ver
name = name or extension_id
version = version or time.strftime('%Y%m%d')
version = version or response.url.split('/')[-1]
version = version or time.strftime('%Y-%m-%d')
crx_filename = '{name} ({id}) [{version}]'
crx_filename = crx_filename.format(
@ -90,8 +100,8 @@ def getcrx(extension_id, auto_overwrite=None):
if permission:
crx_handle = open(crx_filename, 'wb')
crx_handle.write(response.content)
print(f'Downloaded "{crx_filename}".')
crx_handle.write(crx_bytes)
log.info(f'Downloaded "{crx_filename}".')
def getcrx_argparse(args):
extension_ids = []
@ -107,7 +117,8 @@ def getcrx_argparse(args):
lines = handle.readlines()
extension_ids.extend(lines)
extension_ids = [x.split('/')[-1].strip() for x in extension_ids]
extension_ids = [x for x in extension_ids if not x.startswith('#')]
extension_ids = [x.rsplit('/', 1)[-1].strip() for x in extension_ids]
if args.overwrite and not args.dont_overwrite:
auto_overwrite = True
@ -118,14 +129,17 @@ def getcrx_argparse(args):
for extension_id in extension_ids:
try:
getcrx(extension_id, auto_overwrite=auto_overwrite)
download_crx(extension_id, auto_overwrite=auto_overwrite)
except Exception:
if args.fail_early:
raise
else:
traceback.print_exc()
print('Resuming...')
log.error(traceback.format_exc())
pipeable.stderr('Resuming...')
return 0
@operatornotify.main_decorator(subject='getcrx')
@vlogging.main_decorator
def main(argv):
parser = argparse.ArgumentParser()