| 5 |
| 6 | from apk_version import read_apk_version |
| 7 | from pyrogram import Client |
| 8 | from pyrogram.types import InputMediaDocument, Message |
| 9 |
| 10 | api_id = 11535358 |
| 11 | api_hash = "33d372962fadb01df47e6ceed4e33cd6" |
| 12 | metadata_channel = -1001471208507 |
| 13 | metadata_channel_msg_id = 46 |
| 14 | artifacts_path = Path("artifacts") |
| 15 |
| 16 |
| 17 | def find_apk(abi: str) -> Path: |
| 18 | dirs = list(artifacts_path.glob("*")) |
| 19 | for dir in dirs: |
| 20 | if dir.is_dir(): |
| 21 | apks = list(dir.glob("*.apk")) |
| 22 | for apk in apks: |
| 23 | if abi in apk.name: |
| 24 | return apk |
| 25 |
| 26 |
| 27 | def get_thumb() -> str: |
| 28 | return "TMessagesProj/src/main/" + "ic_launcher_nagram_round_blue-playstore.png" |
| 29 |
| 30 |
| 31 | def get_document() -> list["InputMediaDocument"]: |
| 32 | documents = [] |
| 33 | abis = ["arm64-v8a", "armeabi-v7a"] |
| 34 | for abi in abis: |
| 35 | if apk := find_apk(abi): |
| 36 | documents.append( |
| 37 | InputMediaDocument( |
| 38 | media=str(apk), |
| 39 | thumb=get_thumb(), |
| 40 | ) |
| 41 | ) |
| 42 | return documents |
| 43 |
| 44 |
| 45 | def get_timestamp() -> int: |
| 46 | with open("gradle.properties", "r", encoding="utf-8") as f: |
| 47 | for line in f: |
| 48 | if line.startswith("APP_BUILD_TIMESTAMP="): |
| 49 | return int(line.replace("APP_BUILD_TIMESTAMP=", "").strip()) |
| 50 | raise FileNotFoundError |
| 51 |
| 52 |
| 53 | def get_version() -> tuple[str, int]: |
| 54 | apk = find_apk("arm64-v8a") or find_apk("armeabi-v7a") |
| 55 | if apk is None: |
| 56 | raise FileNotFoundError("No supported APK found") |
| 57 | return read_apk_version(apk) |
| 58 |
| 59 |
| 60 | def retry(func): |
| 61 | async def wrapper(*args, **kwargs): |
| 62 | for _ in range(3): |
| 63 | try: |
| 64 | return await func(*args, **kwargs) |
| 65 | except Exception as e: |
| 66 | print(e) |
| 67 |
| 68 | return wrapper |
| 69 |
| 70 |
| 71 | # The in-app updater reads one document message per ABI from the metadata |
| 72 | # channel itself, so the APKs are sent there apart from the channel posts. |
| 73 | @retry |
| 74 | async def send_to_metadata_channel(client: "Client"): |
| 75 | return await client.send_media_group( |
| 76 | metadata_channel, |
| 77 | media=get_document(), |
| 78 | ) |
| 79 |
| 80 |
| 81 | @retry |
| 82 | async def edit_metadata_msg( |
| 83 | client: "Client", msg: Union["Message", Iterable["Message"]], timestamp: int |
| 84 | ): |
| 85 | message = await client.get_messages(metadata_channel, metadata_channel_msg_id) |
| 86 | json_dict = json.loads(message.text.replace("#updatetest", "")) |
| 87 | version_name, version_code = get_version() |
| 88 | abis = ["gcm", "nogcm"] |
| 89 | if not isinstance(msg, list): |
| 90 | v8a, v7a = msg.id, msg.id |
| 91 | elif len(msg) == 1: |
| 92 | v8a, v7a = msg[0].id, msg[0].id |
| 93 | else: |
| 94 | v8a, v7a = msg[0].id, msg[1].id |
| 95 | for abi in abis: |
| 96 | json_dict[abi] = {"armeabi-v7a": v7a, "arm64-v8a": v8a} |
| 97 | json_dict["version"] = f"{version_name} ({version_code})" |
| 98 | json_dict["version_code"] = version_code |
| 99 | json_dict["timestamp"] = timestamp |
| 100 | json_text = json.dumps(json_dict) |
| 101 | await message.edit(f"#updatetest{json_text}") |
| 102 |
| 103 |
| 104 | def get_client(bot_token: str): |
| 105 | return Client( |
| 106 | "helper_bot", |
| 107 | api_id=api_id, |
| 108 | api_hash=api_hash, |
| 109 | bot_token=bot_token, |
| 110 | ) |
| 111 |
| 112 |
| 113 | async def main(): |
| 114 | timestamp = get_timestamp() |
| 115 | bot_token = argv[1] |
| 116 | client = get_client(bot_token) |
| 117 | await client.start() |
| 118 | msg = await send_to_metadata_channel(client) |
| 119 | await edit_metadata_msg(client, msg, timestamp) |
| 120 | await client.log_out() |
| 121 |
| 122 |
| 123 | if __name__ == "__main__": |
| 124 | from asyncio import run |
| 125 |
| 126 | run(main()) |
| 127 |