Update getcrx.
This commit is contained in:
parent
4ba9d9dabe
commit
993367f6ba
1 changed files with 27 additions and 13 deletions
40
getcrx.py
40
getcrx.py
|
@ -8,12 +8,16 @@ import time
|
||||||
import traceback
|
import traceback
|
||||||
import zipfile
|
import zipfile
|
||||||
|
|
||||||
from voussoirkit import pipeable
|
|
||||||
from voussoirkit import interactive
|
from voussoirkit import interactive
|
||||||
|
from voussoirkit import operatornotify
|
||||||
|
from voussoirkit import pipeable
|
||||||
|
from voussoirkit import vlogging
|
||||||
|
|
||||||
|
log = vlogging.getLogger(__name__, 'getcrx')
|
||||||
|
|
||||||
FILENAME_BADCHARS = '\\/:*?<>|"'
|
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'
|
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()
|
session = requests.Session()
|
||||||
|
@ -26,6 +30,8 @@ def sanitize_filename(name):
|
||||||
def get_webstore_name_version(extension_id):
|
def get_webstore_name_version(extension_id):
|
||||||
url = WEBSTORE_URL.format(extension_id=extension_id)
|
url = WEBSTORE_URL.format(extension_id=extension_id)
|
||||||
response = session.get(url)
|
response = session.get(url)
|
||||||
|
response.raise_for_status()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
name = response.text
|
name = response.text
|
||||||
name = name.split('meta property="og:title" content="')[1]
|
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)
|
version = manifest.get('version', None)
|
||||||
return (name, version)
|
return (name, version)
|
||||||
|
|
||||||
def getcrx(extension_id, auto_overwrite=None):
|
def get_crx(extension_id):
|
||||||
url = CRX_URL.format(extension_id=extension_id)
|
url = CRX_URL.format(extension_id=extension_id)
|
||||||
response = session.get(url)
|
response = session.get(url)
|
||||||
response.raise_for_status()
|
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)
|
(name, version) = get_webstore_name_version(extension_id)
|
||||||
|
|
||||||
|
crx_bytes = get_crx(extension_id)
|
||||||
|
|
||||||
if name is None or version is None:
|
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
|
name = name or crx_name
|
||||||
version = version or crx_ver
|
version = version or crx_ver
|
||||||
|
|
||||||
name = name or extension_id
|
name = name or extension_id
|
||||||
version = version or time.strftime('%Y%m%d')
|
version = version or time.strftime('%Y-%m-%d')
|
||||||
|
|
||||||
version = version or response.url.split('/')[-1]
|
|
||||||
|
|
||||||
crx_filename = '{name} ({id}) [{version}]'
|
crx_filename = '{name} ({id}) [{version}]'
|
||||||
crx_filename = crx_filename.format(
|
crx_filename = crx_filename.format(
|
||||||
|
@ -90,8 +100,8 @@ def getcrx(extension_id, auto_overwrite=None):
|
||||||
|
|
||||||
if permission:
|
if permission:
|
||||||
crx_handle = open(crx_filename, 'wb')
|
crx_handle = open(crx_filename, 'wb')
|
||||||
crx_handle.write(response.content)
|
crx_handle.write(crx_bytes)
|
||||||
print(f'Downloaded "{crx_filename}".')
|
log.info(f'Downloaded "{crx_filename}".')
|
||||||
|
|
||||||
def getcrx_argparse(args):
|
def getcrx_argparse(args):
|
||||||
extension_ids = []
|
extension_ids = []
|
||||||
|
@ -107,7 +117,8 @@ def getcrx_argparse(args):
|
||||||
lines = handle.readlines()
|
lines = handle.readlines()
|
||||||
extension_ids.extend(lines)
|
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:
|
if args.overwrite and not args.dont_overwrite:
|
||||||
auto_overwrite = True
|
auto_overwrite = True
|
||||||
|
@ -118,14 +129,17 @@ def getcrx_argparse(args):
|
||||||
|
|
||||||
for extension_id in extension_ids:
|
for extension_id in extension_ids:
|
||||||
try:
|
try:
|
||||||
getcrx(extension_id, auto_overwrite=auto_overwrite)
|
download_crx(extension_id, auto_overwrite=auto_overwrite)
|
||||||
except Exception:
|
except Exception:
|
||||||
if args.fail_early:
|
if args.fail_early:
|
||||||
raise
|
raise
|
||||||
else:
|
else:
|
||||||
traceback.print_exc()
|
log.error(traceback.format_exc())
|
||||||
print('Resuming...')
|
pipeable.stderr('Resuming...')
|
||||||
|
return 0
|
||||||
|
|
||||||
|
@operatornotify.main_decorator(subject='getcrx')
|
||||||
|
@vlogging.main_decorator
|
||||||
def main(argv):
|
def main(argv):
|
||||||
parser = argparse.ArgumentParser()
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue