From c1547de061f586b304741a680f95aa371924af7f Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Fri, 7 Jan 2022 21:00:09 +0100 Subject: [PATCH] Start working on videos in database --- src/Utils/__init__.py | 54 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Utils/__init__.py b/src/Utils/__init__.py index e018c9a..d9926fb 100644 --- a/src/Utils/__init__.py +++ b/src/Utils/__init__.py @@ -2,9 +2,11 @@ import io import json import sqlite3 +from datetime import datetime from os import environ, makedirs, path from typing import Callable, Union from urllib.parse import urlparse +from Items import Item import requests import wx @@ -15,6 +17,7 @@ SCREEN_WIDTH = int(720 / 2) BASEPATH = path.join(str(environ.get("HOME")), '.config/cast') DB_FILE_NAME = 'cast.db' SUB_TABLE = 'subscriptions' +SUB_TABLE = 'videos' def add_subscription(channel_id: str, @@ -31,11 +34,46 @@ def add_subscription(channel_id: str, cur.execute(create_query) con.commit() upsert_query: str = '''INSERT INTO {} (channel_id, channel_name) - VALUES('{}',"{}") ON CONFLICT(channel_id) DO NOTHING'''.format(SUB_TABLE, channel_id, name) + VALUES('{}',"{}") ON CONFLICT(channel_id) DO NOTHING'''.format( + SUB_TABLE, channel_id, name) cur.execute(upsert_query) con.commit() +def add_video(video_id: str, + channel_id: str, + provider_id: str, + description: str, + link: str, + published: datetime, + bitmap: wx.Bitmap, + title: str, + watchtime: str, + basepath: str = BASEPATH, + filename: str = DB_FILE_NAME) -> None: + thumbnail = bitmap.GetData() + fullpath = path.join(basepath, filename) + if not path.isdir(basepath): + makedirs(basepath) + con = sqlite3.connect(fullpath) + cur = con.cursor() + create_query: str = '''CREATE TABLE IF NOT EXISTS {} + (video_id TEXT PRIMARY KEY, channel_id TEXT, provider_id TEXT, + title TEXT, link text, description TEXT, thumbnail BLOB, published DATETIME)'''.format( + VIDEO_TABLE) + cur.execute(create_query) + con.commit() + + upsert_query: str = '''INSERT INTO {} (video_id, channel_id, provider_id, title, link, description, thumbnail, published, watchtime) + VALUES(?,?,?,?,?,?,?,?,?) ON CONFLICT(video_id) DO NOTHING'''.format( + VIDEO_TABLE) + + cur.execute(upsert_query, video_id, channel_id, provider_id, title, link, + description, thumbnail, published, watchtime) + + con.commit() + + def get_default_logo(providerid: str = 'default') -> wx.Bitmap: if providerid == 'SVT': return wx.Bitmap('{}/assets/SVT.png'.format(MYPATH)) @@ -54,10 +92,22 @@ def get_subscriptions(basepath: str = BASEPATH, select_query = '''SELECT * FROM {}'''.format(SUB_TABLE) cur.execute(select_query) for result in cur.fetchall(): - print(result) subscriptions.append(result) return subscriptions +def get_videos(channel_id: str, + basepath: str = BASEPATH, + filename: str = DB_FILE_NAME) -> list[Item]: + videos = list() + fullpath = path.join(basepath, filename) + con = sqlite3.connect(fullpath) + cur = con.cursor() + select_query = '''SELECT * FROM {} WHERE channel_id = ?'''.format(VIDEO_TABLE) + cur.execute(select_query,channel_id) + for result in cur.fetchall(): + pass + # videos.append(Item()) # Make an item from db + return videos def import_from_newpipe(filename) -> None: