Add tests

This commit is contained in:
2022-12-13 22:09:51 +01:00
parent 32f474a0ed
commit a704a63582
10 changed files with 325 additions and 22 deletions

View File

@ -24,27 +24,31 @@ Exceptions:
from typing import Iterable
from discord_webhook import DiscordWebhook
from reader import Entry
from reader import Entry, Reader
from requests import Response
from discord_rss_bot.settings import reader
from discord_rss_bot.settings import get_reader
def send_to_discord(feed=None) -> None:
def send_to_discord(reader: Reader = None, feed=None, do_once=False) -> 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:
reader: If we should use a custom reader instead of the default one.
feed: The entry to send.
Raises:
NoWebhookFoundError: If no webhook is found.
do_once: If we should only send one entry. This is used in the test.
Returns:
Response: The response from the webhook.
"""
# Get the default reader if we didn't get a custom one.
if reader is None:
reader = get_reader()
# If we should get all entries, or just the entries from a specific feed.
if feed is None:
reader.update_feeds()
entries: Iterable[Entry] = reader.get_entries(read=False)
@ -53,13 +57,21 @@ def send_to_discord(feed=None) -> None:
entries: Iterable[Entry] = reader.get_entries(feed=feed, read=False)
for entry in entries:
# Set the webhook to read, so we don't send it again.
reader.set_entry_read(entry, True)
# Get the webhook from the feed.
webhook_url: str = str(reader.get_tag(entry.feed_url, "webhook"))
webhook_message: str = f":robot: :mega: {entry.title}\n{entry.link}"
webhook: DiscordWebhook = DiscordWebhook(url=webhook_url, content=webhook_message, rate_limit_retry=True)
# Send the webhook.
response: Response = webhook.execute()
if not response.ok:
reader.set_entry_read(entry, False)
# If we only want to send one entry, we will break the loop. This is used when testing this function.
if do_once:
break
reader.update_search()