Somewhat workig search and thumbnails for channels

main
Micke Nordin 3 years ago
parent cf54794220
commit 14ad0a4775
Signed by: micke
GPG Key ID: 0DA0A7A5708FE257

@ -3,17 +3,17 @@ import wx
from Channel import Channel from Channel import Channel
from Items import Item from Items import Item
from Utils import (add_video, get_default_logo, get_latest_video_timestamp, hash_string, from Utils import add_video, hash_string, make_bitmap_from_url, video_exists
make_bitmap_from_url, video_exists)
#from youtubesearchpython.search import VideosSearch #from youtubesearchpython.search import VideosSearch
class YouTube(Channel): class YouTube(Channel):
def __init__(self, channel_id: str, name: str) -> None: def __init__(self, channel_id: str, name: str, logo: wx.Bitmap) -> None:
self.m_name = name self.m_name = name
rss_url = 'https://www.youtube.com/feeds/videos.xml?channel_id={}'.format( rss_url = 'https://www.youtube.com/feeds/videos.xml?channel_id={}'.format(
channel_id) channel_id)
self.m_logo = get_default_logo('YouTube') self.m_logo = logo
self.m_items: list[Item] = list() self.m_items: list[Item] = list()
super().__init__(channel_id, 'YouTube', rss_url, self.m_logo, name) super().__init__(channel_id, 'YouTube', rss_url, self.m_logo, name)
@ -41,6 +41,7 @@ class YouTube(Channel):
item = Item(description, resolved_link, self.m_provider_name, item = Item(description, resolved_link, self.m_provider_name,
published_parsed, thumbnail, title) published_parsed, thumbnail, title)
self.m_items.append(item) self.m_items.append(item)
add_video(video_id, self.m_id, self.m_provider_name,
description, resolved_link, published_parsed, thumbnail, add_video(video_id, self.m_id, self.m_provider_name, description,
title, 0)
resolved_link, published_parsed, thumbnail, title, 0)

