tg_rss_bot/database.py

42 lines
1.8 KiB
Python
Raw Normal View History

2022-05-19 21:12:43 +00:00
import sqlite3
class Database():
def __init__(self, path: str) -> None:
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, link TEXT NOT NULL UNIQUE, PRIMARY KEY(id))')
2022-05-19 22:46:55 +00:00
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))')
2022-05-21 10:48:33 +00:00
def add_user(self, telegram_id: str) -> int:
self.cur.execute('INSERT INTO users (telegram_id) VALUES (:telegram_id)', {'telegram_id': 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-22 22:41:37 +00:00
def get_user(self, telegram_id: str) -> int | None:
2022-05-22 21:40:01 +00:00
self.cur.execute('SELECT id FROM users WHERE telegram_id = :telegram_id', {'telegram_id': telegram_id})
2022-05-22 22:02:17 +00:00
user_id = self.cur.fetchone()
2022-05-22 22:41:37 +00:00
if user_id is None:
return None
return user_id[0]
2022-05-22 21:40:01 +00:00
2022-05-22 22:02:17 +00:00
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
2022-05-22 22:41:37 +00:00
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()
2022-05-19 21:12:43 +00:00
def get_feeds():
pass
def get_users_subscribes():
pass
def get_feed_items():
pass
def storing_feed_content():
2022-05-22 21:40:01 +00:00
pass