Files
infomate.club/parsing/telegram/models.py
Vitalii Honchar 2eaa9fa65c Telegram -> RSS parser (#16)
* Created first dev version of Telegram channels parser

* Created endpoint for get Telegram Channel RSS

* Added *.session and venv/ in .gitignore

* Added dependency telethon

* Added script for init Telethon

* Removed unnecessary script parse.py

* Added possibility for dynamic generate RSS feeds from Telegram channel

* Deleted saving Telegram messages in database

* Minor refactor

Created separated files: parsers.py and utils.py

* Enhanced text parser: parse title and description if can

* Added attachment parsers: video, photo, voice and file parsers

* Added possibility for merge grouped messages into one item

* Renamed url for parse Telegram channel from "rss/telegram/<str:channel>" to "parsing/telegram/<str:channel>/"

* Format code with black

* Refactor: created package parsing.telegram and rename utils to models

* Added Makefile target telegram
2020-02-12 10:57:52 +01:00

70 lines
1.8 KiB
Python

from enum import Enum
class TelegramChannel:
def __init__(
self, channel_id=None, title=None, link=None, description=None, messages=None
):
self.channel_id = channel_id
self.title = title
self.link = link
self.description = description
self.messages = messages if messages is not None else []
def add_message(self, message):
self.messages.append(message)
def remove_message(self, message):
self.messages.remove(message)
def to_dict(self):
return {
"channel_id": self.channel_id,
"title": self.title,
"link": self.link,
"description": self.description,
"messages": list(map(lambda message: message.to_dict(), self.messages)),
}
class TelegramChannelMessage:
def __init__(
self,
telegram_id=None,
title=None,
description=None,
link=None,
channel=None,
grouped_id=None,
type=None,
timestamp=None,
):
self.telegram_id = telegram_id
self.title = title
self.description = description
self.link = link
self.channel = channel
self.grouped_id = grouped_id
self.type = type
self.timestamp = timestamp
def to_dict(self):
return {
"telegram_id": self.telegram_id,
"title": self.title,
"description": self.description,
"link": self.link,
"channel": self.channel.channel_id,
"grouped_id": self.grouped_id,
"type": self.type.value,
"timestamp": self.timestamp.strftime("%Y-%m-%d %H:%M:%S"),
}
class MessageType(Enum):
TEXT = "text"
VIDEO = "video"
PHOTO = "photo"
VOICE = "voice"
FILE = "file"