From a5dbd896d010bb6eea007fb936d81dbf137e14b2 Mon Sep 17 00:00:00 2001 From: vas3k Date: Wed, 26 Feb 2020 10:29:42 +0100 Subject: [PATCH] Add types to telegram parser --- parsing/telegram/telegram.py | 11 +++++++---- parsing/views.py | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/parsing/telegram/telegram.py b/parsing/telegram/telegram.py index 26ca935..dca6cf0 100644 --- a/parsing/telegram/telegram.py +++ b/parsing/telegram/telegram.py @@ -7,7 +7,7 @@ from parsing.telegram.models import MessageType import asyncio -def get_channel(channel_id, messages_limit): +def get_channel(channel_id, *, types=None, limit=10): loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) with TelegramClient( @@ -24,12 +24,12 @@ def get_channel(channel_id, messages_limit): except ValueError: raise ParsingException(f"No channel named '{channel_id}'") - channel.messages = get_channel_messages(client, channel, messages_limit) + channel.messages = get_channel_messages(client, channel, types=types, limit=limit) return channel -def get_channel_messages(client, channel, messages_limit): +def get_channel_messages(client, channel, *, types=None, limit=10): def get_messages_indexes(messages, grouped_id, type=None, inverse=False): type_predicate = lambda m: m != type if inverse else m == type indexes = [] @@ -60,11 +60,14 @@ def get_channel_messages(client, channel, messages_limit): channel_messages = [] - for t_message in client.iter_messages(channel.channel_id, limit=messages_limit): + for t_message in client.iter_messages(channel.channel_id, limit=limit): parser = Parser.from_message(channel, t_message) if parser is not None: message = parser.parse(channel, t_message) if message is not None: + if types and message.type not in types: + continue + if message.grouped_id is not None: merge_messages(channel_messages, message) else: diff --git a/parsing/views.py b/parsing/views.py index 5d127ef..e77b35a 100644 --- a/parsing/views.py +++ b/parsing/views.py @@ -10,10 +10,13 @@ class TelegramChannelFeed(Feed): FEED_ITEMS = 30 def get_object(self, request, channel): - limit = int(request.GET["size"]) if "size" in request.GET else self.FEED_ITEMS + limit = int(request.GET.get("size") or self.FEED_ITEMS) + only = str(request.GET.get("only") or "") + if only: + only = [item.strip() for item in only.split(",")] try: - return get_channel(channel, limit) + return get_channel(channel, types=only, limit=limit) except ParsingException: raise Http404()