Close database connection when command is finished instead of waiting for GC
This commit is contained in:
@ -1,4 +1,4 @@
|
|||||||
from typing import Optional
|
from contextlib import closing
|
||||||
|
|
||||||
import typer
|
import typer
|
||||||
from dhooks import Webhook
|
from dhooks import Webhook
|
||||||
@ -6,9 +6,8 @@ from reader import FeedExistsError, make_reader
|
|||||||
|
|
||||||
from discord_rss_bot.settings import Settings
|
from discord_rss_bot.settings import Settings
|
||||||
|
|
||||||
app = typer.Typer() # For CLI (https://typer.tiangolo.com/)
|
app = typer.Typer()
|
||||||
hook = Webhook(Settings.webhook_url) # For Webhooks (https://github.com/kyb3r/dhooks)
|
hook = Webhook(Settings.webhook_url)
|
||||||
reader = make_reader(Settings.db_file) # For RSS (https://github.com/lemon24/reader)
|
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
@ -20,71 +19,74 @@ def add(
|
|||||||
|
|
||||||
Args:
|
Args:
|
||||||
feed_url (str): The url of the feed to add
|
feed_url (str): The url of the feed to add
|
||||||
|
notify_discord (bool): Whether to send a message to Discord when the feed is added
|
||||||
"""
|
"""
|
||||||
try:
|
with closing(make_reader(Settings.db_file)) as reader:
|
||||||
# Add the feed to the database
|
try:
|
||||||
reader.add_feed(feed_url)
|
# Add the feed to the database
|
||||||
|
reader.add_feed(feed_url)
|
||||||
|
|
||||||
except FeedExistsError:
|
except FeedExistsError:
|
||||||
# If the feed already exists, print a message
|
# If the feed already exists, print a message
|
||||||
typer.echo(f"{feed_url} already exists")
|
typer.echo(f"{feed_url} already exists")
|
||||||
raise typer.Exit()
|
raise typer.Exit()
|
||||||
|
|
||||||
# Update the feeds
|
# Update the feeds
|
||||||
reader.update_feeds()
|
reader.update_feeds()
|
||||||
|
|
||||||
# Mark the feed as read
|
# Mark the feed as read
|
||||||
entries = reader.get_entries(feed=feed_url, read=False)
|
entries = reader.get_entries(feed=feed_url, read=False)
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
reader.mark_entry_as_read(entry)
|
reader.mark_entry_as_read(entry)
|
||||||
|
|
||||||
if notify_discord:
|
if notify_discord:
|
||||||
# Send a message to Discord
|
# Send a message to Discord
|
||||||
hook.send(
|
hook.send(
|
||||||
f"discord-rss-bot: {feed_url} added to the database.\nYou now have "
|
f"discord-rss-bot: {feed_url} added to the database.\nYou now have "
|
||||||
f"{reader.get_feed_counts()} feeds."
|
f"{reader.get_feed_counts()} feeds."
|
||||||
)
|
)
|
||||||
|
|
||||||
typer.echo(f"{feed_url} added")
|
typer.echo(f"{feed_url} added")
|
||||||
|
|
||||||
|
|
||||||
@app.command()
|
@app.command()
|
||||||
def check() -> None:
|
def check() -> None:
|
||||||
"""Check new entries for every feed"""
|
"""Check new entries for every feed"""
|
||||||
feed_count = reader.get_feed_counts()
|
with closing(make_reader(Settings.db_file)) as reader:
|
||||||
entry_count = reader.get_entry_counts()
|
feed_count = reader.get_feed_counts()
|
||||||
|
entry_count = reader.get_entry_counts()
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"""Feeds:
|
f"""Feeds:
|
||||||
Total: {feed_count.total}
|
Total: {feed_count.total}
|
||||||
Broken: {feed_count.broken}
|
Broken: {feed_count.broken}
|
||||||
Enabled: {feed_count.updates_enabled}"""
|
Enabled: {feed_count.updates_enabled}"""
|
||||||
)
|
)
|
||||||
|
|
||||||
print(
|
print(
|
||||||
f"""Entries:
|
f"""Entries:
|
||||||
Total: {entry_count.total} feeds
|
Total: {entry_count.total} feeds
|
||||||
Read: {entry_count.read} feeds
|
Read: {entry_count.read} feeds
|
||||||
Important: {entry_count.important} feeds
|
Important: {entry_count.important} feeds
|
||||||
Has enclosures: {entry_count.has_enclosures} feeds
|
Has enclosures: {entry_count.has_enclosures} feeds
|
||||||
Average number of entries per day:
|
Average number of entries per day:
|
||||||
1 Month: {entry_count.averages[0]:.2f} feeds per day
|
1 Month: {entry_count.averages[0]:.2f} feeds per day
|
||||||
3 Months: {entry_count.averages[1]:.2f} feeds per day
|
3 Months: {entry_count.averages[1]:.2f} feeds per day
|
||||||
12 Months: {entry_count.averages[2]:.2f} feeds per day"""
|
12 Months: {entry_count.averages[2]:.2f} feeds per day"""
|
||||||
)
|
)
|
||||||
|
|
||||||
# Update the feeds
|
# Update the feeds
|
||||||
reader.update_feeds()
|
reader.update_feeds()
|
||||||
|
|
||||||
# Get new entries that are not read
|
# Get new entries that are not read
|
||||||
entries = reader.get_entries(read=False)
|
entries = reader.get_entries(read=False)
|
||||||
|
|
||||||
for entry in entries:
|
for entry in entries:
|
||||||
# Mark the entry as read
|
# Mark the entry as read
|
||||||
reader.mark_entry_as_read(entry)
|
reader.mark_entry_as_read(entry)
|
||||||
|
|
||||||
# Send the entries to Discord
|
# Send the entries to Discord
|
||||||
hook.send(f":robot: :mega: {entry.title}")
|
hook.send(f":robot: :mega: {entry.title}")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Reference in New Issue
Block a user