|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +from aiogram import Bot, types |
| 4 | +from aiogram.dispatcher import Dispatcher |
| 5 | +from aiogram.utils import executor |
| 6 | +from random import randint |
| 7 | +from threading import Thread |
| 8 | +import asyncio |
| 9 | +import itertools |
| 10 | +import os |
| 11 | +import sys |
| 12 | + |
| 13 | +PENDING_DURATION = 30 |
| 14 | +PENDING_MAX_SIZE = 20 |
| 15 | +LIST_MAX_SIZE = 30 |
| 16 | +TOKEN = os.environ['TELEGRAM_TOKEN'] |
| 17 | +ZMQ_CHANGES = sys.argv[1] |
| 18 | + |
| 19 | +bot = Bot(token=TOKEN) |
| 20 | + |
| 21 | +with open('chat_id.lst', 'r') as f: |
| 22 | + chat_ids = set([int(line) for line in f.readlines()]) |
| 23 | + |
| 24 | +dp = Dispatcher(bot) |
| 25 | + |
| 26 | +@dp.message_handler(commands=['start']) |
| 27 | +async def send_welcome(message: types.Message): |
| 28 | + chat_id = int(message.chat.id) |
| 29 | + if chat_id in chat_ids: |
| 30 | + return |
| 31 | + chat_ids.add(int(message.chat.id)) |
| 32 | + with open('chat_id.lst', 'w') as f: |
| 33 | + for i in chat_ids: |
| 34 | + f.write(str(i)+'\n') |
| 35 | + await message.reply("喵") |
| 36 | + |
| 37 | +@dp.message_handler(commands=['stop']) |
| 38 | +async def send_welcome(message: types.Message): |
| 39 | + chat_id = int(message.chat.id) |
| 40 | + if chat_id not in chat_ids: |
| 41 | + return |
| 42 | + chat_ids.remove(int(message.chat.id)) |
| 43 | + with open('chat_id.lst', 'w') as f: |
| 44 | + for i in chat_ids: |
| 45 | + f.write(str(i)+'\n') |
| 46 | + await message.reply("发不出声音了") |
| 47 | + |
| 48 | +@dp.message_handler(commands=['ping']) |
| 49 | +async def send_echo(message: types.Message): |
| 50 | + await bot.send_chat_action(message.chat.id, action=types.ChatActions.TYPING) |
| 51 | + |
| 52 | +import zmq |
| 53 | +import zmq.asyncio |
| 54 | +ctx = zmq.asyncio.Context.instance() |
| 55 | +s = ctx.socket(zmq.SUB) |
| 56 | +s.connect(ZMQ_CHANGES) |
| 57 | +s.subscribe(b'') |
| 58 | + |
| 59 | +def classify(pending_list: list): |
| 60 | + msg = '' |
| 61 | + def get_header(p): |
| 62 | + comp = p['comp'] |
| 63 | + arch = p['arch'] |
| 64 | + return f'<b>{comp}</b> {arch}\n' |
| 65 | + pending_list.sort(key=get_header) |
| 66 | + for header, g in itertools.groupby(pending_list, key=get_header): |
| 67 | + entries = list(g) |
| 68 | + msg += header |
| 69 | + preferred_order = ['delete', 'new', 'overwrite', 'upgrade'] |
| 70 | + entries.sort(key=lambda x: (preferred_order.index(x['method']), x['pkg'])) |
| 71 | + too_long = len(entries) > LIST_MAX_SIZE |
| 72 | + for p in entries if not too_long else entries[:LIST_MAX_SIZE]: |
| 73 | + pkg = p['pkg'] |
| 74 | + to_ver = p['to_ver'] |
| 75 | + from_ver = p['from_ver'] |
| 76 | + method = p['method'] |
| 77 | + if method == 'upgrade': |
| 78 | + msg += f'<code> ^</code> <a href="https://packages.aosc.io/packages/{pkg}">{pkg}</a> <code>{from_ver}</code> ⇒ <code>{to_ver}</code>\n' |
| 79 | + if method == 'new': |
| 80 | + msg += f'<code> +</code> <a href="https://packages.aosc.io/packages/{pkg}">{pkg}</a> <code>{to_ver}</code>\n' |
| 81 | + if method == 'delete': |
| 82 | + msg += f'<code> -</code> <a href="https://packages.aosc.io/packages/{pkg}">{pkg}</a> <code>{from_ver}</code>\n' |
| 83 | + if method == 'overwrite': |
| 84 | + msg += f'<code> *</code> <a href="https://packages.aosc.io/packages/{pkg}">{pkg}</a> <code>{from_ver}</code>\n' |
| 85 | + if too_long: |
| 86 | + remain = len(entries) - LIST_MAX_SIZE |
| 87 | + msg += f'<i>and {remain} more...</i>\n' |
| 88 | + msg += '\n' |
| 89 | + print(msg) |
| 90 | + return msg[:-1] |
| 91 | + |
| 92 | +async def co(): |
| 93 | + pending_list = [] |
| 94 | + while True: |
| 95 | + try: |
| 96 | + message = await asyncio.wait_for(s.recv_json(), timeout=PENDING_DURATION) |
| 97 | + print(message) |
| 98 | + pending_list.append(message) |
| 99 | + if len(pending_list) > PENDING_MAX_SIZE: |
| 100 | + raise asyncio.TimeoutError() |
| 101 | + except asyncio.TimeoutError: |
| 102 | + if len(pending_list) > 0: |
| 103 | + print('send', len(pending_list)) |
| 104 | + for chat_id in [-1001433534622]: #chat_ids: |
| 105 | + await bot.send_message(chat_id, classify(pending_list), |
| 106 | + parse_mode='HTML', |
| 107 | + disable_web_page_preview=True) |
| 108 | + pending_list = [] |
| 109 | + |
| 110 | +asyncio.ensure_future(co(), loop=dp.loop) |
| 111 | + |
| 112 | +if __name__ == '__main__': |
| 113 | + executor.start_polling(dp) |
| 114 | + |
0 commit comments