@ -8,6 +8,7 @@ from datetime import datetime
from os import environ, makedirs, path from os import environ, makedirs, path
from typing import Callable, Union from typing import Callable, Union
from urllib.parse import urlparse from urllib.parse import urlparse
from youtubesearchpython import ChannelsSearch
import requests import requests
import wx import wx
@ -31,18 +32,26 @@ def add_subscription(channel_id: str,
basepath: str = BASEPATH, basepath: str = BASEPATH,
filename: str = DB_FILE_NAME) -> None: filename: str = DB_FILE_NAME) -> None:
fullpath = path.join(basepath, filename) fullpath = path.join(basepath, filename)
if not path.isdir(basepath): thumbpath = path.join(basepath, 'thumbnails')
makedirs(basepath) thumbnail = path.join(thumbpath, channel_id)
fullpath = path.join(basepath, filename)
if not path.isdir(thumbpath):
makedirs(thumbpath)
if not path.isfile(thumbnail):
channels_search = ChannelsSearch(name, limit=1).result()['result'][0] #type: ignore
bitmap = make_bitmap_from_url('https:' + channels_search['thumbnails'][0]['url'])
bitmap.SaveFile(thumbnail, wx.BITMAP_TYPE_PNG)
con = sqlite3.connect(fullpath) con = sqlite3.connect(fullpath)
cur = con.cursor() cur = con.cursor()
create_query: str = '''CREATE TABLE IF NOT EXISTS {} create_query: str = '''CREATE TABLE IF NOT EXISTS {}
(channel_id TEXT PRIMARY KEY, channel_name TEXT)'''.format(SUB_TABLE) (channel_id TEXT PRIMARY KEY, channel_name TEXT, thumb_path TEXT)'''.format(
SUB_TABLE)
cur.execute(create_query) cur.execute(create_query)
con.commit() con.commit()
upsert_query: str = '''INSERT INTO {} (channel_id, channel_name) upsert_query: str = '''INSERT INTO {} (channel_id, channel_name, thumb_path)
VALUES('{}',"{}") ON CONFLICT(channel_id) DO NOTHING'''.format( VALUES(?,?,?) ON CONFLICT(channel_id) DO NOTHING'''.format(
SUB_TABLE, channel_id, name) SUB_TABLE )
cur.execute(upsert_query) cur.execute(upsert_query, [channel_id, name, thumbnail])
con.commit() con.commit()
@ -227,23 +236,23 @@ def make_sized_button(parent_pnl: wx.Panel, bitmap_or_str: Union[wx.Bitmap,
bitmap = wx.Bitmap(bitmap_or_str, wx.BITMAP_TYPE_ANY) bitmap = wx.Bitmap(bitmap_or_str, wx.BITMAP_TYPE_ANY)
else: else:
bitmap = bitmap_or_str bitmap = bitmap_or_str
btn_style = wx.BORDER_NONE | wx.BU_AUTODRAW | wx.BU_EXACTFIT | wx.BU_NOTEXT btn_logo_style = wx.BU_AUTODRAW | wx.BU_EXACTFIT | wx.BU_NOTEXT | wx.BORDER_NONE#| wx.BOTTOM | wx.RIGHT | wx.TOP
btn_logo = wx.BitmapButton(parent_pnl, btn_logo = wx.BitmapButton(parent_pnl,
wx.ID_ANY, wx.ID_ANY,
bitmap, bitmap,
style=btn_style, style=btn_logo_style,
size=bitmap.GetSize()) size=SIZE)
btn_logo.SetToolTip(text) btn_logo.SetToolTip(text)
btn_sizer.Add(btn_logo, 0, wx.BOTTOM | wx.EXPAND | wx.LEFT | wx.TOP, 1) btn_sizer.Add(btn_logo, 0, wx.EXPAND, 1)
btn_text = wx.Button(parent_pnl, btn_text = wx.Button(parent_pnl,
wx.ID_ANY, wx.ID_ANY,
text, text,
style=wx.BORDER_NONE | wx.BU_AUTODRAW, style=wx.BU_AUTODRAW | wx.BOTTOM | wx.LEFT | wx.TOP | wx.BORDER_NONE,
size=wx.Size(SCREEN_WIDTH - SIZE.GetWidth(), size=wx.Size(SCREEN_WIDTH - SIZE.GetWidth(),
SIZE.GetHeight())) SIZE.GetHeight()))
btn_text.SetToolTip(text) btn_text.SetToolTip(text)
btn_sizer.Add(btn_text, 0, wx.BOTTOM | wx.RIGHT | wx.TOP | wx.EXPAND, 1) btn_sizer.Add(btn_text, 0, wx.EXPAND, 1)
parent_pnl.Bind(wx.EVT_BUTTON, callback, btn_logo) parent_pnl.Bind(wx.EVT_BUTTON, callback, btn_logo)
parent_pnl.Bind(wx.EVT_BUTTON, callback, btn_text) parent_pnl.Bind(wx.EVT_BUTTON, callback, btn_text)
@ -255,8 +264,8 @@ def make_bitmap_from_url(logo_url: str, size: wx.Size = SIZE) -> wx.Bitmap:
content = res.content content = res.content
content_bytes = io.BytesIO(content) content_bytes = io.BytesIO(content)
image = wx.Image(content_bytes, type=wx.BITMAP_TYPE_ANY, index=-1) image = wx.Image(content_bytes, type=wx.BITMAP_TYPE_ANY, index=-1)
scale_factor = image.GetWidth() / SCREEN_WIDTH scale_factor = image.GetWidth() / size.GetWidth()
size.SetWidth(SCREEN_WIDTH) size.SetWidth(image.GetWidth() / scale_factor)
height = image.GetHeight() height = image.GetHeight()
size.SetHeight(height / scale_factor) size.SetHeight(height / scale_factor)
image.Rescale(size.GetWidth(), size.GetHeight()) image.Rescale(size.GetWidth(), size.GetHeight())
@ -265,11 +274,11 @@ def make_bitmap_from_url(logo_url: str, size: wx.Size = SIZE) -> wx.Bitmap:
def make_bitmap_from_file(path, size: wx.Size = SIZE) -> wx.Bitmap: def make_bitmap_from_file(path, size: wx.Size = SIZE) -> wx.Bitmap:
image = wx.Image(path, type=wx.BITMAP_TYPE_ANY, index=-1) image = wx.Image(path, type=wx.BITMAP_TYPE_ANY, index=-1)
# scale_factor = image.GetWidth() / SCREEN_WIDTH scale_factor = image.GetWidth() / size.GetWidth()
# size.SetWidth(SCREEN_WIDTH) size.SetWidth(image.GetWidth() / scale_factor)
# height = image.GetHeight() height = image.GetHeight()
# size.SetHeight(height/scale_factor) size.SetHeight(height / scale_factor)
# image.Rescale(size.GetWidth(), size.GetHeight() ) image.Rescale(size.GetWidth(), size.GetHeight())
return wx.Bitmap(image) return wx.Bitmap(image)

@ -12,8 +12,8 @@ from youtubesearchpython import ChannelsSearch
from Channel import SVT, YouTube from Channel import SVT, YouTube
from ChannelProvider import ChannelProvider from ChannelProvider import ChannelProvider
from Utils import (get_subscriptions, import_from_newpipe, make_sized_button, from Utils import (get_default_logo, get_subscriptions, import_from_newpipe, make_bitmap_from_file, make_sized_button,
resolve_youtube_link) resolve_youtube_link, make_bitmap_from_url)
WIDTH = int(720 / 2) WIDTH = int(720 / 2)
HEIGHT = int(1440 / 2) HEIGHT = int(1440 / 2)
@ -64,8 +64,10 @@ class Cast(wx.Frame):
reply = text.GetLineText(0) reply = text.GetLineText(0)
text.Clear() text.Clear()
channels_search = ChannelsSearch(reply, channels_search = ChannelsSearch(reply,
limit=1).result()['result'][0] limit=1).result()['result'][0] #type: ignore
print(channels_search) if 'id' in channels_search:
found_channel = YouTube.YouTube(channels_search['id'], channels_search['title'], logo=make_bitmap_from_url('https:' + channels_search['thumbnails'][0]['url'], size=wx.Size(68,100))) #type: ignore
self.m_selected_provider.append_channel(found_channel)
text: wx.TextCtrl = wx.TextCtrl(self.m_panel, size=(WIDTH, BTN_HEIGHT)) text: wx.TextCtrl = wx.TextCtrl(self.m_panel, size=(WIDTH, BTN_HEIGHT))
self.m_sizer.Add(text) self.m_sizer.Add(text)
@ -177,10 +179,12 @@ class Cast(wx.Frame):
subscriptions = get_subscriptions() subscriptions = get_subscriptions()
if subscriptions: if subscriptions:
for channel in subscriptions: for channel in subscriptions:
channels.append(YouTube.YouTube(channel[0], channel[1])) logo = make_bitmap_from_file(channel[2])
channels.append(YouTube.YouTube(channel[0], channel[1], logo))
else: else:
logo = make_bitmap_from_url('https://yt3.ggpht.com/ytc/AKedOLQ5L9xUSDxB2j6V3VC8L_HEwiKeHM21CgbSUyqe=s88-c-k-c0x00ffffff-no-rj')
channels.append( channels.append(
YouTube.YouTube("UCs6A_0Jm21SIvpdKyg9Gmxw", "Pine 64")) YouTube.YouTube("UCs6A_0Jm21SIvpdKyg9Gmxw", "Pine 64",logo))
youtube = ChannelProvider("YouTube", channels=channels) youtube = ChannelProvider("YouTube", channels=channels)
providers.append(youtube) providers.append(youtube)
@ -201,11 +205,12 @@ class Cast(wx.Frame):
# Proceed loading the file chosen by the user # Proceed loading the file chosen by the user
subfile = file_dialog.GetPath() subfile = file_dialog.GetPath()
channels = list() channels = list()
logo = get_default_logo('YouTube')
if os.path.isfile(subfile): if os.path.isfile(subfile):
import_from_newpipe(subfile) import_from_newpipe(subfile)
subscriptions = get_subscriptions() subscriptions = get_subscriptions()
for channel in subscriptions: for channel in subscriptions:
yt_chan = YouTube.YouTube(channel[0], channel[1]) yt_chan = YouTube.YouTube(channel[0], channel[1], logo)
yt_chan.refresh() yt_chan.refresh()
channels.append(yt_chan) channels.append(yt_chan)
# Index 1 is YouTube # Index 1 is YouTube

Loading…
Cancel
Save