Fix send_to_discord and add more excepts

This commit is contained in:
2022-12-06 10:18:33 +01:00
parent b5c3edf871
commit c8e76291a5
2 changed files with 48 additions and 24 deletions

View File

@ -24,12 +24,15 @@ Exceptions:
from discord_webhook import DiscordWebhook
from pydantic import BaseModel
from reader import (
EntryNotFoundError,
FeedExistsError,
FeedNotFoundError,
InvalidFeedURLError,
ParseError,
StorageError,
TagNotFoundError,
)
from reader.types import EntryLike
from requests import Response
from discord_rss_bot.settings import logger, reader
@ -127,19 +130,17 @@ def check_feeds() -> None:
if it was successful.
"""
reader.update_feeds()
entries = reader.get_entries(read=False)
for entry in entries:
send_to_discord(entry)
send_to_discord()
def send_to_discord(entry) -> Response:
def send_to_discord(feed=None):
"""
Send entries to Discord.
If response was not ok, we will log the error and mark the entry as unread, so it will be sent again next time.
Args:
entry: The entry to send.
feed: The entry to send.
Raises:
NoWebhookFoundError: If no webhook is found.
@ -147,11 +148,36 @@ def send_to_discord(entry) -> Response:
Returns:
Response: The response from the webhook.
"""
if feed is None:
entries = reader.get_entries(read=False)
else:
entries = reader.get_entries(feed=feed, read=False)
reader.set_entry_read(entry)
if not entries:
logger.info("No entries to send")
return
for entry in entries:
logger.debug(f"Sending entry {entry} to Discord")
try:
reader.set_entry_read(entry, True)
logger.debug(f"New entry: {entry.title}")
except EntryNotFoundError:
logger.error("Entry not found", exc_info=True)
raise
except StorageError:
logger.error("Storage error", exc_info=True)
raise
try:
webhook_url = str(reader.get_tag(entry.feed.url, "webhook"))
except TagNotFoundError:
logger.error("Tag not found", exc_info=True)
raise
except StorageError:
logger.error("Storage error", exc_info=True)
raise
if not webhook_url:
logger.error(f"No webhook found for feed: {entry.feed.url}")
raise NoWebhookFoundError(f"No webhook found for feed: {entry.feed.url}")
@ -163,7 +189,6 @@ def send_to_discord(entry) -> Response:
if not response.ok:
logger.error(f"Error: {response.status_code} {response.reason}")
reader.set_entry_read(entry, False)
return response
def update_feed(feed_url: str, webhook: str) -> IfFeedError:

View File

@ -37,7 +37,7 @@ from fastapi import FastAPI, Form, HTTPException, Request
from fastapi.responses import FileResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from reader import Entry, EntryCounts, Feed, FeedCounts, ResourceNotFoundError
from reader import EntryCounts, Feed, FeedCounts, ResourceNotFoundError
from tomlkit.toml_document import TOMLDocument
from discord_rss_bot.feeds import IfFeedError, add_feed, send_to_discord, update_feed
@ -53,8 +53,7 @@ templates: Jinja2Templates = Jinja2Templates(directory="templates")
def check_feed(request: Request, feed_url: str = Form()):
"""Check all feeds"""
reader.update_feeds()
entry: Iterable[Entry] = reader.get_entries(feed=feed_url, read=False)
send_to_discord(entry)
send_to_discord(feed_url)
logger.info(f"Get feed: {feed_url}")
feed: Feed = reader.get_feed(feed_url)