|
1 | 1 | #!/usr/bin/python3 |
2 | 2 | # -*- coding: utf-8 -*- |
| 3 | +import logging |
3 | 4 | from time import sleep |
4 | 5 |
|
5 | 6 | from tg_ids import DC_GROUP_CHATID |
6 | 7 |
|
7 | 8 | from pony.orm import db_session, select |
8 | 9 | from telegram import Update |
9 | | -from telegram.ext import Updater, CallbackContext |
| 10 | +from telegram.ext import CallbackContext |
10 | 11 |
|
11 | 12 | from models import Listable |
12 | 13 |
|
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
13 | 16 |
|
14 | 17 | def update_group_url(context: CallbackContext, chat_id: str) -> (str, str, bool): |
15 | 18 | try: |
16 | 19 | url = context.bot.export_chat_invite_link(chat_id=chat_id) |
17 | 20 | return chat_id, url, True # too GO-like huh? |
18 | | - except: # TODO: filter excepts |
| 21 | + except Exception: |
| 22 | + logger.error(f"Could not create invite link for {chat_id}", exc_info=True) |
19 | 23 | return None, None, False # too GO-like huh? |
20 | 24 |
|
21 | 25 |
|
22 | 26 | def update_groups(context: CallbackContext): |
| 27 | + logger.info("Starting update_groups job") |
23 | 28 | with db_session: |
24 | 29 | chats = list(select((l.id, l.chat_id, l.name) for l in Listable if l.validated)) |
25 | | - for id, (chat_id, url, validated), name in [(id,update_group_url(context, chat_id), name) for id, chat_id, name in |
| 30 | + logger.info(f"Found {len(chats)} groups to update") |
| 31 | + |
| 32 | + for id, (chat_id, url, validated), name in [(id, update_group_url(context, chat_id), name) for id, chat_id, name in |
26 | 33 | chats]: |
27 | 34 | sleep(1) |
28 | 35 | if not validated: |
| 36 | + logger.warning(f"Failed to update URL for group '{name}'. De-validating.") |
29 | 37 | with db_session: |
30 | 38 | Listable[id].validated = False |
31 | 39 | context.bot.send_message(chat_id=DC_GROUP_CHATID, text=f"El grupo {name} murió 💀") |
32 | 40 | else: |
| 41 | + logger.info(f"Updating URL for group '{name}'") |
33 | 42 | with db_session: |
34 | 43 | Listable[id].url = url |
| 44 | + logger.info("Finished update_groups job") |
35 | 45 |
|
36 | 46 |
|
37 | 47 | def actualizar_grupos(update: Update, context: CallbackContext): |
| 48 | + logger.info(f"Manual update of groups triggered by {update.effective_user.id}") |
| 49 | + update.message.reply_text("Actualizando grupos...") |
38 | 50 | update_groups(context) |
| 51 | + update.message.reply_text("¡Grupos actualizados!") |
0 commit comments