|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import sys |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +from justapk import __version__ |
| 9 | +from justapk.downloader import APKDownloader |
| 10 | +from justapk.sources import SOURCE_PRIORITY |
| 11 | + |
| 12 | + |
| 13 | +def main(argv: list[str] | None = None) -> int: |
| 14 | + parser = argparse.ArgumentParser( |
| 15 | + prog="justapk", |
| 16 | + description="Multi-source APK downloader", |
| 17 | + ) |
| 18 | + parser.add_argument("-V", "--version", action="version", version=f"justapk {__version__}") |
| 19 | + sub = parser.add_subparsers(dest="command") |
| 20 | + |
| 21 | + # download |
| 22 | + dl = sub.add_parser("download", help="Download APK by package name") |
| 23 | + dl.add_argument("package", help="Android package name (e.g. org.telegram.messenger)") |
| 24 | + dl.add_argument("-s", "--source", choices=SOURCE_PRIORITY, help="Use specific source") |
| 25 | + dl.add_argument("-o", "--output", type=Path, default=Path("."), help="Output directory") |
| 26 | + dl.add_argument("-v", "--app-version", dest="app_version", help="Specific version to download") |
| 27 | + dl.add_argument( |
| 28 | + "--no-convert", action="store_true", |
| 29 | + help="Do not auto-convert XAPK/split APK to APK", |
| 30 | + ) |
| 31 | + |
| 32 | + # search |
| 33 | + sr = sub.add_parser("search", help="Search for apps") |
| 34 | + sr.add_argument("query", help="Search query") |
| 35 | + sr.add_argument("-s", "--source", choices=SOURCE_PRIORITY, help="Search in specific source") |
| 36 | + |
| 37 | + # info |
| 38 | + inf = sub.add_parser("info", help="Get app metadata") |
| 39 | + inf.add_argument("package", help="Android package name") |
| 40 | + inf.add_argument("-s", "--source", choices=SOURCE_PRIORITY, help="Use specific source") |
| 41 | + |
| 42 | + # convert |
| 43 | + cv = sub.add_parser("convert", help="Convert XAPK/split APK to single APK") |
| 44 | + cv.add_argument("file", type=Path, help="Path to .xapk or .apks file") |
| 45 | + cv.add_argument("-o", "--output", type=Path, help="Output directory (default: same as input)") |
| 46 | + |
| 47 | + # sources |
| 48 | + sub.add_parser("sources", help="List available sources") |
| 49 | + |
| 50 | + args = parser.parse_args(argv) |
| 51 | + |
| 52 | + if not args.command: |
| 53 | + parser.print_help() |
| 54 | + return 1 |
| 55 | + |
| 56 | + try: |
| 57 | + if args.command == "sources": |
| 58 | + return _cmd_sources() |
| 59 | + elif args.command == "download": |
| 60 | + return _cmd_download(args) |
| 61 | + elif args.command == "search": |
| 62 | + return _cmd_search(args) |
| 63 | + elif args.command == "info": |
| 64 | + return _cmd_info(args) |
| 65 | + elif args.command == "convert": |
| 66 | + return _cmd_convert(args) |
| 67 | + except KeyboardInterrupt: |
| 68 | + sys.stderr.write("\nInterrupted.\n") |
| 69 | + return 130 |
| 70 | + except Exception as e: |
| 71 | + sys.stderr.write(f"Error: {e}\n") |
| 72 | + |
| 73 | + return 1 |
| 74 | + |
| 75 | + |
| 76 | +def _cmd_sources() -> int: |
| 77 | + data = [{"name": name, "priority": i + 1} for i, name in enumerate(SOURCE_PRIORITY)] |
| 78 | + print(json.dumps(data, indent=2)) |
| 79 | + return 0 |
| 80 | + |
| 81 | + |
| 82 | +def _cmd_download(args) -> int: |
| 83 | + dl = APKDownloader(auto_convert_xapk=not args.no_convert) |
| 84 | + result = dl.download( |
| 85 | + package=args.package, |
| 86 | + output_dir=args.output, |
| 87 | + source=args.source, |
| 88 | + version=args.app_version, |
| 89 | + ) |
| 90 | + print(json.dumps(result.to_dict(), indent=2)) |
| 91 | + return 0 |
| 92 | + |
| 93 | + |
| 94 | +def _cmd_search(args) -> int: |
| 95 | + dl = APKDownloader() |
| 96 | + results = dl.search(args.query, source=args.source) |
| 97 | + data = [r.to_dict() for r in results] |
| 98 | + print(json.dumps(data, indent=2)) |
| 99 | + return 0 |
| 100 | + |
| 101 | + |
| 102 | +def _cmd_convert(args) -> int: |
| 103 | + from justapk.utils import sha256_file |
| 104 | + from justapk.xapk import convert_xapk_to_apk |
| 105 | + |
| 106 | + xapk = args.file |
| 107 | + if not xapk.exists(): |
| 108 | + sys.stderr.write(f"File not found: {xapk}\n") |
| 109 | + return 1 |
| 110 | + if xapk.suffix.lower() not in (".xapk", ".apks"): |
| 111 | + sys.stderr.write(f"Not an XAPK/APKS file: {xapk}\n") |
| 112 | + return 1 |
| 113 | + |
| 114 | + out_dir = args.output or xapk.parent |
| 115 | + apk_path = convert_xapk_to_apk(xapk, out_dir) |
| 116 | + print(json.dumps({ |
| 117 | + "path": str(apk_path), |
| 118 | + "size": apk_path.stat().st_size, |
| 119 | + "sha256": sha256_file(apk_path), |
| 120 | + }, indent=2)) |
| 121 | + return 0 |
| 122 | + |
| 123 | + |
| 124 | +def _cmd_info(args) -> int: |
| 125 | + dl = APKDownloader() |
| 126 | + result = dl.info(args.package, source=args.source) |
| 127 | + if result: |
| 128 | + print(json.dumps(result.to_dict(), indent=2)) |
| 129 | + return 0 |
| 130 | + else: |
| 131 | + sys.stderr.write(f"No info found for: {args.package}\n") |
| 132 | + return 1 |
0 commit comments