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 (r"^application-icon-320:'([^']+)'" , re .MULTILINE )
14+ LANGUAGE_REGEX = re .compile (r"tachiyomi-([^.]+)" )
15+
16+ * _ , ANDROID_BUILD_TOOLS = (Path (os .environ ["ANDROID_HOME" ]) / "build-tools" ).iterdir ()
17+ REPO_DIR = Path ("repo" )
18+ REPO_APK_DIR = REPO_DIR / "apk"
19+ REPO_ICON_DIR = REPO_DIR / "icon"
20+
21+ REPO_ICON_DIR .mkdir (parents = True , exist_ok = True )
22+
23+ with open ("output.json" , encoding = "utf-8" ) as f :
24+ inspector_data = json .load (f )
25+
26+ index_min_data = []
27+
28+ for apk in REPO_APK_DIR .iterdir ():
29+ badging = subprocess .check_output (
30+ [
31+ ANDROID_BUILD_TOOLS / "aapt" ,
32+ "dump" ,
33+ "--include-meta-data" ,
34+ "badging" ,
35+ apk ,
36+ ]
37+ ).decode ()
38+
39+ package_info = next (x for x in badging .splitlines () if x .startswith ("package: " ))
40+ package_name = PACKAGE_NAME_REGEX .search (package_info ).group (1 )
41+ application_icon = APPLICATION_ICON_320_REGEX .search (badging ).group (1 )
42+
43+ with ZipFile (apk ) as z , z .open (application_icon ) as i , (
44+ REPO_ICON_DIR / f"{ package_name } .png"
45+ ).open ("wb" ) as f :
46+ f .write (i .read ())
47+
48+ language = LANGUAGE_REGEX .search (apk .name ).group (1 )
49+ sources = inspector_data [package_name ]
50+
51+ if len (sources ) == 1 :
52+ source_language = sources [0 ]["lang" ]
53+
54+ if (
55+ source_language != language
56+ and source_language not in {"all" , "other" }
57+ and language not in {"all" , "other" }
58+ ):
59+ language = source_language
60+
61+ common_data = {
62+ "name" : APPLICATION_LABEL_REGEX .search (badging ).group (1 ),
63+ "pkg" : package_name ,
64+ "apk" : apk .name ,
65+ "lang" : language ,
66+ "code" : int (VERSION_CODE_REGEX .search (package_info ).group (1 )),
67+ "version" : VERSION_NAME_REGEX .search (package_info ).group (1 ),
68+ "nsfw" : int (IS_NSFW_REGEX .search (badging ).group (1 )),
69+ }
70+ min_data = {
71+ ** common_data ,
72+ "sources" : [],
73+ }
74+
75+ for source in sources :
76+ min_data ["sources" ].append (
77+ {
78+ "name" : source ["name" ],
79+ "lang" : source ["lang" ],
80+ "id" : source ["id" ],
81+ "baseUrl" : source ["baseUrl" ],
82+ }
83+ )
84+
85+ index_min_data .append (min_data )
86+
87+ with REPO_DIR .joinpath ("index.min.json" ).open ("w" , encoding = "utf-8" ) as index_file :
88+ json .dump (index_min_data , index_file , ensure_ascii = False , separators = ("," , ":" ))
0 commit comments