Skip to content

Commit 760d94b

Browse files
Kavutiiflare3g
authored andcommitted
Introduced the Hashtag management to mark the URL resources in the message and in the log channel
1 parent 2b23911 commit 760d94b

1 file changed

Lines changed: 31 additions & 4 deletions

File tree

tg_bot/modules/log_channel.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
FILENAME = __name__.rsplit(".", 1)[-1]
66

77
if is_module_loaded(FILENAME):
8-
from telegram import Bot, Update, ParseMode, Message, Chat
8+
from telegram import Bot, Update, ParseMode, Message, Chat, MessageEntity
99
from telegram.error import BadRequest, Unauthorized
1010
from telegram.ext import CommandHandler, MessageHandler, run_async
1111
from telegram.utils.helpers import escape_markdown
@@ -15,6 +15,7 @@
1515
from tg_bot.modules.helper_funcs.chat_status import user_admin
1616
from tg_bot.modules.sql import log_channel_sql as sql
1717
import requests
18+
import tldextract
1819

1920
def loggable(func):
2021
@wraps(func)
@@ -49,19 +50,37 @@ def log_action(bot: Bot, update: Update, *args, **kwargs):
4950
def log_resource(bot: Bot, update: Update):
5051
entities = update.effective_message.parse_entities()
5152
caption_entities = update.effective_message.parse_caption_entities()
53+
LOGGER.info(entities)
5254
chat = update.effective_chat # type: Optional[Chat]
5355
log_chat = sql.get_chat_log_channel(chat.id)
56+
57+
url = None
58+
tags = []
59+
5460
if log_chat:
55-
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+
5667
for descriptor, entity in entities.items():
5768
result = (
5869
f"<b>Risorsa inviata da @{update.effective_user.username}:</b>\n"
5970
)
60-
if descriptor["type"] in ["url", "text_link"]:
71+
if descriptor["type"] == MessageEntity.URL:
72+
extracted = tldextract.extract(entity)
73+
if f"#{extracted.domain}" not in tags:
74+
tags.append(f"#{extracted.domain}")
75+
tags.sort()
76+
if descriptor["type"] in [MessageEntity.URL, MessageEntity.TEXT_LINK]:
6177
try:
6278
response = requests.get(entity)
6379
if response.status_code == requests.codes.ok:
6480
result += f"{entity}"
81+
if tags:
82+
tags_string = " ".join(tags)
83+
result += f"\n\nTags:\n{tags_string}"
6584
send_log(bot, log_chat, chat.id, result)
6685
except Exception as e:
6786
LOGGER.info(f"Resource {entity} is not a valid url")
@@ -71,11 +90,19 @@ def log_resource(bot: Bot, update: Update):
7190
result = (
7291
"<b>Risorsa inviata da @{update.effective_user.username}:</b>\n"
7392
)
74-
if descriptor["type"] in ["url", "text_link"]:
93+
if descriptor["type"] == MessageEntity.URL:
94+
extracted = tldextract.extract(entity)
95+
if f"#{extracted.domain}" not in tags:
96+
tags.append(f"#{extracted.domain}")
97+
tags.sort()
98+
if descriptor["type"] in [MessageEntity.URL, MessageEntity.TEXT_LINK]:
7599
try:
76100
response = requests.get(entity)
77101
if response.status_code == requests.codes.ok:
78102
result += f"{entity}"
103+
if tags:
104+
tags_string = " ".join(tags)
105+
result += f"\n\nTags:\n{tags_string}"
79106
send_log(bot, log_chat, chat.id, result)
80107
except Exception as e:
81108
LOGGER.info(f"Resource {entity} is not a valid url")

0 commit comments

Comments
 (0)