tg_rss_bot/database.py

83 lines
3.8 KiB
Python
Raw Normal View History

2022-05-19 21:12:43 +00:00
import sqlite3
2022-05-29 17:36:40 +00:00
"""
Classes:
Database - implement intercaction with the database.
"""
2022-05-19 21:12:43 +00:00
class Database():
2022-05-29 17:36:40 +00:00
"""
Implement intercaction with the database.
Methods:
add_user, find_user, add_feed, subscribe_user,
unsubscribe_user, delete_feed, find_feeds,
find_user_feeds, find_feed_items, update_feed_items.
"""
def __init__(self, path: str) -> None:
2022-05-29 17:36:40 +00:00
"""Create a database file if not exists."""
2022-05-19 22:46:55 +00:00
self.conn = sqlite3.connect(path)
2022-05-19 21:12:43 +00:00
self.cur = self.conn.cursor()
self.cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER, telegram_id NUMERIC NOT NULL UNIQUE, PRIMARY KEY(id))')
self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id INTEGER, url TEXT NOT NULL UNIQUE, PRIMARY KEY(id))')
2022-05-29 17:36:40 +00:00
self.cur.execute('CREATE TABLE IF NOT EXISTS subscriptions (user_id INTEGER, feed_id INTEGER, UNIQUE (user_id, feed_id), FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))')
self.cur.execute('CREATE TABLE IF NOT EXISTS feeds_last_items (feed_id INTEGER, url TEXT NOT NULL UNIQUE, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))')
2022-05-21 10:48:33 +00:00
def add_user(self, telegram_id: str) -> int:
2022-05-29 17:36:40 +00:00
"""Add a user's telegram id to the database and return his database id."""
self.cur.execute('INSERT INTO users (telegram_id) VALUES (?)', [telegram_id])
2022-05-22 22:02:17 +00:00
self.conn.commit()
return self.cur.lastrowid
2022-05-19 21:12:43 +00:00
2022-05-28 20:55:48 +00:00
def find_user(self, telegram_id: str) -> int | None:
2022-05-29 17:36:40 +00:00
"""Get a user's telegram id and return his database id."""
self.cur.execute('SELECT id FROM users WHERE telegram_id = ?', [telegram_id])
row = self.cur.fetchone()
if row is None:
2022-05-22 22:41:37 +00:00
return None
return row[0]
2022-05-22 21:40:01 +00:00
def add_feed(self, url: str) -> int:
2022-05-29 17:36:40 +00:00
"""Add a feed from to the database and return its id."""
self.cur.execute('INSERT INTO feeds (url) VALUES (?)', [url])
2022-05-22 22:02:17 +00:00
self.conn.commit()
return self.cur.lastrowid
2022-05-28 22:06:34 +00:00
def subscribe_user(self, user_id: int, feed_id: int) -> None:
2022-05-29 17:36:40 +00:00
"""Subscribe a user to the feed."""
self.cur.execute('INSERT INTO subscriptions (user_id, feed_id) VALUES (?, ?)', [user_id, feed_id])
self.conn.commit()
2022-05-28 22:06:34 +00:00
def unsubscribe_user(self, user_id: int, feed_id: int) -> None:
2022-05-29 17:36:40 +00:00
"""Unsubscribe a user from the feed."""
self.cur.execute('DELETE FROM subscriptions WHERE feed_id = ? AND user_id = ?', [feed_id, user_id])
2022-05-28 22:06:34 +00:00
self.conn.commit()
2022-05-22 22:41:37 +00:00
def delete_feed(self, feed_id: int) -> None:
2022-05-29 17:36:40 +00:00
"""Delete a feed from the database."""
self.cur.execute('DELETE FROM feeds WHERE id = ?', [feed_id])
2022-05-22 22:41:37 +00:00
self.conn.commit()
2022-05-19 21:12:43 +00:00
def find_feeds(self) -> list:
2022-05-29 17:36:40 +00:00
"""Get a list of feeds from the database and returns it."""
2022-05-28 22:22:41 +00:00
self.cur.execute('SELECT * FROM feeds')
return self.cur.fetchall()
2022-05-29 17:36:40 +00:00
def find_user_feeds(self, user_id: int) -> list:
2022-05-29 17:36:40 +00:00
"""Return a list of feeds the user is subscribed to."""
self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscriptions WHERE user_id = ?)', [user_id])
2022-05-28 22:47:19 +00:00
return self.cur.fetchall()
2022-05-19 21:12:43 +00:00
def find_feed_items(self, feed_id: int) -> list:
2022-05-29 17:36:40 +00:00
"""Get feed items and return it."""
2022-05-29 15:16:48 +00:00
self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = ?', [feed_id])
return self.cur.fetchall()
def update_feed_items(self, feed_id: int, new_items: list) -> None:
2022-05-29 17:36:40 +00:00
"""Replace feed items from the database with a list items that receive."""
2022-05-29 15:16:48 +00:00
for i in range(len(new_items)):
new_items[i] = (feed_id,) + new_items[i]
2022-05-29 17:36:40 +00:00
2022-05-29 15:16:48 +00:00
self.cur.execute('DELETE FROM feeds_last_items WHERE feed_id = ?', [feed_id])
self.cur.executemany('INSERT INTO feeds_last_items (feed_id, url, title, description, date) VALUES (?, ?, ?, ?, ?)', new_items)
2022-05-29 17:36:40 +00:00
self.conn.commit()