Download the f-droid index.json instead of parsing the website.

This commit is contained in:
voussoir 2022-01-20 15:00:14 -08:00
parent 822f01b56e
commit cb226265a3
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -19,13 +19,17 @@ package_names:
''' '''
import argparse import argparse
import bs4 import bs4
import io
import json
import requests import requests
import sys import sys
import tenacity import tenacity
import traceback import traceback
import zipfile
from voussoirkit import betterhelp from voussoirkit import betterhelp
from voussoirkit import downloady from voussoirkit import downloady
from voussoirkit import httperrors
from voussoirkit import operatornotify from voussoirkit import operatornotify
from voussoirkit import pathclass from voussoirkit import pathclass
from voussoirkit import pipeable from voussoirkit import pipeable
@ -52,18 +56,17 @@ def download_file(url, path):
timeout=30, timeout=30,
) )
@my_tenacity def get_fdroid_index():
def get_apk_url(package_name): '''
url = f'https://f-droid.org/en/packages/{package_name}' Download the index-v1.json and return it as a dict.
log.debug('Downloading page %s.', url) '''
response = session.get(url, timeout=30) log.info('Downloading F-Droid package index.')
response.raise_for_status() url = 'https://f-droid.org/repo/index-v1.jar'
soup = bs4.BeautifulSoup(response.text, 'html.parser') response = requests.get(url)
li = soup.find('li', {'class': 'package-version'}) httperrors.raise_for_status(response)
aa = li.find_all('a') zf = zipfile.ZipFile(io.BytesIO(response.content))
aa = [a for a in aa if a.get('href', '').endswith('.apk')] index = json.load(zf.open('index-v1.json', 'r'))
apk_url = aa[0]['href'] return index
return apk_url
def ls_packages(path): def ls_packages(path):
packages = set() packages = set()
@ -96,19 +99,23 @@ def fpk_argparse(args):
download_count = 0 download_count = 0
index = get_fdroid_index()
for package in packages: for package in packages:
package = normalize_package_name(package) package = normalize_package_name(package)
log.info('Checking %s.', package)
try: try:
apk_url = get_apk_url(package) this_packages = index['packages'][package]
except Exception as exc: except KeyError:
exc = traceback.format_exc() log.error('%s is not in the package index.', package)
log.error('%s was unable to get apk url:\n%s', package, exc)
return_status = 1 return_status = 1
continue continue
apk_basename = downloady.basename_from_url(apk_url) most_recent = sorted(this_packages, key=lambda p: p['versionCode'])[-1]
apk_basename = most_recent['apkName']
log.debug('Most recent is %s', apk_basename)
apk_url = f'https://f-droid.org/repo/{apk_basename}'
if args.folders: if args.folders:
this_dest = destination.with_child(package) this_dest = destination.with_child(package)
this_dest.makedirs(exist_ok=True) this_dest.makedirs(exist_ok=True)