diff --git a/infomate/settings.py b/infomate/settings.py index 7af16a2..ac04689 100644 --- a/infomate/settings.py +++ b/infomate/settings.py @@ -120,6 +120,7 @@ MEDIA_UPLOAD_CODE = None # should be set in private_settings.py TELEGRAM_APP_ID = None # should set in private_settings.py TELEGRAM_APP_HASH = None # should set in private_settings.py TELEGRAM_SESSION_FILE = None # should set in private settings.py +TELEGRAM_CACHE_SECONDS = 10 * 60 # 10 min try: # poor mans' private settings diff --git a/infomate/urls.py b/infomate/urls.py index 3b416c5..6183d0e 100644 --- a/infomate/urls.py +++ b/infomate/urls.py @@ -1,7 +1,9 @@ from django.urls import path +from django.views.decorators.cache import cache_page from auth.views import login, logout, club_callback from boards.views import index, board, privacy_policy, what, export +from infomate import settings from parsing.views import TelegramChannelFeed urlpatterns = [ @@ -16,5 +18,8 @@ urlpatterns = [ path("/", board, name="board"), path("/export/", export, name="export"), - path("parsing/telegram//", TelegramChannelFeed(), name="telegram_channel_feed") + + path("parsing/telegram//", + cache_page(settings.TELEGRAM_CACHE_SECONDS)(TelegramChannelFeed()), + name="telegram_channel_feed") ] diff --git a/parsing/exceptions.py b/parsing/exceptions.py new file mode 100644 index 0000000..aec9c9a --- /dev/null +++ b/parsing/exceptions.py @@ -0,0 +1,2 @@ +class ParsingException(Exception): + pass diff --git a/parsing/telegram/telegram.py b/parsing/telegram/telegram.py index 280891a..93e3110 100644 --- a/parsing/telegram/telegram.py +++ b/parsing/telegram/telegram.py @@ -1,5 +1,7 @@ from telethon.sync import TelegramClient, functions from django.conf import settings + +from parsing.exceptions import ParsingException from parsing.telegram.parsers import Parser, parse_channel from parsing.telegram.models import MessageType import asyncio @@ -14,11 +16,16 @@ def get_channel(channel_id, messages_limit): settings.TELEGRAM_APP_HASH, loop=loop, ) as client: - channel = parse_channel( - channel_id, - client(functions.channels.GetFullChannelRequest(channel=channel_id)), - ) + try: + channel = parse_channel( + channel_id, + client(functions.channels.GetFullChannelRequest(channel=channel_id)), + ) + except ValueError: + raise ParsingException(f"No channel named '{channel_id}'") + channel.messages = __get_channel_messages(client, channel, messages_limit) + return channel diff --git a/parsing/views.py b/parsing/views.py index 5415ca3..453b37a 100644 --- a/parsing/views.py +++ b/parsing/views.py @@ -1,4 +1,7 @@ from django.contrib.syndication.views import Feed +from django.http import Http404 + +from parsing.exceptions import ParsingException from parsing.telegram.telegram import get_channel @@ -10,7 +13,11 @@ class TelegramChannelFeed(Feed): feed_items = ( int(request.GET["size"]) if "size" in request.GET else self.FEED_ITEMS ) - return get_channel(channel, feed_items) + + try: + return get_channel(channel, feed_items) + except ParsingException: + raise Http404() def title(self, obj): return obj.title