Fix send_to_discord and add more excepts
This commit is contained in:
@ -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,23 +148,47 @@ 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)
|
||||
logger.debug(f"New entry: {entry.title}")
|
||||
if not entries:
|
||||
logger.info("No entries to send")
|
||||
return
|
||||
|
||||
webhook_url = str(reader.get_tag(entry.feed.url, "webhook"))
|
||||
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}")
|
||||
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
|
||||
|
||||
logger.debug(f"Sending to webhook: {webhook_url}")
|
||||
webhook_message = f":robot: :mega: {entry.title}\n{entry.link}"
|
||||
webhook = DiscordWebhook(url=webhook_url, content=webhook_message, rate_limit_retry=True)
|
||||
response = webhook.execute()
|
||||
if not response.ok:
|
||||
logger.error(f"Error: {response.status_code} {response.reason}")
|
||||
reader.set_entry_read(entry, False)
|
||||
return response
|
||||
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}")
|
||||
|
||||
logger.debug(f"Sending to webhook: {webhook_url}")
|
||||
webhook_message = f":robot: :mega: {entry.title}\n{entry.link}"
|
||||
webhook = DiscordWebhook(url=webhook_url, content=webhook_message, rate_limit_retry=True)
|
||||
response = webhook.execute()
|
||||
if not response.ok:
|
||||
logger.error(f"Error: {response.status_code} {response.reason}")
|
||||
reader.set_entry_read(entry, False)
|
||||
|
||||
|
||||
def update_feed(feed_url: str, webhook: str) -> IfFeedError:
|
||||
|
@ -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)
|
||||
@ -94,8 +93,8 @@ async def create_feed(feed_url: str = Form(), webhook_dropdown: str = Form()) ->
|
||||
|
||||
# Check if set_hook_by_name() was successful.
|
||||
if isinstance(
|
||||
set_hook_by_name(name=webhook_dropdown, feed_url=feed_url),
|
||||
ResourceNotFoundError,
|
||||
set_hook_by_name(name=webhook_dropdown, feed_url=feed_url),
|
||||
ResourceNotFoundError,
|
||||
):
|
||||
return set_hook_by_name(name=webhook_dropdown, feed_url=feed_url)
|
||||
|
||||
|
Reference in New Issue
Block a user