| 5 |
| from sys import argv |
| 6 | from typing import Union, Iterable |
| 7 |
| 8 | from pyrogram import Client, enums |
| 9 | from pyrogram.types import InputMediaDocument, Message |
| 10 |
| 11 | from release_caption import read_gradle_property, render_test_caption |
| 12 |
| 13 | api_id = 11535358 |
| 14 | api_hash = "33d372962fadb01df47e6ceed4e33cd6" |
| 15 | metadata_channel = -1001471208507 |
| 16 | metadata_channel_msg_id = 46 |
| 17 | artifacts_path = Path("artifacts") |
| 18 | test_version = len(argv) > 3 and argv[3] == "test" |
| 19 |
| 20 |
| 21 | def find_apk(abi: str) -> Path: |
| 22 | dirs = list(artifacts_path.glob("*")) |
| 23 | for dir in dirs: |
| 24 | if dir.is_dir(): |
| 25 | apks = list(dir.glob("*.apk")) |
| 26 | for apk in apks: |
| 27 | if abi in apk.name: |
| 28 | return apk |
| 29 |
| 30 |
| 31 | def get_thumb() -> str: |
| 32 | return "TMessagesProj/src/main/" + "ic_launcher_nagram_round_blue-playstore.png" |
| 33 |
| 34 |
| 35 | def get_caption() -> str: |
| 36 | with open(artifacts_path / "caption.txt", "r", encoding="utf-8") as f: |
| 37 | commit_message = f.read() |
| 38 | if not test_version: |
| 39 | return commit_message |
| 40 | repository_url = "/".join( |
| 41 | part.strip("/") |
| 42 | for part in (os.environ.get("GITHUB_SERVER_URL", ""), os.environ.get("GITHUB_REPOSITORY", "")) |
| 43 | if part |
| 44 | ) |
| 45 | return render_test_caption( |
| 46 | commit_message, |
| 47 | read_gradle_property("APP_VERSION_NAME"), |
| 48 | read_gradle_property("APP_VERSION_CODE"), |
| 49 | os.environ.get("GITHUB_SHA", ""), |
| 50 | repository_url, |
| 51 | ) |
| 52 |
| 53 |
| 54 | def get_document() -> list["InputMediaDocument"]: |
| 55 | documents = [] |
| 56 | abis = ["arm64-v8a", "armeabi-v7a"] |
| 57 | for abi in abis: |
| 58 | if apk := find_apk(abi): |
| 59 | documents.append( |
| 60 | InputMediaDocument( |
| 61 | media=str(apk), |
| 62 | thumb=get_thumb(), |
| 63 | ) |
| 64 | ) |
| 65 | documents[-1].caption = get_caption() |
| 66 | if test_version: |
| 67 | documents[-1].parse_mode = enums.ParseMode.HTML |
| 68 | return documents |
| 69 |
| 70 |
| 71 | def get_timestamp() -> int: |
| 72 | with open("gradle.properties", "r", encoding="utf-8") as f: |
| 73 | for line in f: |
| 74 | if line.startswith("APP_BUILD_TIMESTAMP="): |
| 75 | return int(line.replace("APP_BUILD_TIMESTAMP=", "").strip()) |
| 76 | raise FileNotFoundError |
| 77 |
| 78 |
| 79 | def retry(func): |
| 80 | async def wrapper(*args, **kwargs): |
| 81 | for _ in range(3): |
| 82 | try: |
| 83 | return await func(*args, **kwargs) |
| 84 | except Exception as e: |
| 85 | print(e) |
| 86 |
| 87 | return wrapper |
| 88 |
| 89 |
| 90 | @retry |
| 91 | async def send_to_channel(client: "Client", cid: str): |
| 92 | with contextlib.suppress(ValueError): |
| 93 | cid = int(cid) |
| 94 | return await client.send_media_group( |
| 95 | cid, |
| 96 | media=get_document(), |
| 97 | ) |
| 98 |
| 99 |
| 100 | @retry |
| 101 | async def forward_to_channel(client: "Client", msg: list["Message"]): |
| 102 | cid = msg[0].chat.id |
| 103 | msg_ids = [m.id for m in msg] |
| 104 | return await client.forward_messages( |
| 105 | metadata_channel, |
| 106 | cid, |
| 107 | msg_ids, |
| 108 | ) |
| 109 |
| 110 |
| 111 | @retry |
| 112 | async def edit_metadata_msg(client: "Client", msg: Union["Message", Iterable["Message"]], timestamp: int): |
| 113 | message = await client.get_messages(metadata_channel, metadata_channel_msg_id) |
| 114 | json_dict = json.loads(message.text.replace("#updatetest", "")) |
| 115 | abis = ["gcm", "nogcm"] |
| 116 | if not isinstance(msg, list): |
| 117 | v8a, v7a = msg.id, msg.id |
| 118 | elif len(msg) == 1: |
| 119 | v8a, v7a = msg[0].id, msg[0].id |
| 120 | else: |
| 121 | v8a, v7a = msg[0].id, msg[1].id |
| 122 | for abi in abis: |
| 123 | json_dict[abi] = {"armeabi-v7a":v7a,"arm64-v8a":v8a} |
| 124 | json_dict["timestamp"] = timestamp |
| 125 | json_text = json.dumps(json_dict) |
| 126 | await message.edit(f"#updatetest{json_text}") |
| 127 |
| 128 |
| 129 | def get_client(bot_token: str): |
| 130 | return Client( |
| 131 | "helper_bot", |
| 132 | api_id=api_id, |
| 133 | api_hash=api_hash, |
| 134 | bot_token=bot_token, |
| 135 | ) |
| 136 |
| 137 |
| 138 | async def main(): |
| 139 | timestamp = get_timestamp() |
| 140 | bot_token = argv[1] |
| 141 | chat_id = argv[2] |
| 142 | client = get_client(bot_token) |
| 143 | await client.start() |
| 144 | msg = await send_to_channel(client, chat_id) |
| 145 | msg = await forward_to_channel(client, msg) |
| 146 | await edit_metadata_msg(client, msg, timestamp) |
| 147 | await client.log_out() |
| 148 |
| 149 |
| 150 | if __name__ == "__main__": |
| 151 | from asyncio import run |
| 152 |
| 153 | run(main()) |
| 154 |