database_module #17
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -5,4 +5,4 @@
|
|||
/.venv
|
||||
|
||||
# Database
|
||||
/bot.db
|
||||
/*.db
|
24
database.py
24
database.py
|
@ -5,14 +5,8 @@ Database - implement intercaction with the database.
|
|||
"""
|
||||
|
||||
class Database():
|
||||
"""
|
||||
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.
|
||||
"""
|
||||
"""Implement intercaction with the database."""
|
||||
|
||||
def __init__(self, path: str) -> None:
|
||||
"""Create a database file if not exists."""
|
||||
self.conn = sqlite3.connect(path)
|
||||
|
@ -23,13 +17,13 @@ class Database():
|
|||
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))')
|
||||
|
||||
def add_user(self, telegram_id: str) -> int:
|
||||
"""Add a user's telegram id to the database and return his database id."""
|
||||
"""Add a user's telegram id to the database and return its database id."""
|
||||
self.cur.execute('INSERT INTO users (telegram_id) VALUES (?)', [telegram_id])
|
||||
self.conn.commit()
|
||||
return self.cur.lastrowid
|
||||
|
||||
def find_user(self, telegram_id: str) -> int | None:
|
||||
"""Get a user's telegram id and return his database id."""
|
||||
"""Get a user's telegram id and return its database id."""
|
||||
self.cur.execute('SELECT id FROM users WHERE telegram_id = ?', [telegram_id])
|
||||
row = self.cur.fetchone()
|
||||
if row is None:
|
||||
|
@ -37,7 +31,7 @@ class Database():
|
|||
return row[0]
|
||||
|
||||
def add_feed(self, url: str) -> int:
|
||||
"""Add a feed from to the database and return its id."""
|
||||
"""Add a feed to the database and return its id."""
|
||||
self.cur.execute('INSERT INTO feeds (url) VALUES (?)', [url])
|
||||
self.conn.commit()
|
||||
return self.cur.lastrowid
|
||||
|
@ -53,12 +47,12 @@ class Database():
|
|||
self.conn.commit()
|
||||
|
||||
def delete_feed(self, feed_id: int) -> None:
|
||||
"""Delete a feed from the database."""
|
||||
"""Delete a feed."""
|
||||
self.cur.execute('DELETE FROM feeds WHERE id = ?', [feed_id])
|
||||
self.conn.commit()
|
||||
|
||||
def find_feeds(self) -> list:
|
||||
"""Get a list of feeds from the database and returns it."""
|
||||
"""Get a list of feeds."""
|
||||
self.cur.execute('SELECT * FROM feeds')
|
||||
return self.cur.fetchall()
|
||||
|
||||
|
@ -68,12 +62,12 @@ class Database():
|
|||
return self.cur.fetchall()
|
||||
|
||||
def find_feed_items(self, feed_id: int) -> list:
|
||||
"""Get feed items and return it."""
|
||||
"""Get last feed items."""
|
||||
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:
|
||||
"""Replace feed items from the database with a list items that receive."""
|
||||
"""Replace last feed items with a list items that receive."""
|
||||
for i in range(len(new_items)):
|
||||
new_items[i] = (feed_id,) + new_items[i]
|
||||
|
||||
|
|
Loading…
Reference in a new issue