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