-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
1956 lines (1613 loc) · 80.9 KB
/
parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import argparse
import copy
import html
import json
import os
import re
import shutil
import sys
import tempfile
import urllib.request
from datetime import datetime
from http.cookiejar import MozillaCookieJar
from sys import exit
from typing import Dict, List, Tuple, Optional
from urllib.error import HTTPError
import requests
import ruamel.yaml
from PIL import Image
from colorama import Fore, init
import recompiler
import renamer
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0"
def main():
parser = argparse.ArgumentParser(description="Parser for PlayStore information to F-Droid YML metadata files.")
parser.add_argument("--metadata-dir",
help="Directory where F-Droid metadata files are stored.",
type=str,
nargs=1)
parser.add_argument("--repo-dir",
help="Directory where F-Droid repo files are stored.",
type=str,
nargs=1)
parser.add_argument("--unsigned-dir",
help="Directory where unsigned app files are stored.",
type=str,
nargs=1)
parser.add_argument("--language",
help="Language of the information to retrieve.",
type=str,
nargs=1,
required=True)
parser.add_argument("--force-metadata",
help="Force overwrite existing metadata.",
action="store_true")
parser.add_argument("--force-version",
help="Force updating version name and code even if they are already specified in the YML file.",
action="store_true")
parser.add_argument("--force-screenshots",
help="Force overwrite existing screenshots.",
action="store_true")
parser.add_argument("--force-icons",
help="Force overwrite existing icons.",
action="store_true")
parser.add_argument("--force-all",
help="Force overwrite existing metadata, screenshots and icons.",
action="store_true")
parser.add_argument("--convert-apks",
help="Convert APKS files to APK and sign them.",
action="store_true")
parser.add_argument("--sign-apk",
help="Sign resulting APK files from APKS conversion.",
action="store_true")
parser.add_argument("--key-file",
help="Key file used to sign the APK, required if --convert-apks is used.",
type=str,
nargs=1)
parser.add_argument("--cert-file",
help="Cert file used to sign the APK, required if --convert-apks is used.",
type=str,
nargs=1)
parser.add_argument("--certificate-password",
help="Password to sign the APK.",
type=str,
nargs=1)
parser.add_argument("--build-tools-path",
help="Path to Android SDK buildtools binaries.",
type=str,
nargs=1)
parser.add_argument("--apk-editor-path",
help="Path to the ApkEditor.jar file.",
type=str,
nargs=1)
parser.add_argument("--download-screenshots",
help="Download screenshots which will be stored in the repo directory.",
action="store_true")
parser.add_argument("--data-file",
help="Path to the JSON formatted data file. "
"Default: data.json located in the program's directory.",
type=str,
nargs=1)
parser.add_argument("--replacement-file",
help="JSON formatted file containing a dict with replacements for the package name of all found"
" apps.",
type=str,
nargs=1)
parser.add_argument("--log-path",
help="Path to the directory where to store the log files. Default: Program's directory.",
type=str,
nargs=1)
parser.add_argument("--cookie-path",
help="Path to a Netscape cookie file.",
type=str,
nargs=1)
parser.add_argument("--use-eng-name",
help="Use the English app name instead of the localized one.",
action="store_true")
parser.add_argument("--rename-files",
help="Rename APK files to packageName_versionCode. Requires aapt2 and aapt2.",
action="store_true")
parser.add_argument("--skip-if-exists",
help="Skip renaming if the file already exists. By default a numeric suffix is appended to"
" the name.",
action="store_true")
parser.add_argument("--recompile-bad-apk",
help="Recompile APK files that have CRC errors. File dates are preserved. Requires apktool.",
action="store_true")
parser.add_argument("--apktool-path",
help="Path to apktool. By default uses apktool.jar in the program's directory.",
type=str,
nargs=1)
args = parser.parse_args()
init(autoreset=True)
if args.metadata_dir is None:
metadata_dir = args.metadata_dir
else:
metadata_dir = os.path.abspath(args.metadata_dir[0]) # type: Optional[str]
if args.repo_dir is None:
repo_dir = args.repo_dir
else:
repo_dir = os.path.abspath(args.repo_dir[0]) # type: Optional[str]
if args.unsigned_dir is None:
unsigned_dir = args.unsigned_dir
else:
unsigned_dir = os.path.abspath(args.unsigned_dir[0]) # type: Optional[str]
if args.build_tools_path is None:
build_tools_path = args.build_tools_path
else:
build_tools_path = os.path.abspath(args.build_tools_path[0]) # type: Optional[str]
if args.key_file is None:
key_file = args.key_file
else:
key_file = os.path.abspath(args.key_file[0]) # type: Optional[str]
if args.cert_file is None:
cert_file = args.cert_file
else:
cert_file = os.path.abspath(args.cert_file[0]) # type: Optional[str]
if args.certificate_password is None:
certificate_password = args.certificate_password
else:
certificate_password = args.certificate_password[0] # type: Optional[str]
if args.apk_editor_path is None:
apk_editor_path = args.apk_editor_path
else:
apk_editor_path = os.path.abspath(args.apk_editor_path[0]) # type: Optional[str]
if args.replacement_file is None:
replacement_file = args.replacement_file
else:
replacement_file = os.path.abspath(args.replacement_file[0]) # type: Optional[str]
if args.cookie_path is None:
cookie_path = args.cookie_path
else:
cookie_path = os.path.abspath(args.cookie_path[0]) # type: Optional[str]
if args.data_file is None:
data_file = os.path.join(get_program_dir(), "data.json")
else:
data_file = os.path.abspath(args.data_file[0])
if args.log_path is None:
log_path = get_program_dir()
else:
log_path = os.path.abspath(args.log_path[0])
if args.apktool_path is None:
apktool_path = os.path.join(get_program_dir(), "apktool.jar")
else:
apktool_path = os.path.abspath(args.apktool_path[0])
language = args.language[0] # type: str
force_metadata = args.force_metadata # type: bool
force_version = args.force_version # type: bool
force_icons = args.force_icons # type: bool
force_screenshots = args.force_screenshots # type: bool
force_all = args.force_all # type: bool
convert_apks = args.convert_apks # type: bool
sign_apk = args.sign_apk # type: bool
download_screenshots = args.download_screenshots # type: bool
use_eng_name = args.use_eng_name # type: bool
rename_files = args.rename_files # type: bool
skip_if_exists = args.skip_if_exists # type: bool
recompile_bad_apk = args.recompile_bad_apk # type: bool
if metadata_dir is None and repo_dir is None and unsigned_dir is None:
print(Fore.RED + "ERROR: Please provide at least the metadata directory, "
"the repository directory or the unsigned directory.")
exit(1)
if metadata_dir is not None and repo_dir is not None and unsigned_dir is not None:
print(Fore.RED + "ERROR: Please provide only the metadata, "
"the repository or the unsigned directory. Not all of them.")
exit(1)
if ((metadata_dir is not None and repo_dir is not None)
or (repo_dir is not None and unsigned_dir is not None)
or (metadata_dir is not None and unsigned_dir is not None)):
print(Fore.RED + "ERROR: Please provide only one of the directories.")
exit(1)
if metadata_dir is not None:
provided_dir = "metadata"
if os.path.split(metadata_dir)[1] != "metadata":
print(Fore.RED + "ERROR: Metadata directory path doesn't look like a "
"F-Droid repository metadata directory, aborting...")
exit(1)
elif not os.path.exists(metadata_dir):
print(Fore.RED + "ERROR: Metadata directory path doesn't exist, aborting...")
exit(1)
elif not os.path.isdir(metadata_dir):
print(Fore.RED + "ERROR: Invalid metadata directory, supplied path is not a directory")
exit(1)
if repo_dir is not None:
provided_dir = "repo"
if os.path.split(repo_dir)[1] != "repo":
print(Fore.RED + "ERROR: Repo directory path doesn't look like a F-Droid repository directory, aborting...")
exit(1)
elif not os.path.exists(repo_dir):
print(Fore.RED + "ERROR: Repo directory path doesn't exist, aborting...")
exit(1)
elif not os.path.isdir(repo_dir):
print(Fore.RED + "ERROR: Invalid repo directory, supplied path is not a directory")
exit(1)
if unsigned_dir is not None:
provided_dir = "unsigned"
if os.path.split(unsigned_dir)[1] != "unsigned":
print(Fore.RED + "ERROR: Unsigned directory path doesn't look like a F-Droid unsigned directory, "
"aborting...")
exit(1)
elif not os.path.exists(unsigned_dir):
print(Fore.RED + "ERROR: Unsigned directory path doesn't exist, aborting...")
exit(1)
if not os.path.isdir(unsigned_dir):
print(Fore.RED + "ERROR: Invalid unsigned directory, supplied path is not a directory")
exit(1)
if not os.path.isfile(data_file):
print(Fore.RED + "ERROR: Invalid data file.")
exit(1)
if build_tools_path is None:
if shutil.which("aapt") is None:
print(Fore.RED + "ERROR: Please install aapt before running this program.")
exit(1)
if shutil.which("aapt2") is None:
print(Fore.RED + "ERROR: Please install aapt2 before running this program.")
exit(1)
if replacement_file is not None and not os.path.isfile(replacement_file):
print(Fore.RED + "ERROR: Invalid replacement file.")
exit(1)
if recompile_bad_apk:
if not os.path.exists(apktool_path):
print(Fore.RED + "ERROR: Apktool JAR file was not found. Required to recompile APK files.")
exit(1)
if shutil.which("java") is None:
print(Fore.RED + "ERROR: Please install java if you want to recompile APK files.")
exit(1)
try:
data_file_stream = open(data_file, mode="r", encoding="utf_8")
except FileNotFoundError:
print(Fore.RED + "ERROR: Data file not found.")
exit(1)
except PermissionError:
print(Fore.RED + "ERROR: Couldn't read data file. Permission denied.")
exit(1)
try:
data_file_content = json.load(data_file_stream) # type: dict
except json.decoder.JSONDecodeError as e:
print(Fore.RED + "ERROR: Error decoding data file.", end="\n\n")
print(e)
exit(1)
data_file_stream.close()
if not check_data_file(data_file_content=data_file_content):
exit(1)
lang = sanitize_lang(lang=language)
if lang not in data_file_content["Locales"]["Play_Store"]:
print(Fore.RED + "ERROR: Invalid language.")
exit(1)
if cookie_path is None:
print(Fore.YELLOW + "WARNING: Cookie file not specified, Amazon scraping wont work.", end="\n\n")
else:
if not os.path.isfile(cookie_path):
print(Fore.RED + "ERROR: Invalid cookie file path.")
exit(1)
if convert_apks:
if build_tools_path is None and shutil.which("apksigner") is None:
print(Fore.RED + "ERROR: Please install the build-tools package of "
"the Android SDK if you want to convert APKS files.")
exit(1)
if build_tools_path is not None:
if (not os.path.isdir(build_tools_path) or
not (os.path.isfile(os.path.join(build_tools_path, "apksigner")) or
os.path.isfile(os.path.join(build_tools_path, "apksigner.bat")))):
print(Fore.RED + "ERROR: Invalid build-tools path.")
exit(1)
if shutil.which("java") is None:
print(Fore.RED + "ERROR: Please install java if you want to convert APKS files.")
exit(1)
if apk_editor_path is None:
print(Fore.RED + "ERROR: Please specify the full path of the ApkEditor.jar file.")
exit(1)
elif not os.path.isfile(apk_editor_path):
print(Fore.RED + "ERROR: Invalid ApkEditor.jar path.")
exit(1)
if sign_apk:
if key_file is None or cert_file is None:
print(Fore.RED + "ERROR: Please provide the key and certificate files for APK signing.", end="\n\n")
exit(1)
else:
if not os.path.isfile(key_file):
print(Fore.RED + "ERROR: Invalid key file path.")
exit(1)
if not os.path.isfile(cert_file):
print(Fore.RED + "ERROR: Invalid cert file path.")
exit(1)
if os.path.exists(log_path) and not os.path.isdir(log_path):
print(Fore.RED + "Invalid log path.")
exit(1)
if not os.path.exists(log_path):
os.makedirs(log_path)
package_list = {}
package_and_version = {}
if force_all:
force_metadata = True
force_screenshots = True
force_icons = True
if metadata_dir is not None: # program needs repo_dir to store icons & screenshots.
repo_dir = os.path.join(os.path.split(metadata_dir)[0], "repo")
os.makedirs(repo_dir, exist_ok=True)
dir_to_process = repo_dir
elif repo_dir is not None: # program needs metadata_dir to store the YAML files.
metadata_dir = os.path.join(os.path.split(repo_dir)[0], "metadata")
os.makedirs(metadata_dir, exist_ok=True)
dir_to_process = repo_dir
elif unsigned_dir is not None: # program needs both repo_dir and metadata_dir, nothing is saved in unsigned_dir.
metadata_dir = os.path.join(os.path.split(unsigned_dir)[0], "metadata")
repo_dir = os.path.join(os.path.split(unsigned_dir)[0], "repo")
os.makedirs(metadata_dir, exist_ok=True)
os.makedirs(repo_dir, exist_ok=True)
dir_to_process = unsigned_dir
if convert_apks:
print(Fore.GREEN + "Starting APKS conversion...", end="\n\n")
convert_apks_to_apk(sign_apk=sign_apk,
key_file=key_file,
cert_file=cert_file,
password=certificate_password,
apks_dir=dir_to_process,
build_tools_path=build_tools_path,
apk_editor_path=apk_editor_path)
if rename_files:
print(Fore.GREEN + "Renaming files...", end="\n\n")
renamer.process_path(item_path=dir_to_process,
pattern="%package_name%_%version_code%",
skip_if_exists=skip_if_exists,
build_tools_path=build_tools_path)
if recompile_bad_apk and len(os.listdir(dir_to_process)) != 0:
print(Fore.GREEN + "Checking and recompiling APK files...", end="\n\n")
recompiler.start_processing(path=dir_to_process,
apktool_path=apktool_path,
build_tools_path=build_tools_path)
print("\n")
if provided_dir == "metadata":
print(Fore.GREEN + "Getting package names, version names and version codes...", end="\n\n")
mapped_apk_files = map_apk_to_packagename(repo_dir=repo_dir)
for item in os.listdir(metadata_dir):
base_name = os.path.splitext(item)[0]
try:
apk_file_path = os.path.join(repo_dir, mapped_apk_files[base_name])
except KeyError:
apk_file_path = None
if os.path.splitext(item)[1].lower() != ".yml":
print(Fore.YELLOW + "WARNING: Skipping {}.".format(item), end="\n\n")
else:
new_base_name = get_new_packagename(replacement_file=replacement_file,
base_name=base_name)
if new_base_name is not None:
package_list[base_name] = new_base_name
else:
package_list[base_name] = base_name
if apk_file_path is not None and os.path.isfile(apk_file_path):
apk_info = renamer.get_info(app_file_path=apk_file_path,
build_tools_path=build_tools_path)
if new_base_name is not None:
package_and_version[new_base_name] = (int(apk_info["Version Code"]),
str(apk_info["Version Name"]))
else:
package_and_version[base_name] = (int(apk_info["Version Code"]),
str(apk_info["Version Name"]))
else:
if new_base_name is not None:
package_and_version[new_base_name] = (0, "0")
else:
package_and_version[base_name] = (0, "0")
retrieve_info(package_list=package_list,
package_and_version=package_and_version,
lang=lang,
metadata_dir=metadata_dir,
repo_dir=repo_dir,
force_metadata=force_metadata,
force_version=force_version,
force_screenshots=force_screenshots,
force_icons=force_icons,
dl_screenshots=download_screenshots,
data_file_content=data_file_content,
log_path=log_path,
cookie_path=cookie_path,
use_eng_name=use_eng_name)
elif provided_dir in ("repo", "unsigned"):
print(Fore.GREEN + "Getting package names, version names and version codes...", end="\n\n")
for apk_file in os.listdir(dir_to_process):
apk_file_path = os.path.join(dir_to_process, apk_file)
if os.path.isfile(apk_file_path) and os.path.splitext(apk_file)[1].lower() == ".apk":
apk_info = renamer.get_info(apk_file_path)
base_name = apk_info["Package Name"]
new_base_name = get_new_packagename(replacement_file=replacement_file,
base_name=base_name)
if new_base_name is not None:
package_list[base_name] = new_base_name
package_and_version[new_base_name] = (int(apk_info["Version Code"]),
str(apk_info["Version Name"]))
else:
package_list[base_name] = base_name
package_and_version[base_name] = (int(apk_info["Version Code"]),
str(apk_info["Version Name"]))
print(Fore.GREEN + "Finished getting package names, version names and version codes.", end="\n\n")
retrieve_info(package_list=package_list,
package_and_version=package_and_version,
lang=lang,
metadata_dir=metadata_dir,
repo_dir=repo_dir,
force_metadata=force_metadata,
force_version=force_version,
force_screenshots=force_screenshots,
force_icons=force_icons,
dl_screenshots=download_screenshots,
data_file_content=data_file_content,
log_path=log_path,
cookie_path=cookie_path,
use_eng_name=use_eng_name)
else:
print(Fore.RED + "ERROR: We shouldn't have got here.")
exit(1)
def get_new_packagename(replacement_file: Optional[str],
base_name: str) -> Optional[str]:
if replacement_file is not None:
try:
replace_stream = open(replacement_file, encoding="utf_8", mode="r")
except UnicodeDecodeError as e:
print("ERROR: Decode error.", end="\n\n")
print(e, end="\n\n")
return None
except PermissionError as e:
print("ERROR: Couldn't open replacement file. Permission denied.", end="\n\n")
print(e, end="\n\n")
return None
try:
replacements = json.load(replace_stream)["Replacements"] # type: Dict[str, str]
except PermissionError as e:
print(Fore.RED + "ERROR: Couldn't read replacement file. Permission denied.", end="\n\n")
print(e, end="\n\n")
exit(1)
except json.decoder.JSONDecodeError as e:
print(Fore.RED + "ERROR: Couldn't load replacement file. Decoding error.", end="\n\n")
print(e, end="\n\n")
exit(1)
for term in replacements.keys():
search_term = term
replace_term = replacements[term]
if search_term in base_name:
base_name = base_name.replace(search_term, replace_term)
break
return base_name
else:
return None
def check_data_file(data_file_content) -> bool:
for key_name in ("Locales",
"Licenses",
"App_Categories",
"Game_Categories",
"Icon_Relations",
"Regex_Patterns",
"Sport_Category_Pattern"):
if data_file_content.get(key_name) is None or len(data_file_content[key_name]) == 0:
print(Fore.RED + "ERROR: \"{}\" key is missing or empty in the data file.".format(key_name), end="\n\n")
return False
if key_name == "Licenses":
if type(data_file_content.get(key_name)) is not list:
print(Fore.RED + "ERROR: \"{}\" key is wrong type, should be a list and currently it's a {}".format(
key_name, type(data_file_content.get(key_name))))
return False
elif type(data_file_content.get(key_name)) is not dict:
print(Fore.RED + "ERROR: \"{}\" key is wrong type, should be a dict and currently it's a {}".format(
key_name, type(data_file_content.get(key_name))))
return False
return True
def convert_apks_to_apk(apks_dir: str,
apk_editor_path: str,
sign_apk: bool,
key_file: str,
cert_file: str,
password: Optional[str],
build_tools_path: Optional[str]) -> None:
proc = False
for file in os.listdir(apks_dir):
if os.path.splitext(file)[1].lower() != ".apks":
continue
apks_path = os.path.join(apks_dir, file)
proc = True
renamer.convert_to_apk(apks_file=apks_path,
apk_editor_path=apk_editor_path,
sign_apk=sign_apk,
build_tools_path=build_tools_path,
key_file=key_file,
cert_file=cert_file,
certificate_password=password)
if proc:
print(Fore.GREEN + "Finished converting all APKS files.", end="\n\n")
else:
print(Fore.GREEN + "No APKS files were converted.", end="\n\n")
def map_apk_to_packagename(repo_dir: str) -> Dict:
mapped_apk_files = {}
for apk_file in os.listdir(repo_dir):
apk_file_path = os.path.join(repo_dir, apk_file)
if os.path.isfile(apk_file_path) and os.path.splitext(apk_file_path)[1].lower() == ".apk":
mapped_apk_files[renamer.get_info(apk_file_path)["Package Name"]] = apk_file
return mapped_apk_files
def get_version(package_content: dict,
package_and_version: Dict[str, Tuple[int, str]],
new_package: str,
force_metadata: bool,
force_version: bool) -> None:
if (package_content.get("CurrentVersionCode", "") == "" or package_content.get("CurrentVersionCode", "") == 0
or package_content.get("CurrentVersionCode", "") == 2147483647
or package_content.get("CurrentVersionCode") is None or force_metadata or force_version):
if package_and_version[new_package][0] is not None:
package_content["CurrentVersionCode"] = int(package_and_version[new_package][0])
else:
package_content["CurrentVersionCode"] = 0
if (package_content.get("CurrentVersion", "") == "" or package_content.get("CurrentVersion", "") == "0"
or package_content.get("CurrentVersion") is None or force_metadata or force_version):
if package_and_version[new_package][1] is not None:
package_content["CurrentVersion"] = str(package_and_version[new_package][1])
else:
package_content["CurrentVersion"] = "0"
def retrieve_info(package_list: Dict[str, str],
package_and_version: Dict[str, Tuple[int, str]],
lang: str,
metadata_dir: str,
repo_dir: str,
force_metadata: bool,
force_version: bool,
force_screenshots: bool,
force_icons: bool,
dl_screenshots: bool,
data_file_content: dict,
log_path: str,
cookie_path: Optional[str],
use_eng_name: bool) -> None:
proc = False
not_found_packages = []
authorname_not_found_packages = []
authoremail_not_found_packages = []
name_not_found_packages = []
website_not_found_packages = []
summary_not_found_packages = []
description_not_found_packages = []
category_not_found_packages = []
icon_not_found_packages = []
screenshots_not_found_packages = []
for pkg in package_list.keys():
package = pkg
new_package = package_list[pkg]
print(Fore.GREEN + "Processing " + package + "...", end="\n\n")
package_content = load_yml(metadata_dir=metadata_dir,
package=package)
if package_content is None:
continue
package_content_orig = copy.deepcopy(package_content)
metadata_exist = None
icons_exist = None
screenshots_exist = None
# If none of the force arguments is declared then check for available metadata, if screenshots
# should be downloaded then check if they exist, otherwise check only for the rest of the data
if dl_screenshots:
if not force_metadata and not force_screenshots and not force_icons:
metadata_exist = is_metadata_complete(package_content=package_content)
icons_exist = is_icon_complete(package=package,
version_code=package_and_version[new_package][0],
repo_dir=repo_dir,
data_file_content=data_file_content)
screenshots_exist = screenshot_exist(package=package,
repo_dir=repo_dir)
if metadata_exist and icons_exist and screenshots_exist:
if package_and_version[new_package][0] is None:
print(Fore.BLUE + "\tSkipping processing for the package as all the metadata"
" is complete in the YML file, and screenshots exist.", end="\n\n")
continue
else:
print(Fore.BLUE + "\tSkipping processing for the package as all the metadata is complete in "
"the YML file, all the icons are available and screenshots exist.",
end="\n\n")
continue
elif not force_metadata and not force_icons:
metadata_exist = is_metadata_complete(package_content=package_content)
icons_exist = is_icon_complete(package=package,
version_code=package_and_version[new_package][0],
repo_dir=repo_dir,
data_file_content=data_file_content)
if metadata_exist and icons_exist:
if package_and_version[new_package][0] is None:
print(Fore.BLUE + "\tSkipping processing for the package as all the metadata "
"is complete in the YML file.", end="\n\n")
continue
else:
print(Fore.BLUE + "\tSkipping processing for the package as all the metadata is complete in the "
"YML file and all the icons are available.", end="\n\n")
continue
if (force_version and not force_metadata and not force_screenshots and not force_icons and metadata_exist
and icons_exist):
if screenshots_exist is not None:
screenshots_exist = screenshot_exist(package=package,
repo_dir=repo_dir)
if screenshots_exist:
print(Fore.GREEN + "\tGetting version...", end="\n\n")
get_version(package_content=package_content,
package_and_version=package_and_version,
new_package=new_package,
force_metadata=force_metadata,
force_version=force_version)
print(Fore.GREEN + "\tFinished getting version for {}.".format(package), end="\n\n")
if package_content_orig != package_content:
write_yml(metadata_dir=metadata_dir,
package=package,
package_content=package_content)
continue
proc = True
resp_list = []
skip_package = False
store_name = None
for _ in [1]:
print(Fore.GREEN + "\tDownloading Play Store page...", end="\n\n")
if get_play_store_page(new_package=new_package,
resp_list=resp_list,
language=lang):
store_name = "Play_Store"
break
resp_list = []
print(Fore.GREEN + "\tDownloading Amazon Appstore page...", end="\n\n")
if get_amazon_page(resp_list=resp_list,
language=lang,
new_package=new_package,
cookie_path=cookie_path):
store_name = "Amazon_Store"
break
resp_list = []
print(Fore.GREEN + "\tDownloading Apkcombo page...", end="\n\n")
if get_apkcombo_page(resp_list=resp_list,
language=lang,
new_package=new_package,
data_file_content=data_file_content):
store_name = "Apkcombo_Store"
break
resp_list = []
not_found_packages.append(package)
get_version(package_content=package_content,
package_and_version=package_and_version,
new_package=new_package,
force_metadata=force_metadata,
force_version=force_version)
if package_content_orig != package_content:
write_yml(metadata_dir=metadata_dir,
package=package,
package_content=package_content)
print(Fore.GREEN + "Finished processing {}.".format(package), end="\n\n")
skip_package = True
if skip_package:
continue
resp = resp_list[0]
resp_int = resp_list[1]
print(Fore.GREEN + "\tExtracting information...", end="\n\n")
if not force_metadata:
if metadata_exist is None:
metadata_exist = is_metadata_complete(package_content=package_content)
if not metadata_exist:
get_metadata(package_content=package_content,
resp=resp,
resp_int=resp_int,
package=package,
name_not_found_packages=name_not_found_packages,
authorname_not_found_packages=authorname_not_found_packages,
authoremail_not_found_packages=authoremail_not_found_packages,
website_not_found_packages=website_not_found_packages,
category_not_found_packages=category_not_found_packages,
summary_not_found_packages=summary_not_found_packages,
description_not_found_packages=description_not_found_packages,
force_metadata=force_metadata,
data_file_content=data_file_content,
store_name=store_name,
use_eng_name=use_eng_name)
else:
get_metadata(package_content=package_content,
resp=resp,
resp_int=resp_int,
package=package,
name_not_found_packages=name_not_found_packages,
authorname_not_found_packages=authorname_not_found_packages,
authoremail_not_found_packages=authoremail_not_found_packages,
website_not_found_packages=website_not_found_packages,
category_not_found_packages=category_not_found_packages,
summary_not_found_packages=summary_not_found_packages,
description_not_found_packages=description_not_found_packages,
force_metadata=force_metadata,
data_file_content=data_file_content,
store_name=store_name,
use_eng_name=use_eng_name)
get_version(package_content=package_content,
package_and_version=package_and_version,
new_package=new_package,
force_metadata=force_metadata,
force_version=force_version)
print(Fore.GREEN + "\tFinished information extraction for {}.".format(package), end="\n\n")
if package_content_orig != package_content:
if not write_yml(metadata_dir=metadata_dir,
package=package,
package_content=package_content):
continue
if not force_icons and icons_exist is None:
icons_exist = is_icon_complete(package=package,
version_code=package_and_version[new_package][0],
repo_dir=repo_dir,
data_file_content=data_file_content)
if force_icons or not icons_exist:
print(Fore.GREEN + "\tDownloading icons...", end="\n\n")
# Function to download icons need to check force_icons because there might be cases where one of the icons
# is missing, with screenshots as long as there is at least one file we assume it's complete.
get_icon(resp_int=resp_int,
package=package,
new_package=new_package,
version_code=package_and_version[new_package][0],
repo_dir=repo_dir,
force_icons=force_icons,
data_file_content=data_file_content,
icon_not_found_packages=icon_not_found_packages,
store_name=store_name)
print(Fore.GREEN + "\tFinished downloading icons for {}.".format(package), end="\n\n")
else:
print(Fore.BLUE + "\tAll icon files for {} already exist, skipping...".format(package), end="\n\n")
if dl_screenshots:
if not force_screenshots and screenshots_exist is None:
screenshots_exist = screenshot_exist(package=package,
repo_dir=repo_dir)
if force_screenshots or not screenshots_exist:
get_screenshots(resp=resp,
repo_dir=repo_dir,
package=package,
new_package=new_package,
screenshots_not_found_packages=screenshots_not_found_packages,
data_file_content=data_file_content,
screenshots_exist=screenshots_exist,
store_name=store_name)
else:
print(Fore.BLUE + "\tScreenshots for {} already exists, skipping...".format(package), end="\n\n")
print(Fore.GREEN + "Finished processing {}.".format(package), end="\n\n")
if proc:
print(Fore.GREEN + "Everything done! Don't forget to run:")
print(Fore.CYAN + "\nfdroid rewritemeta\nfdroid update")
else:
print(Fore.GREEN + "Nothing was processed, no files changed.")
if len(not_found_packages) != 0:
print(Fore.YELLOW + "\nThese packages weren't found on any store:", end="\n\n")
for item in not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=not_found_packages, file_name="NotFound_Package", log_path=log_path)
if len(authorname_not_found_packages) != 0:
print(Fore.YELLOW + "\nThe AuthorName for these packages wasn't found:", end="\n\n")
for item in authorname_not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=authorname_not_found_packages, file_name="NotFound_AuthorName", log_path=log_path)
if len(authoremail_not_found_packages) != 0:
print(Fore.YELLOW + "\nThe AuthorName for these packages wasn't found:", end="\n\n")
for item in authoremail_not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=authoremail_not_found_packages, file_name="NotFound_AuthorEmail", log_path=log_path)
if len(website_not_found_packages) != 0:
print(Fore.YELLOW + "\nThe Website for these packages wasn't found:", end="\n\n")
for item in website_not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=website_not_found_packages, file_name="NotFound_Website", log_path=log_path)
if len(summary_not_found_packages) != 0:
print(Fore.YELLOW + "\nThe Summary for these packages wasn't found:", end="\n\n")
for item in summary_not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=summary_not_found_packages, file_name="NotFound_Summary", log_path=log_path)
if len(description_not_found_packages) != 0:
print(Fore.YELLOW + "\nThe Description for these packages wasn't found:", end="\n\n")
for item in description_not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=description_not_found_packages, file_name="NotFound_Description", log_path=log_path)
if len(category_not_found_packages) != 0:
print(Fore.YELLOW + "\nThe Category for these packages wasn't found:", end="\n\n")
for item in category_not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=category_not_found_packages, file_name="NotFound_Category", log_path=log_path)
if len(name_not_found_packages) != 0:
print(Fore.YELLOW + "\nThe Name for these packages wasn't found:", end="\n\n")
for item in name_not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=name_not_found_packages, file_name="NotFound_Name", log_path=log_path)
if len(icon_not_found_packages) != 0:
print(Fore.YELLOW + "\nThe icon URL for these packages wasn't found:", end="\n\n")
for item in icon_not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=icon_not_found_packages, file_name="NotFound_IconURL", log_path=log_path)
if len(screenshots_not_found_packages) != 0:
print(Fore.YELLOW + "\nThe screenshots URL for these packages weren't found:", end="\n\n")
for item in screenshots_not_found_packages:
print(Fore.YELLOW + item)
write_not_found_log(items=screenshots_not_found_packages,
file_name="NotFound_ScreenshotsURL",
log_path=log_path)
def get_metadata(package_content: dict,
resp: str,
resp_int: str,
package: str,
name_not_found_packages: list,
authorname_not_found_packages: list,
authoremail_not_found_packages: list,
website_not_found_packages: list,
category_not_found_packages: list,
summary_not_found_packages: list,
description_not_found_packages: list,
force_metadata: bool,
data_file_content: dict,
store_name: str,
use_eng_name: bool) -> None:
author_name_pattern = data_file_content["Regex_Patterns"][store_name]["author_name_pattern"]
author_email_pattern = data_file_content["Regex_Patterns"][store_name]["author_email_pattern"]
name_pattern = data_file_content["Regex_Patterns"][store_name]["name_pattern"]
website_pattern = data_file_content["Regex_Patterns"][store_name]["website_pattern"]
category_pattern = data_file_content["Regex_Patterns"][store_name]["category_pattern"]
summary_pattern = data_file_content["Regex_Patterns"][store_name]["summary_pattern"]
summary_pattern_alt = data_file_content["Regex_Patterns"][store_name]["summary_pattern_alt"]
description_pattern = data_file_content["Regex_Patterns"][store_name]["description_pattern"]
gitlab_repo_id_pattern = data_file_content["Regex_Patterns"][store_name]["gitlab_repo_id_pattern"]
ads_pattern = data_file_content["Regex_Patterns"][store_name]["ads_pattern"]
inapp_purchases_pattern = data_file_content["Regex_Patterns"][store_name]["inapp_purchases_pattern"]
tracking_pattern = data_file_content["Regex_Patterns"][store_name]["tracking_pattern"]
if name_pattern != "":
get_name(package_content=package_content,
name_pattern=name_pattern,
resp=resp,
resp_int=resp_int,
package=package,
name_not_found_packages=name_not_found_packages,
force_metadata=force_metadata,
use_eng_name=use_eng_name)