ReVancedBuilder/APKPure_dl.py

106 lines
3.7 KiB
Python
Raw Normal View History

2023-08-09 02:17:29 -05:00
import os
2023-08-08 23:33:09 -05:00
import sys
import json
from packaging.version import Version
import requests as req
from bs4 import BeautifulSoup as bs
# Determine the best version available to download
2023-08-09 02:17:29 -05:00
def apkpure_best_match(version, soup):
2023-08-08 23:33:09 -05:00
try:
2023-08-09 02:17:29 -05:00
vers_list = [Version(x['data-dt-version']) for x in soup.css.select(f"a[data-dt-apkid^=\"b/APK/\"]")]
2023-08-08 23:33:09 -05:00
except:
sys.exit(f" There was some error getting list of versions of {apk}...")
if version != '0':
vers_list = filter(lambda x: x <= Version(version), vers_list)
2023-08-09 02:17:29 -05:00
return str(max(vers_list))
2023-08-08 23:33:09 -05:00
# Download an apk from APKPure.com
2023-08-09 02:17:29 -05:00
def apkpure_dl(apk, appname, version, hard_version, session, present_vers, flag):
res = session.get(f"https://apkpure.com/{appname}/{apk}/versions")
res.raise_for_status()
soup = bs(res.text, 'html.parser')
2023-08-08 23:33:09 -05:00
if not hard_version:
2023-08-09 02:17:29 -05:00
version = apkpure_best_match(version, soup)
if flag == 'checkonly' and present_vers[apk] != version:
print(f"{apk} has an update ({present_vers[apk]} -> {version})")
return
2023-08-08 23:33:09 -05:00
try:
2023-08-09 02:17:29 -05:00
if present_vers[apk] == version and flag != 'force' and os.path.isfile(apk):
2023-08-08 23:33:09 -05:00
print(f"Recommended version {version} of {apk} is already present.")
return
except KeyError:
pass
print(f" Downloading {apk} version {version}...")
# Get the version code
try:
2023-08-09 02:17:29 -05:00
ver_code = soup.css.select(f"a[data-dt-version=\"{version}\"][data-dt-apkid^=\"b/APK/\"]")[0]['data-dt-versioncode']
2023-08-08 23:33:09 -05:00
except:
sys.exit(f" There was some error while downloading {apk}...")
res = session.get(f"https://d.apkpure.com/b/APK/{apk}?versionCode={ver_code}", stream=True)
res.raise_for_status()
2023-08-09 02:17:29 -05:00
with open(apk+'.apk', 'wb') as f:
2023-08-08 23:33:09 -05:00
for chunk in res.iter_content(chunk_size=8192):
f.write(chunk)
print(" Done!")
# Download apk files, if needed
2023-08-09 02:17:29 -05:00
def get_apks(present_vers, build_config, flag):
2023-08-08 23:33:09 -05:00
print('Downloading required apk files from APKPure...')
# Get latest patches using the ReVanced API
try:
patches = req.get('https://releases.revanced.app/patches').json()
except req.exceptions.RequestException as e:
sys.exit(e)
session = req.Session()
session.headers.update({'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0'})
for app in build_config:
# Check if we need to build an app
if not build_config[app].getboolean('build'):
continue
try:
apk = build_config[app]['apk']
2023-08-09 02:17:29 -05:00
pretty_name = build_config[app]['pretty_name']
2023-08-08 23:33:09 -05:00
apkpure_appname = build_config[app]['apkpure_appname']
except:
sys.exit(f"Invalid config for {app} in build_config.toml!")
2023-08-09 02:17:29 -05:00
print(f"Checking {pretty_name}...")
2023-08-08 23:33:09 -05:00
try:
required_ver = build_config[app]['version']
required_ver[0]
hard_version = True
print(f"Using version {required_ver} of {app} from ")
except:
hard_version = False
compatible_vers = []
for patch in patches:
for pkg in patch['compatiblePackages']:
if pkg['name'] == apk:
try:
compatible_vers.append(pkg['versions'][-1])
except IndexError:
pass
if not compatible_vers:
required_ver = Version('0')
else:
required_ver = min(map(lambda x: Version(x), compatible_vers))
2023-08-09 02:17:29 -05:00
apkpure_dl(apk, apkpure_appname, str(required_ver), hard_version, session, present_vers, flag)
2023-08-08 23:33:09 -05:00
present_vers.update({apk: str(required_ver)})
return present_vers