|
5 | 5 | FILENAME = __name__.rsplit(".", 1)[-1] |
6 | 6 |
|
7 | 7 | if is_module_loaded(FILENAME): |
8 | | - from telegram import Bot, Update, ParseMode, Message, Chat |
| 8 | + from telegram import Bot, Update, ParseMode, Message, Chat, MessageEntity |
9 | 9 | from telegram.error import BadRequest, Unauthorized |
10 | 10 | from telegram.ext import CommandHandler, MessageHandler, run_async |
11 | 11 | from telegram.utils.helpers import escape_markdown |
|
14 | 14 | from tg_bot import dispatcher, LOGGER |
15 | 15 | from tg_bot.modules.helper_funcs.chat_status import user_admin |
16 | 16 | from tg_bot.modules.sql import log_channel_sql as sql |
| 17 | + import requests |
| 18 | + import tldextract |
17 | 19 |
|
18 | 20 | def loggable(func): |
19 | 21 | @wraps(func) |
@@ -48,29 +50,94 @@ def log_action(bot: Bot, update: Update, *args, **kwargs): |
48 | 50 | def log_resource(bot: Bot, update: Update): |
49 | 51 | entities = update.effective_message.parse_entities() |
50 | 52 | caption_entities = update.effective_message.parse_caption_entities() |
| 53 | + LOGGER.info(entities) |
51 | 54 | chat = update.effective_chat # type: Optional[Chat] |
52 | 55 | log_chat = sql.get_chat_log_channel(chat.id) |
| 56 | + |
| 57 | + url = None |
| 58 | + tags = [] |
| 59 | + |
53 | 60 | if log_chat: |
54 | | - result = f"<b>Risorsa inviata da @{update.effective_user.username}:</b>\n" |
| 61 | + for descriptor, entity in entities.items(): |
| 62 | + if descriptor["type"] == MessageEntity.HASHTAG: |
| 63 | + tags.append(entity) |
| 64 | + if tags: |
| 65 | + tags.sort() |
| 66 | + |
55 | 67 | for descriptor, entity in entities.items(): |
56 | 68 | result = ( |
57 | 69 | f"<b>Risorsa inviata da @{update.effective_user.username}:</b>\n" |
58 | 70 | ) |
59 | | - if descriptor["type"] in ["url", "text_link"]: |
60 | | - result += f"{entity}" |
61 | | - LOGGER.debug(f"Found message entity: {descriptor['type']} {entity}") |
62 | | - send_log(bot, log_chat, chat.id, result) |
| 71 | + |
| 72 | + if descriptor["type"] == MessageEntity.URL: |
| 73 | + try: |
| 74 | + response = requests.get(entity) |
| 75 | + if response.status_code == requests.codes.ok: |
| 76 | + result += f"{entity}" |
| 77 | + extracted = tldextract.extract(entity) |
| 78 | + if f"#{extracted.domain}" not in tags: |
| 79 | + tags.append(f"#{extracted.domain}") |
| 80 | + tags.sort() |
| 81 | + if tags: |
| 82 | + tags_string = " ".join(tags) |
| 83 | + result += f"\n\nTags:\n{tags_string}" |
| 84 | + send_log(bot, log_chat, chat.id, result) |
| 85 | + except Exception as e: |
| 86 | + LOGGER.info(f"Resource {entity} is not a valid url") |
| 87 | + LOGGER.error(e) |
| 88 | + elif descriptor["type"] == MessageEntity.TEXT_LINK: |
| 89 | + try: |
| 90 | + response = requests.get(descriptor["url"]) |
| 91 | + if response.status_code == requests.codes.ok: |
| 92 | + result += f'{descriptor["url"]}' |
| 93 | + extracted = tldextract.extract(descriptor["url"]) |
| 94 | + if f"#{extracted.domain}" not in tags: |
| 95 | + tags.append(f"#{extracted.domain}") |
| 96 | + tags.sort() |
| 97 | + if tags: |
| 98 | + tags_string = " ".join(tags) |
| 99 | + result += f"\n\nTags:\n{tags_string}" |
| 100 | + send_log(bot, log_chat, chat.id, result) |
| 101 | + except Exception as e: |
| 102 | + LOGGER.info(f"Resource {entity} is not a valid url") |
| 103 | + LOGGER.error(e) |
63 | 104 |
|
64 | 105 | for descriptor, entity in caption_entities.items(): |
65 | 106 | result = ( |
66 | 107 | "<b>Risorsa inviata da @{update.effective_user.username}:</b>\n" |
67 | 108 | ) |
68 | | - if descriptor["type"] in ["url", "text_link"]: |
69 | | - result += f"{entity}" |
70 | | - LOGGER.debug(f"Found message entity: {descriptor['type']} {entity}") |
71 | | - send_log(bot, log_chat, chat.id, result) |
72 | | - else: |
73 | | - send_log(bot, log_chat, chat.id, result) |
| 109 | + if descriptor["type"] == MessageEntity.URL: |
| 110 | + try: |
| 111 | + response = requests.get(entity) |
| 112 | + if response.status_code == requests.codes.ok: |
| 113 | + result += f"{entity}" |
| 114 | + extracted = tldextract.extract(entity) |
| 115 | + if f"#{extracted.domain}" not in tags: |
| 116 | + tags.append(f"#{extracted.domain}") |
| 117 | + tags.sort() |
| 118 | + if tags: |
| 119 | + tags_string = " ".join(tags) |
| 120 | + result += f"\n\nTags:\n{tags_string}" |
| 121 | + send_log(bot, log_chat, chat.id, result) |
| 122 | + except Exception as e: |
| 123 | + LOGGER.info(f"Resource {entity} is not a valid url") |
| 124 | + LOGGER.error(e) |
| 125 | + elif descriptor["type"] == MessageEntity.TEXT_LINK: |
| 126 | + try: |
| 127 | + response = requests.get(descriptor["url"]) |
| 128 | + if response.status_code == requests.codes.ok: |
| 129 | + result += f'{descriptor["url"]}' |
| 130 | + extracted = tldextract.extract(descriptor["url"]) |
| 131 | + if f"#{extracted.domain}" not in tags: |
| 132 | + tags.append(f"#{extracted.domain}") |
| 133 | + tags.sort() |
| 134 | + if tags: |
| 135 | + tags_string = " ".join(tags) |
| 136 | + result += f"\n\nTags:\n{tags_string}" |
| 137 | + send_log(bot, log_chat, chat.id, result) |
| 138 | + except Exception as e: |
| 139 | + LOGGER.info(f"Resource {entity} is not a valid url") |
| 140 | + LOGGER.error(e) |
74 | 141 |
|
75 | 142 | def send_log(bot: Bot, log_chat_id: str, orig_chat_id: str, result: str): |
76 | 143 | try: |
@@ -207,7 +274,12 @@ def __chat_settings__(chat_id, user_id): |
207 | 274 | UNSET_LOG_HANDLER = CommandHandler("unsetlog", unsetlog) |
208 | 275 |
|
209 | 276 | LOG_RESOURCES_HANDLER = MessageHandler( |
210 | | - (Filters.entity("url") | Filters.entity("text_link")), log_resource |
| 277 | + ( |
| 278 | + Filters.entity("url") |
| 279 | + | Filters.entity("text_link") |
| 280 | + | Filters.entity("hashtag") |
| 281 | + ), |
| 282 | + log_resource, |
211 | 283 | ) |
212 | 284 |
|
213 | 285 | dispatcher.add_handler(LOG_HANDLER) |
|
0 commit comments