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