|
| 1 | +import json |
| 2 | +import os |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +from pathlib import Path |
| 6 | +from zipfile import ZipFile |
| 7 | + |
| 8 | +PACKAGE_NAME_REGEX = re.compile(r"package: name='([^']+)'") |
| 9 | +VERSION_CODE_REGEX = re.compile(r"versionCode='([^']+)'") |
| 10 | +VERSION_NAME_REGEX = re.compile(r"versionName='([^']+)'") |
| 11 | +IS_NSFW_REGEX = re.compile(r"'tachiyomi.extension.nsfw' value='([^']+)'") |
| 12 | +APPLICATION_LABEL_REGEX = re.compile(r"^application-label:'([^']+)'", re.MULTILINE) |
| 13 | +APPLICATION_ICON_320_REGEX = re.compile( |
| 14 | + r"^application-icon-320:'([^']+)'", re.MULTILINE |
| 15 | +) |
| 16 | +LANGUAGE_REGEX = re.compile(r"tachiyomi-([^\.]+)") |
| 17 | + |
| 18 | +*_, ANDROID_BUILD_TOOLS = (Path(os.environ["ANDROID_HOME"]) / "build-tools").iterdir() |
| 19 | +REPO_DIR = Path("repo") |
| 20 | +REPO_APK_DIR = REPO_DIR / "apk" |
| 21 | +REPO_ICON_DIR = REPO_DIR / "icon" |
| 22 | + |
| 23 | +REPO_ICON_DIR.mkdir(parents=True, exist_ok=True) |
| 24 | + |
| 25 | +with open("output.json", encoding="utf-8") as f: |
| 26 | + inspector_data = json.load(f) |
| 27 | + |
| 28 | +index_data = [] |
| 29 | +index_min_data = [] |
| 30 | + |
| 31 | +for apk in REPO_APK_DIR.iterdir(): |
| 32 | + badging = subprocess.check_output( |
| 33 | + [ |
| 34 | + ANDROID_BUILD_TOOLS / "aapt", |
| 35 | + "dump", |
| 36 | + "--include-meta-data", |
| 37 | + "badging", |
| 38 | + apk, |
| 39 | + ] |
| 40 | + ).decode() |
| 41 | + |
| 42 | + package_info = next(x for x in badging.splitlines() if x.startswith("package: ")) |
| 43 | + package_name = PACKAGE_NAME_REGEX.search(package_info).group(1) |
| 44 | + application_icon = APPLICATION_ICON_320_REGEX.search(badging).group(1) |
| 45 | + |
| 46 | + with ZipFile(apk) as z, z.open(application_icon) as i, ( |
| 47 | + REPO_ICON_DIR / f"{package_name}.png" |
| 48 | + ).open("wb") as f: |
| 49 | + f.write(i.read()) |
| 50 | + |
| 51 | + language = LANGUAGE_REGEX.search(apk.name).group(1) |
| 52 | + sources = inspector_data[package_name] |
| 53 | + |
| 54 | + if len(sources) == 1: |
| 55 | + source_language = sources[0]["lang"] |
| 56 | + |
| 57 | + if ( |
| 58 | + source_language != language |
| 59 | + and source_language not in {"all", "other"} |
| 60 | + and language not in {"all", "other"} |
| 61 | + ): |
| 62 | + language = source_language |
| 63 | + |
| 64 | + common_data = { |
| 65 | + "name": APPLICATION_LABEL_REGEX.search(badging).group(1), |
| 66 | + "pkg": package_name, |
| 67 | + "apk": apk.name, |
| 68 | + "lang": language, |
| 69 | + "code": int(VERSION_CODE_REGEX.search(package_info).group(1)), |
| 70 | + "version": VERSION_NAME_REGEX.search(package_info).group(1), |
| 71 | + "nsfw": int(IS_NSFW_REGEX.search(badging).group(1)), |
| 72 | + } |
| 73 | + min_data = { |
| 74 | + **common_data, |
| 75 | + "sources": [], |
| 76 | + } |
| 77 | + |
| 78 | + for source in sources: |
| 79 | + min_data["sources"].append( |
| 80 | + { |
| 81 | + "name": source["name"], |
| 82 | + "lang": source["lang"], |
| 83 | + "id": source["id"], |
| 84 | + "baseUrl": source["baseUrl"], |
| 85 | + } |
| 86 | + ) |
| 87 | + |
| 88 | + index_min_data.append(min_data) |
| 89 | + index_data.append( |
| 90 | + { |
| 91 | + **common_data, |
| 92 | + "hasReadme": 0, |
| 93 | + "hasChangelog": 0, |
| 94 | + "sources": sources, |
| 95 | + } |
| 96 | + ) |
| 97 | + |
| 98 | +with (REPO_DIR / "index.json").open("w", encoding="utf-8") as f: |
| 99 | + index_data_str = json.dumps(index_data, ensure_ascii=False, indent=2) |
| 100 | + |
| 101 | + print(index_data_str) |
| 102 | + f.write(index_data_str) |
| 103 | + |
| 104 | +with (REPO_DIR / "index.min.json").open("w", encoding="utf-8") as f: |
| 105 | + json.dump(index_min_data, f, ensure_ascii=False, separators=(",", ":")) |
0 commit comments