switch to peewee ORM
Marco Andronaco andronacomarco@gmail.com
Wed, 23 Aug 2023 12:25:29 +0200
5 files changed,
42 insertions(+),
53 deletions(-)
M
fxyoutube/db.py
→
fxyoutube/db.py
@@ -1,54 +1,40 @@
-import sqlite3 -from contextlib import closing +from peewee import Model, CharField, TextField, IntegerField, DateTimeField, DoesNotExist +from playhouse.sqliteq import SqliteQueueDatabase from datetime import datetime, timedelta from fxyoutube.yt_info import get_info_ytdl import fxyoutube.constants as c -create_query = ''' -CREATE TABLE IF NOT EXISTS videos ( - id TEXT PRIMARY KEY, - title TEXT NOT NULL, - description TEXT NOT NULL, - uploader TEXT NOT NULL, - duration int NOT NULL, - height TEXT NOT NULL, - width TEXT NOT NULL, - url TEXT, - timestamp DATETIME DEFAULT (datetime('now','localtime')) -);''' +db = SqliteQueueDatabase(c.DB_URL) -def execute_query(query: str, attributes: list = []): - with sqlite3.connect(c.DB_URL) as db_connection: - with closing(db_connection.cursor()) as db_cursor: - return list(db_cursor.execute(query, attributes)) +class BaseModel(Model): + class Meta: + database = db -def get_video_db(video_id): - return execute_query("SELECT * FROM videos WHERE id = (?);", [ video_id ]) +class Video(BaseModel): + id = CharField(unique=True) + title = CharField() + description = TextField() + uploader = CharField() + duration = IntegerField() + height = IntegerField() + width = IntegerField() + url = TextField() + timestamp = DateTimeField(default=datetime.now) def cache_video(info): - return execute_query("INSERT OR REPLACE INTO videos (id, title, description, uploader, duration, height, width, url) VALUES (?, ?, ?, ?, ?, ?, ?, ?);", list(info.values())) + return Video.create(**info) def get_video_from_cache(video): - result = get_video_db(video) try: - temp = result[0] - timestamp = datetime.strptime(temp[8], c.TS_FORMAT) - delta = datetime.now() - timestamp - if delta > timedelta(minutes=c.YT_TTL_MINUTES): - raise IndexError - except IndexError: + temp = Video.get(Video.id == video) + except DoesNotExist: + return None + + delta = datetime.now() - temp.timestamp + if delta > timedelta(minutes=c.YT_TTL_MINUTES): return None - return { - "id": temp[0], - "title": temp[1], - "description": temp[2], - "uploader": temp[3], - "duration": temp[4], - "height": temp[5], - "width": temp[6], - "url": temp[7], - } + return temp def get_info(video): info = get_video_from_cache(video)@@ -63,7 +49,7 @@
return info def clear_cache(): - execute_query("DELETE FROM videos;") - execute_query("VACUUM;") + Video.delete().execute() -execute_query(create_query) +db.connect() +db.create_tables([Video])
M
fxyoutube/yt_info.py
→
fxyoutube/yt_info.py
@@ -32,16 +32,8 @@
def get_info_ytdl(yt_id: str): info = ydl.extract_info(c.BASE_URL + yt_id, download=False) - yt_info = { - "id": info["id"], - "title": info["title"], - "description": truncate_lines(info["description"]), - "uploader": info["uploader"], - "duration": info["duration"], - "height": 0, - "width": 0, - "url": None - } + yt_info = { k: truncate_lines(info[k]) for k in ["id", "title", "description", "uploader", "duration"] } + yt_info.update({ "height": 0, "width": 0, "url": None }) formats = map(handle_format, info["formats"]) formats = filter(lambda x: x is not None, formats)
M
poetry.lock
→
poetry.lock
@@ -472,6 +472,16 @@ {file = "mutagen-1.46.0.tar.gz", hash = "sha256:6e5f8ba84836b99fe60be5fb27f84be4ad919bbb6b49caa6ae81e70584b55e58"},
] [[package]] +name = "peewee" +version = "3.16.3" +description = "a little orm" +optional = false +python-versions = "*" +files = [ + {file = "peewee-3.16.3.tar.gz", hash = "sha256:12b30e931193bc37b11f7c2ac646e3f67125a8b1a543ad6ab37ad124c8df7d16"}, +] + +[[package]] name = "pycparser" version = "2.21" description = "C parser in Python"@@ -694,4 +704,4 @@
[metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "df1119f7bc942f5ea9f4d3c879584a600ac52ab374b74d8a33616201307d5f56" +content-hash = "de50625467a5189bd8d766842d16b45c094c8ed2922522e8f813bb6fb1f1078d"
M
pyproject.toml
→
pyproject.toml
@@ -10,6 +10,7 @@ python = "^3.11"
flask = "^2.3.2" requests = "^2.31.0" yt-dlp = "^2023.7.6" +peewee = "^3.16.3" [tool.poetry.group.prod] optional = true