| 5 |
| from typing import Union, Iterable |
| 6 |
| 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 | test_version = argv[3] == "test" if len(argv) > 2 else None |
| 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 | pre = "Test version, " if test_version else "" |
| 34 | with open(artifacts_path / "caption.txt", "r", encoding="utf-8") as f: |
| 35 | return pre + f.read() |
| 36 |
| 37 |
| 38 | def get_document() -> list["InputMediaDocument"]: |
| 39 | documents = [] |
| 40 | abis = ["arm64-v8a", "armeabi-v7a"] |
| 41 | for abi in abis: |
| 42 | if apk := find_apk(abi): |
| 43 | documents.append( |
| 44 | InputMediaDocument( |
| 45 | media=str(apk), |
| 46 | thumb=get_thumb(), |
| 47 | ) |
| 48 | ) |
| 49 | documents[-1].caption = get_caption() |
| 50 | return documents |
| 51 |
| 52 |
| 53 | def get_timestamp() -> int: |
| 54 | with open("gradle.properties", "r", encoding="utf-8") as f: |
| 55 | for line in f: |
| 56 | if line.startswith("APP_BUILD_TIMESTAMP="): |
| 57 | return int(line.replace("APP_BUILD_TIMESTAMP=", "").strip()) |
| 58 | raise FileNotFoundError |
| 59 |
| 60 |
| 61 | def retry(func): |
| 62 | async def wrapper(*args, **kwargs): |
| 63 | for _ in range(3): |
| 64 | try: |
| 65 | return await func(*args, **kwargs) |
| 66 | except Exception as e: |
| 67 | print(e) |
| 68 |
| 69 | return wrapper |
| 70 |
| 71 |
| 72 | @retry |
| 73 | async def send_to_channel(client: "Client", cid: str): |
| 74 | with contextlib.suppress(ValueError): |
| 75 | cid = int(cid) |
| 76 | return await client.send_media_group( |
| 77 | cid, |
| 78 | media=get_document(), |
| 79 | ) |
| 80 |
| 81 |
| 82 | @retry |
| 83 | async def forward_to_channel(client: "Client", msg: list["Message"]): |
| 84 | cid = msg[0].chat.id |
| 85 | msg_ids = [m.id for m in msg] |
| 86 | return await client.forward_messages( |
| 87 | metadata_channel, |
| 88 | cid, |
| 89 | msg_ids, |
| 90 | ) |
| 91 |
| 92 |
| 93 | @retry |
| 94 | async def edit_metadata_msg(client: "Client", msg: Union["Message", Iterable["Message"]], timestamp: int): |
| 95 | message = await client.get_messages(metadata_channel, metadata_channel_msg_id) |
| 96 | json_dict = json.loads(message.text.replace("#updatetest", "")) |
| 97 | abis = ["gcm", "nogcm"] |
| 98 | if not isinstance(msg, list): |
| 99 | v8a, v7a = msg.id, msg.id |
| 100 | elif len(msg) == 1: |
| 101 | v8a, v7a = msg[0].id, msg[0].id |
| 102 | else: |
| 103 | v8a, v7a = msg[0].id, msg[1].id |
| 104 | for abi in abis: |
| 105 | json_dict[abi] = {"armeabi-v7a":v7a,"arm64-v8a":v8a} |
| 106 | json_dict["timestamp"] = timestamp |
| 107 | json_text = json.dumps(json_dict) |
| 108 | await message.edit(f"#updatetest{json_text}") |
| 109 |
| 110 |
| 111 | def get_client(bot_token: str): |
| 112 | return Client( |
| 113 | "helper_bot", |
| 114 | api_id=api_id, |
| 115 | api_hash=api_hash, |
| 116 | bot_token=bot_token, |
| 117 | ) |
| 118 |
| 119 |
| 120 | async def main(): |
| 121 | timestamp = get_timestamp() |
| 122 | bot_token = argv[1] |
| 123 | chat_id = argv[2] |
| 124 | client = get_client(bot_token) |
| 125 | await client.start() |
| 126 | msg = await send_to_channel(client, chat_id) |
| 127 | msg = await forward_to_channel(client, msg) |
| 128 | await edit_metadata_msg(client, msg, timestamp) |
| 129 | await client.log_out() |
| 130 |
| 131 |
| 132 | if __name__ == "__main__": |
| 133 | from asyncio import run |
| 134 |
| 135 | run(main()) |
| 136 |