Fix 500 errors in parser + add caching

This commit is contained in:
Vasily Zubarev
2020-02-13 13:46:52 +01:00
parent 252b735d1c
commit 57b08b7df1
5 changed files with 28 additions and 6 deletions

View File

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

View File

@@ -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("<slug:board_slug>/", board, name="board"),
path("<slug:board_slug>/export/", export, name="export"),
path("parsing/telegram/<str:channel>/", TelegramChannelFeed(), name="telegram_channel_feed")
path("parsing/telegram/<str:channel>/",
cache_page(settings.TELEGRAM_CACHE_SECONDS)(TelegramChannelFeed()),
name="telegram_channel_feed")
]

2
parsing/exceptions.py Normal file
View File

@@ -0,0 +1,2 @@
class ParsingException(Exception):
pass

View File

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

View File

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