Start working on videos in database

main
Micke Nordin 3 years ago
parent f6628730d6
commit c1547de061

@ -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:

Loading…
Cancel
Save