From f0c4b0425a6175c64a5c5aa3d264eb9921b368bc Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Thu, 19 May 2022 21:12:43 +0000 Subject: [PATCH 01/20] init database object --- .gitignore | 3 +++ database_interaction.py | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 database_interaction.py diff --git a/.gitignore b/.gitignore index 5493d8f..dbd23b8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,6 @@ # Python /.venv + +# Database +users_subscribes.db \ No newline at end of file diff --git a/database_interaction.py b/database_interaction.py new file mode 100644 index 0000000..56447d3 --- /dev/null +++ b/database_interaction.py @@ -0,0 +1,36 @@ +import sqlite3 +class Database(): + __instance = None + + def __new__(cls, *args, **kwargs): + if cls.__instance is None: + cls.__instance = super().__new__(cls) + + return cls.__instance + + def __init__(self): + self.conn = sqlite3.connect('users_subscribes.db') + self.cur = self.conn.cursor() + self.cur.execute('CREATE TABLE users (id INTEGER, telegram_id NUMERIC, PRIMARY KEY(id))') + self.cur.execute('CREATE TABLE feeds (id INTEGER, link TEXT, PRIMARY KEY(id))') + self.cur.execute('CREATE TABLE subscribes (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))') + self.cur.execute('CREATE TABLE feeds_last_items (feed_id INTEGER, link TEXT, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))') + + def add_rss_feed(): + pass + + def delete_rss_feed(): + pass + + def get_feeds(): + pass + + def get_users_subscribes(): + pass + + def get_feed_items(): + pass + + def storing_feed_content(): + pass + -- 2.43.5 From 60a7648ce421a61e5cbefa186c8c29f2c4ae7dae Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Thu, 19 May 2022 22:46:55 +0000 Subject: [PATCH 02/20] correct init database object --- .gitignore | 2 +- database_interaction.py | 20 ++++++-------------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/.gitignore b/.gitignore index dbd23b8..8b2d7e0 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ /.venv # Database -users_subscribes.db \ No newline at end of file +/bot.db \ No newline at end of file diff --git a/database_interaction.py b/database_interaction.py index 56447d3..48aadc9 100644 --- a/database_interaction.py +++ b/database_interaction.py @@ -1,20 +1,12 @@ import sqlite3 class Database(): - __instance = None - - def __new__(cls, *args, **kwargs): - if cls.__instance is None: - cls.__instance = super().__new__(cls) - - return cls.__instance - - def __init__(self): - self.conn = sqlite3.connect('users_subscribes.db') + def __init__(self, path): + self.conn = sqlite3.connect(path) self.cur = self.conn.cursor() - self.cur.execute('CREATE TABLE users (id INTEGER, telegram_id NUMERIC, PRIMARY KEY(id))') - self.cur.execute('CREATE TABLE feeds (id INTEGER, link TEXT, PRIMARY KEY(id))') - self.cur.execute('CREATE TABLE subscribes (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))') - self.cur.execute('CREATE TABLE feeds_last_items (feed_id INTEGER, link TEXT, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))') + self.cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER, telegram_id NUMERIC, PRIMARY KEY(id))') + self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id INTEGER, link TEXT, PRIMARY KEY(id))') + self.cur.execute('CREATE TABLE IF NOT EXISTS subscribes (user_id INTEGER, feed_id INTEGER, 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, link TEXT, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))') def add_rss_feed(): pass -- 2.43.5 From 8b04adef2b2764baf0d5839077bf19de6b93651d Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 21 May 2022 10:48:33 +0000 Subject: [PATCH 03/20] add add_user method --- database_interaction.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/database_interaction.py b/database_interaction.py index 48aadc9..2586e86 100644 --- a/database_interaction.py +++ b/database_interaction.py @@ -6,7 +6,11 @@ class Database(): self.cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER, telegram_id NUMERIC, PRIMARY KEY(id))') self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id INTEGER, link TEXT, PRIMARY KEY(id))') self.cur.execute('CREATE TABLE IF NOT EXISTS subscribes (user_id INTEGER, feed_id INTEGER, 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, link TEXT, 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, link TEXT, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))') + + def add_user(self, telegram_id): + self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id}) + self.conn.commit() def add_rss_feed(): pass @@ -25,4 +29,3 @@ class Database(): def storing_feed_content(): pass - -- 2.43.5 From c25e0c78fee6b44eeaf0772943a2e7a824708138 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 21 May 2022 13:55:28 +0000 Subject: [PATCH 04/20] make unique values in database and add handling exception in add_user method --- database_interaction.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/database_interaction.py b/database_interaction.py index 2586e86..af621d1 100644 --- a/database_interaction.py +++ b/database_interaction.py @@ -3,13 +3,16 @@ class Database(): def __init__(self, path): self.conn = sqlite3.connect(path) self.cur = self.conn.cursor() - self.cur.execute('CREATE TABLE IF NOT EXISTS users (id INTEGER, telegram_id NUMERIC, PRIMARY KEY(id))') - self.cur.execute('CREATE TABLE IF NOT EXISTS feeds (id INTEGER, link TEXT, PRIMARY KEY(id))') + 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, link TEXT NOT NULL UNIQUE, PRIMARY KEY(id))') self.cur.execute('CREATE TABLE IF NOT EXISTS subscribes (user_id INTEGER, feed_id INTEGER, 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, link TEXT, 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, link TEXT NOT NULL UNIQUE, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))') def add_user(self, telegram_id): - self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id}) + try: + self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id}) + except sqlite3.IntegrityError: + return 1 self.conn.commit() def add_rss_feed(): @@ -29,3 +32,5 @@ class Database(): def storing_feed_content(): pass + +allo = Database('bot.db') \ No newline at end of file -- 2.43.5 From 98e6e16ae52f81a94cbc4c00f12cae61b8cb90b9 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 21 May 2022 13:57:25 +0000 Subject: [PATCH 05/20] delete unnecessary line --- database_interaction.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/database_interaction.py b/database_interaction.py index af621d1..cd125a3 100644 --- a/database_interaction.py +++ b/database_interaction.py @@ -32,5 +32,3 @@ class Database(): def storing_feed_content(): pass - -allo = Database('bot.db') \ No newline at end of file -- 2.43.5 From 565b3ae87a4d4d2a06cc2e0587cba284d3e3c0b6 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 22 May 2022 21:26:27 +0000 Subject: [PATCH 06/20] remove handling exception in add_user method and change return value --- database_interaction.py | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/database_interaction.py b/database_interaction.py index cd125a3..14c40e2 100644 --- a/database_interaction.py +++ b/database_interaction.py @@ -1,6 +1,6 @@ import sqlite3 class Database(): - def __init__(self, path): + def __init__(self, path: str) -> None: self.conn = sqlite3.connect(path) 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))') @@ -8,12 +8,9 @@ class Database(): self.cur.execute('CREATE TABLE IF NOT EXISTS subscribes (user_id INTEGER, feed_id INTEGER, 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, link TEXT NOT NULL UNIQUE, title TEXT, description TEXT, date NUMERIC, FOREIGN KEY(feed_id) REFERENCES feeds(id))') - def add_user(self, telegram_id): - try: - self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id}) - except sqlite3.IntegrityError: - return 1 - self.conn.commit() + def add_user(self, telegram_id: str) -> int: + self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id}) + return self.cur.lastrowid def add_rss_feed(): pass -- 2.43.5 From 91e84bfd67c720e3a0ac9a0e0e32818e8232ced2 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 22 May 2022 21:40:01 +0000 Subject: [PATCH 07/20] add get_user method --- database_interaction.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/database_interaction.py b/database_interaction.py index 14c40e2..0b7e717 100644 --- a/database_interaction.py +++ b/database_interaction.py @@ -12,6 +12,11 @@ class Database(): self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id}) return self.cur.lastrowid + def get_user(self, telegram_id: str) -> tuple | None: + self.cur.execute('SELECT id FROM users WHERE telegram_id = :telegram_id', {'telegram_id': telegram_id}) + id = self.cur.fetchone() + return id + def add_rss_feed(): pass @@ -28,4 +33,4 @@ class Database(): pass def storing_feed_content(): - pass + pass \ No newline at end of file -- 2.43.5 From 6db0da68be4c55e05ff3a84e015799768fce19f8 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 22 May 2022 22:02:17 +0000 Subject: [PATCH 08/20] add add_feed method --- database_interaction.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/database_interaction.py b/database_interaction.py index 0b7e717..17c1d42 100644 --- a/database_interaction.py +++ b/database_interaction.py @@ -10,17 +10,20 @@ class Database(): def add_user(self, telegram_id: str) -> int: self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_id}) + self.conn.commit() return self.cur.lastrowid def get_user(self, telegram_id: str) -> tuple | None: self.cur.execute('SELECT id FROM users WHERE telegram_id = :telegram_id', {'telegram_id': telegram_id}) - id = self.cur.fetchone() - return id + user_id = self.cur.fetchone() + return user_id - def add_rss_feed(): - pass - - def delete_rss_feed(): + def add_feed(self, link: str) -> int: + self.cur.execute('INSERT INTO feeds (link) VALUES (:link)', {'link': link}) + self.conn.commit() + return self.cur.lastrowid + + def delete_feed(): pass def get_feeds(): -- 2.43.5 From b4ed8070d5f5d8b537e1d8a4a9b71707b4badbe9 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 22 May 2022 22:41:37 +0000 Subject: [PATCH 09/20] add delete_feed method --- database_interaction.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/database_interaction.py b/database_interaction.py index 17c1d42..0003754 100644 --- a/database_interaction.py +++ b/database_interaction.py @@ -13,18 +13,21 @@ class Database(): self.conn.commit() return self.cur.lastrowid - def get_user(self, telegram_id: str) -> tuple | None: + def get_user(self, telegram_id: str) -> int | None: self.cur.execute('SELECT id FROM users WHERE telegram_id = :telegram_id', {'telegram_id': telegram_id}) user_id = self.cur.fetchone() - return user_id + if user_id is None: + return None + return user_id[0] def add_feed(self, link: str) -> int: self.cur.execute('INSERT INTO feeds (link) VALUES (:link)', {'link': link}) self.conn.commit() return self.cur.lastrowid - def delete_feed(): - pass + def delete_feed(self, feed_id: int) -> None: + self.cur.execute('DELETE FROM feeds WHERE id = :feed_id', {'feed_id': feed_id}) + self.conn.commit() def get_feeds(): pass -- 2.43.5 From baa215334fbefb15495516fc1b5c69959e6a8f46 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 28 May 2022 20:30:09 +0000 Subject: [PATCH 10/20] rename database_interaction.py to database.py --- database_interaction.py => database.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename database_interaction.py => database.py (100%) diff --git a/database_interaction.py b/database.py similarity index 100% rename from database_interaction.py rename to database.py -- 2.43.5 From a5422cab393e281c6d0cdb038b1473e338bdd860 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 28 May 2022 20:55:48 +0000 Subject: [PATCH 11/20] rename get_user method to find_user --- database.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database.py b/database.py index 0003754..f10687f 100644 --- a/database.py +++ b/database.py @@ -13,7 +13,7 @@ class Database(): self.conn.commit() return self.cur.lastrowid - def get_user(self, telegram_id: str) -> int | None: + def find_user(self, telegram_id: str) -> int | None: self.cur.execute('SELECT id FROM users WHERE telegram_id = :telegram_id', {'telegram_id': telegram_id}) user_id = self.cur.fetchone() if user_id is None: -- 2.43.5 From 319a21472fb34410cb8b0b81c9add6ba117d0309 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 28 May 2022 22:00:04 +0000 Subject: [PATCH 12/20] add subscribe_user method and rename some variables --- database.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/database.py b/database.py index f10687f..e8ce88f 100644 --- a/database.py +++ b/database.py @@ -4,29 +4,33 @@ class Database(): self.conn = sqlite3.connect(path) 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, link TEXT 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))') self.cur.execute('CREATE TABLE IF NOT EXISTS subscribes (user_id INTEGER, feed_id INTEGER, 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, link 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: - self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': telegram_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: - self.cur.execute('SELECT id FROM users WHERE telegram_id = :telegram_id', {'telegram_id': telegram_id}) - user_id = self.cur.fetchone() - if user_id is None: + self.cur.execute('SELECT id FROM users WHERE telegram_id = ?', (telegram_id,)) + row = self.cur.fetchone() + if row is None: return None - return user_id[0] + return row[0] - def add_feed(self, link: str) -> int: - self.cur.execute('INSERT INTO feeds (link) VALUES (:link)', {'link': link}) + def add_feed(self, url: str) -> int: + self.cur.execute('INSERT INTO feeds (url) VALUES (?)', (url,)) self.conn.commit() return self.cur.lastrowid + def subscribe_user(self, user_id: int, feed_id: int): + self.cur.execute('INSERT INTO subscribes (user_id, feed_id) VALUES (?, ?)', (user_id, feed_id)) + self.conn.commit() + def delete_feed(self, feed_id: int) -> None: - self.cur.execute('DELETE FROM feeds WHERE id = :feed_id', {'feed_id': feed_id}) + self.cur.execute('DELETE FROM feeds WHERE id = ?', (feed_id,)) self.conn.commit() def get_feeds(): -- 2.43.5 From 1c105e6ba0342af5a02e53476ab895b3254c54b3 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 28 May 2022 22:06:34 +0000 Subject: [PATCH 13/20] add unsubscribe_user method --- database.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/database.py b/database.py index e8ce88f..d4021d6 100644 --- a/database.py +++ b/database.py @@ -25,10 +25,14 @@ class Database(): self.conn.commit() return self.cur.lastrowid - def subscribe_user(self, user_id: int, feed_id: int): + def subscribe_user(self, user_id: int, feed_id: int) -> None: self.cur.execute('INSERT INTO subscribes (user_id, feed_id) VALUES (?, ?)', (user_id, feed_id)) self.conn.commit() + def unsubscribe_user(self, user_id: int, feed_id: int) -> None: + self.cur.execute('DELETE FROM subscribes WHERE feed_id = ? AND user_id = ?', (feed_id, user_id)) + self.conn.commit() + def delete_feed(self, feed_id: int) -> None: self.cur.execute('DELETE FROM feeds WHERE id = ?', (feed_id,)) self.conn.commit() -- 2.43.5 From a07011ced7e390b08d63132da6553057603ae681 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 28 May 2022 22:22:41 +0000 Subject: [PATCH 14/20] add get_feeds method --- database.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/database.py b/database.py index d4021d6..13ef081 100644 --- a/database.py +++ b/database.py @@ -37,9 +37,10 @@ class Database(): self.cur.execute('DELETE FROM feeds WHERE id = ?', (feed_id,)) self.conn.commit() - def get_feeds(): - pass - + def get_feeds(self) -> list: + self.cur.execute('SELECT * FROM feeds') + return self.cur.fetchall() + def get_users_subscribes(): pass -- 2.43.5 From f1d2ab3b9bf5bb54f23e7d9ed838e1b2b02b8323 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sat, 28 May 2022 22:47:19 +0000 Subject: [PATCH 15/20] add get_users_subscribes method --- database.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/database.py b/database.py index 13ef081..70bc8a0 100644 --- a/database.py +++ b/database.py @@ -41,8 +41,9 @@ class Database(): self.cur.execute('SELECT * FROM feeds') return self.cur.fetchall() - def get_users_subscribes(): - pass + def get_users_subscribes(self, user_id: int) -> list: + self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscribes WHERE user_id = ?)', (user_id,)) + return self.cur.fetchall() def get_feed_items(): pass -- 2.43.5 From 49678a4d6ba0d02f78f3aad995379078f2a1488f Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 14:04:35 +0000 Subject: [PATCH 16/20] rename get_users_subscribes to get_user_feeds method and rename subscribes table to subscriptions --- database.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/database.py b/database.py index 70bc8a0..2ed0774 100644 --- a/database.py +++ b/database.py @@ -5,44 +5,45 @@ class Database(): 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))') - self.cur.execute('CREATE TABLE IF NOT EXISTS subscribes (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))') + self.cur.execute('CREATE TABLE IF NOT EXISTS subscriptions (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))') + self.cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS subscriptions_index ON subscriptions (user_id, feed_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: - 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() return self.cur.lastrowid def find_user(self, telegram_id: str) -> int | None: - 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() if row is None: return None return row[0] def add_feed(self, url: str) -> int: - self.cur.execute('INSERT INTO feeds (url) VALUES (?)', (url,)) + self.cur.execute('INSERT INTO feeds (url) VALUES (?)', [url]) self.conn.commit() return self.cur.lastrowid def subscribe_user(self, user_id: int, feed_id: int) -> None: - self.cur.execute('INSERT INTO subscribes (user_id, feed_id) VALUES (?, ?)', (user_id, feed_id)) + self.cur.execute('INSERT INTO subscriptions (user_id, feed_id) VALUES (?, ?)', [user_id, feed_id]) self.conn.commit() def unsubscribe_user(self, user_id: int, feed_id: int) -> None: - self.cur.execute('DELETE FROM subscribes WHERE feed_id = ? AND user_id = ?', (feed_id, user_id)) + self.cur.execute('DELETE FROM subscriptions WHERE feed_id = ? AND user_id = ?', [feed_id, user_id]) self.conn.commit() def delete_feed(self, feed_id: int) -> None: - self.cur.execute('DELETE FROM feeds WHERE id = ?', (feed_id,)) + self.cur.execute('DELETE FROM feeds WHERE id = ?', [feed_id]) self.conn.commit() def get_feeds(self) -> list: self.cur.execute('SELECT * FROM feeds') return self.cur.fetchall() - def get_users_subscribes(self, user_id: int) -> list: - self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscribes WHERE user_id = ?)', (user_id,)) + def get_user_feeds(self, user_id: int) -> list: + self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscriptions WHERE user_id = ?)', [user_id]) return self.cur.fetchall() def get_feed_items(): -- 2.43.5 From dc3889635e74a52ab55fa3d3483d824cab8e7a8d Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 14:18:07 +0000 Subject: [PATCH 17/20] add find_feed_items method and rename get_feeds to find_feeds, get_user_feeds to find_user_feeds methods --- database.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/database.py b/database.py index 2ed0774..5bf8478 100644 --- a/database.py +++ b/database.py @@ -8,6 +8,7 @@ class Database(): self.cur.execute('CREATE TABLE IF NOT EXISTS subscriptions (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))') self.cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS subscriptions_index ON subscriptions (user_id, feed_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))') + self.cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS feed_item_index ON feeds_last_items (feed_id, date)') def add_user(self, telegram_id: str) -> int: self.cur.execute('INSERT INTO users (telegram_id) VALUES (?)', [telegram_id]) @@ -38,16 +39,14 @@ class Database(): self.cur.execute('DELETE FROM feeds WHERE id = ?', [feed_id]) self.conn.commit() - def get_feeds(self) -> list: + def find_feeds(self) -> list: self.cur.execute('SELECT * FROM feeds') return self.cur.fetchall() - def get_user_feeds(self, user_id: int) -> list: + def find_user_feeds(self, user_id: int) -> list: self.cur.execute('SELECT * FROM feeds WHERE id IN (SELECT feed_id FROM subscriptions WHERE user_id = ?)', [user_id]) return self.cur.fetchall() - def get_feed_items(): - pass - - def storing_feed_content(): - pass \ No newline at end of file + def find_feed_items(self, feed_id: int) -> list: + self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = (?)', [feed_id]) + return self.cur.fetchall() \ No newline at end of file -- 2.43.5 From b31cfb99aa657d2dca2895b9b5fe4c40d503ff65 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 15:16:48 +0000 Subject: [PATCH 18/20] add update_feed_items method --- database.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/database.py b/database.py index 5bf8478..38191aa 100644 --- a/database.py +++ b/database.py @@ -48,5 +48,13 @@ class Database(): return self.cur.fetchall() def find_feed_items(self, feed_id: int) -> list: - self.cur.execute('SELECT * FROM feeds_last_items WHERE feed_id = (?)', [feed_id]) - return self.cur.fetchall() \ No newline at end of file + 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: + for i in range(len(new_items)): + new_items[i] = (feed_id,) + new_items[i] + + 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) + self.conn.commit() \ No newline at end of file -- 2.43.5 From 0dc20fc09871649fe19478022758bafb8a1e9104 Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 17:36:40 +0000 Subject: [PATCH 19/20] add docstrings --- database.py | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/database.py b/database.py index 38191aa..e7e14fb 100644 --- a/database.py +++ b/database.py @@ -1,21 +1,35 @@ import sqlite3 +""" +Classes: +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. + """ def __init__(self, path: str) -> None: + """Create a database file if not exists.""" self.conn = sqlite3.connect(path) 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))') - self.cur.execute('CREATE TABLE IF NOT EXISTS subscriptions (user_id INTEGER, feed_id INTEGER, FOREIGN KEY(user_id) REFERENCES users(id), FOREIGN KEY(feed_id) REFERENCES feeds(id))') - self.cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS subscriptions_index ON subscriptions (user_id, feed_id)') + 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))') - self.cur.execute('CREATE UNIQUE INDEX IF NOT EXISTS feed_item_index ON feeds_last_items (feed_id, date)') def add_user(self, telegram_id: str) -> int: + """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]) 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.""" self.cur.execute('SELECT id FROM users WHERE telegram_id = ?', [telegram_id]) row = self.cur.fetchone() if row is None: @@ -23,38 +37,46 @@ class Database(): return row[0] def add_feed(self, url: str) -> int: + """Add a feed from to the database and return its id.""" self.cur.execute('INSERT INTO feeds (url) VALUES (?)', [url]) self.conn.commit() return self.cur.lastrowid def subscribe_user(self, user_id: int, feed_id: int) -> None: + """Subscribe a user to the feed.""" self.cur.execute('INSERT INTO subscriptions (user_id, feed_id) VALUES (?, ?)', [user_id, feed_id]) self.conn.commit() def unsubscribe_user(self, user_id: int, feed_id: int) -> None: + """Unsubscribe a user from the feed.""" self.cur.execute('DELETE FROM subscriptions WHERE feed_id = ? AND user_id = ?', [feed_id, user_id]) self.conn.commit() def delete_feed(self, feed_id: int) -> None: + """Delete a feed from the database.""" 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.""" self.cur.execute('SELECT * FROM feeds') return self.cur.fetchall() - + def find_user_feeds(self, user_id: int) -> list: + """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]) return self.cur.fetchall() def find_feed_items(self, feed_id: int) -> list: + """Get feed items and return it.""" 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.""" for i in range(len(new_items)): new_items[i] = (feed_id,) + new_items[i] - + 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) - self.conn.commit() \ No newline at end of file + self.conn.commit() -- 2.43.5 From c9ae6dcc78f81de6c86e3b94f6feacd7624a495b Mon Sep 17 00:00:00 2001 From: mitsuha_s Date: Sun, 29 May 2022 23:06:07 +0000 Subject: [PATCH 20/20] correct docstrings --- .gitignore | 2 +- database.py | 24 +++++++++--------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index 8b2d7e0..4bb55d6 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,4 @@ /.venv # Database -/bot.db \ No newline at end of file +/*.db \ No newline at end of file diff --git a/database.py b/database.py index e7e14fb..f8f4542 100644 --- a/database.py +++ b/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] -- 2.43.5