Skip to content

Commit 2b23911

Browse files
Kavutiiflare3g
authored andcommitted
Added a filter to save only valid URLs as resources in log_channel
1 parent e4ddfa3 commit 2b23911

3 files changed

Lines changed: 25 additions & 11 deletions

File tree

tg_bot/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
# enable logging
99
logging.basicConfig(
10-
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.DEBUG
10+
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO
1111
)
1212

1313
LOGGER = logging.getLogger(__name__)

tg_bot/deactivated-modules

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
NO_LOAD = ['translation','rss', 'sed', 'afk','notes','filters']
1+
NO_LOAD = ['translation','rss', 'sed', 'afk','notes','filters', 'spam']

tg_bot/modules/log_channel.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from tg_bot import dispatcher, LOGGER
1515
from tg_bot.modules.helper_funcs.chat_status import user_admin
1616
from tg_bot.modules.sql import log_channel_sql as sql
17+
import requests
1718

1819
def loggable(func):
1920
@wraps(func)
@@ -57,20 +58,28 @@ def log_resource(bot: Bot, update: Update):
5758
f"<b>Risorsa inviata da @{update.effective_user.username}:</b>\n"
5859
)
5960
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)
61+
try:
62+
response = requests.get(entity)
63+
if response.status_code == requests.codes.ok:
64+
result += f"{entity}"
65+
send_log(bot, log_chat, chat.id, result)
66+
except Exception as e:
67+
LOGGER.info(f"Resource {entity} is not a valid url")
68+
LOGGER.error(e)
6369

6470
for descriptor, entity in caption_entities.items():
6571
result = (
6672
"<b>Risorsa inviata da @{update.effective_user.username}:</b>\n"
6773
)
6874
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)
75+
try:
76+
response = requests.get(entity)
77+
if response.status_code == requests.codes.ok:
78+
result += f"{entity}"
79+
send_log(bot, log_chat, chat.id, result)
80+
except Exception as e:
81+
LOGGER.info(f"Resource {entity} is not a valid url")
82+
LOGGER.error(e)
7483

7584
def send_log(bot: Bot, log_chat_id: str, orig_chat_id: str, result: str):
7685
try:
@@ -207,7 +216,12 @@ def __chat_settings__(chat_id, user_id):
207216
UNSET_LOG_HANDLER = CommandHandler("unsetlog", unsetlog)
208217

209218
LOG_RESOURCES_HANDLER = MessageHandler(
210-
(Filters.entity("url") | Filters.entity("text_link")), log_resource
219+
(
220+
Filters.entity("url")
221+
| Filters.entity("text_link")
222+
| Filters.entity("hashtag")
223+
),
224+
log_resource,
211225
)
212226

213227
dispatcher.add_handler(LOG_HANDLER)

0 commit comments

Comments
 (0)