Add types to telegram parser

This commit is contained in:
vas3k
2020-02-26 10:29:42 +01:00
parent 316e5fd3da
commit a5dbd896d0
2 changed files with 12 additions and 6 deletions

View File

@@ -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:

View File

@@ -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()