Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tg_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

# enable logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.DEBUG
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
)

LOGGER = logging.getLogger(__name__)
Expand Down
2 changes: 1 addition & 1 deletion tg_bot/deactivated-modules
Original file line number Diff line number Diff line change
@@ -1 +1 @@
NO_LOAD = ['translation','rss', 'sed', 'afk','notes','filters']
NO_LOAD = ['translation','rss', 'sed', 'afk','notes','filters', 'spam']
98 changes: 85 additions & 13 deletions tg_bot/modules/log_channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
FILENAME = __name__.rsplit(".", 1)[-1]

if is_module_loaded(FILENAME):
from telegram import Bot, Update, ParseMode, Message, Chat
from telegram import Bot, Update, ParseMode, Message, Chat, MessageEntity
from telegram.error import BadRequest, Unauthorized
from telegram.ext import CommandHandler, MessageHandler, run_async
from telegram.utils.helpers import escape_markdown
Expand All @@ -14,6 +14,8 @@
from tg_bot import dispatcher, LOGGER
from tg_bot.modules.helper_funcs.chat_status import user_admin
from tg_bot.modules.sql import log_channel_sql as sql
import requests
import tldextract

def loggable(func):
@wraps(func)
Expand Down Expand Up @@ -48,29 +50,94 @@ def log_action(bot: Bot, update: Update, *args, **kwargs):
def log_resource(bot: Bot, update: Update):
entities = update.effective_message.parse_entities()
caption_entities = update.effective_message.parse_caption_entities()
LOGGER.info(entities)
chat = update.effective_chat # type: Optional[Chat]
log_chat = sql.get_chat_log_channel(chat.id)

url = None
tags = []

if log_chat:
result = f"<b>Risorsa inviata da @{update.effective_user.username}:</b>\n"
for descriptor, entity in entities.items():
if descriptor["type"] == MessageEntity.HASHTAG:
tags.append(entity)
if tags:
tags.sort()

for descriptor, entity in entities.items():
result = (
f"<b>Risorsa inviata da @{update.effective_user.username}:</b>\n"
)
if descriptor["type"] in ["url", "text_link"]:
result += f"{entity}"
LOGGER.debug(f"Found message entity: {descriptor['type']} {entity}")
send_log(bot, log_chat, chat.id, result)

if descriptor["type"] == MessageEntity.URL:
try:
response = requests.get(entity)
if response.status_code == requests.codes.ok:
result += f"{entity}"
extracted = tldextract.extract(entity)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Questa parte di estrazione del dominio e costruzione dei tags viene ripetuta più volte per le varie casistiche di MessageEntity, che ne pensi di provare a generalizzare quel flusso in qualche funzione di util così eviteresti un po' di codice ripetuto.

if f"#{extracted.domain}" not in tags:
tags.append(f"#{extracted.domain}")
tags.sort()
if tags:
tags_string = " ".join(tags)
result += f"\n\nTags:\n{tags_string}"
send_log(bot, log_chat, chat.id, result)
except Exception as e:
LOGGER.info(f"Resource {entity} is not a valid url")
LOGGER.error(e)
elif descriptor["type"] == MessageEntity.TEXT_LINK:
try:
response = requests.get(descriptor["url"])
if response.status_code == requests.codes.ok:
result += f'{descriptor["url"]}'
extracted = tldextract.extract(descriptor["url"])
if f"#{extracted.domain}" not in tags:
tags.append(f"#{extracted.domain}")
tags.sort()
if tags:
tags_string = " ".join(tags)
result += f"\n\nTags:\n{tags_string}"
send_log(bot, log_chat, chat.id, result)
except Exception as e:
LOGGER.info(f"Resource {entity} is not a valid url")
LOGGER.error(e)

for descriptor, entity in caption_entities.items():
result = (
"<b>Risorsa inviata da @{update.effective_user.username}:</b>\n"
)
if descriptor["type"] in ["url", "text_link"]:
result += f"{entity}"
LOGGER.debug(f"Found message entity: {descriptor['type']} {entity}")
send_log(bot, log_chat, chat.id, result)
else:
send_log(bot, log_chat, chat.id, result)
if descriptor["type"] == MessageEntity.URL:
try:
response = requests.get(entity)
if response.status_code == requests.codes.ok:
result += f"{entity}"
extracted = tldextract.extract(entity)
if f"#{extracted.domain}" not in tags:
tags.append(f"#{extracted.domain}")
tags.sort()
if tags:
tags_string = " ".join(tags)
result += f"\n\nTags:\n{tags_string}"
send_log(bot, log_chat, chat.id, result)
except Exception as e:
LOGGER.info(f"Resource {entity} is not a valid url")
LOGGER.error(e)
elif descriptor["type"] == MessageEntity.TEXT_LINK:
try:
response = requests.get(descriptor["url"])
if response.status_code == requests.codes.ok:
result += f'{descriptor["url"]}'
extracted = tldextract.extract(descriptor["url"])
if f"#{extracted.domain}" not in tags:
tags.append(f"#{extracted.domain}")
tags.sort()
if tags:
tags_string = " ".join(tags)
result += f"\n\nTags:\n{tags_string}"
send_log(bot, log_chat, chat.id, result)
except Exception as e:
LOGGER.info(f"Resource {entity} is not a valid url")
LOGGER.error(e)

def send_log(bot: Bot, log_chat_id: str, orig_chat_id: str, result: str):
try:
Expand Down Expand Up @@ -207,7 +274,12 @@ def __chat_settings__(chat_id, user_id):
UNSET_LOG_HANDLER = CommandHandler("unsetlog", unsetlog)

LOG_RESOURCES_HANDLER = MessageHandler(
(Filters.entity("url") | Filters.entity("text_link")), log_resource
(
Filters.entity("url")
| Filters.entity("text_link")
| Filters.entity("hashtag")
),
log_resource,
)

dispatcher.add_handler(LOG_HANDLER)
Expand Down