correct docstrings
This commit is contained in:
parent
0dc20fc098
commit
c9ae6dcc78
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -5,4 +5,4 @@
|
||||||
/.venv
|
/.venv
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
/bot.db
|
/*.db
|
24
database.py
24
database.py
|
@ -5,14 +5,8 @@ Database - implement intercaction with the database.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class Database():
|
class Database():
|
||||||
"""
|
"""Implement intercaction with the 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.
|
|
||||||
"""
|
|
||||||
def __init__(self, path: str) -> None:
|
def __init__(self, path: str) -> None:
|
||||||
"""Create a database file if not exists."""
|
"""Create a database file if not exists."""
|
||||||
self.conn = sqlite3.connect(path)
|
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))')
|
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:
|
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.cur.execute('INSERT INTO users (telegram_id) VALUES (?)', [telegram_id])
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
return self.cur.lastrowid
|
return self.cur.lastrowid
|
||||||
|
|
||||||
def find_user(self, telegram_id: str) -> int | None:
|
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])
|
self.cur.execute('SELECT id FROM users WHERE telegram_id = ?', [telegram_id])
|
||||||
row = self.cur.fetchone()
|
row = self.cur.fetchone()
|
||||||
if row is None:
|
if row is None:
|
||||||
|
@ -37,7 +31,7 @@ class Database():
|
||||||
return row[0]
|
return row[0]
|
||||||
|
|
||||||
def add_feed(self, url: str) -> int:
|
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.cur.execute('INSERT INTO feeds (url) VALUES (?)', [url])
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
return self.cur.lastrowid
|
return self.cur.lastrowid
|
||||||
|
@ -53,12 +47,12 @@ class Database():
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
def delete_feed(self, feed_id: int) -> None:
|
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.cur.execute('DELETE FROM feeds WHERE id = ?', [feed_id])
|
||||||
self.conn.commit()
|
self.conn.commit()
|
||||||
|
|
||||||
def find_feeds(self) -> list:
|
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')
|
self.cur.execute('SELECT * FROM feeds')
|
||||||
return self.cur.fetchall()
|
return self.cur.fetchall()
|
||||||
|
|
||||||
|
@ -68,12 +62,12 @@ class Database():
|
||||||
return self.cur.fetchall()
|
return self.cur.fetchall()
|
||||||
|
|
||||||
def find_feed_items(self, feed_id: int) -> list:
|
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])
|
self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = ?', [feed_id])
|
||||||
return self.cur.fetchall()
|
return self.cur.fetchall()
|
||||||
|
|
||||||
def update_feed_items(self, feed_id: int, new_items: list) -> None:
|
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)):
|
for i in range(len(new_items)):
|
||||||
new_items[i] = (feed_id,) + new_items[i]
|
new_items[i] = (feed_id,) + new_items[i]
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue