From 14ad0a4775ec038d04c4c27c69ee92f4bfa390ec Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Thu, 13 Jan 2022 18:12:26 +0100 Subject: [PATCH] Somewhat workig search and thumbnails for channels --- src/Channel/YouTube/__init__.py | 15 +++++----- src/Utils/__init__.py | 49 +++++++++++++++++++-------------- src/main.py | 19 ++++++++----- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/src/Channel/YouTube/__init__.py b/src/Channel/YouTube/__init__.py index d5fe146..61dda19 100644 --- a/src/Channel/YouTube/__init__.py +++ b/src/Channel/YouTube/__init__.py @@ -3,17 +3,17 @@ import wx from Channel import Channel from Items import Item -from Utils import (add_video, get_default_logo, get_latest_video_timestamp, hash_string, - make_bitmap_from_url, video_exists) +from Utils import add_video, hash_string, make_bitmap_from_url, video_exists + #from youtubesearchpython.search import VideosSearch 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 rss_url = 'https://www.youtube.com/feeds/videos.xml?channel_id={}'.format( channel_id) - self.m_logo = get_default_logo('YouTube') + self.m_logo = logo self.m_items: list[Item] = list() 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, published_parsed, thumbnail, title) self.m_items.append(item) - add_video(video_id, self.m_id, self.m_provider_name, - description, resolved_link, published_parsed, thumbnail, - title, 0) + + add_video(video_id, self.m_id, self.m_provider_name, description, + + resolved_link, published_parsed, thumbnail, title, 0) diff --git a/src/Utils/__init__.py b/src/Utils/__init__.py index ff8a21b..5afe83c 100644 --- a/src/Utils/__init__.py +++ b/src/Utils/__init__.py @@ -8,6 +8,7 @@ from datetime import datetime from os import environ, makedirs, path from typing import Callable, Union from urllib.parse import urlparse +from youtubesearchpython import ChannelsSearch import requests import wx @@ -31,18 +32,26 @@ def add_subscription(channel_id: str, basepath: str = BASEPATH, filename: str = DB_FILE_NAME) -> None: fullpath = path.join(basepath, filename) - if not path.isdir(basepath): - makedirs(basepath) + thumbpath = path.join(basepath, 'thumbnails') + 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) cur = con.cursor() 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) con.commit() - upsert_query: str = '''INSERT INTO {} (channel_id, channel_name) - VALUES('{}',"{}") ON CONFLICT(channel_id) DO NOTHING'''.format( - SUB_TABLE, channel_id, name) - cur.execute(upsert_query) + upsert_query: str = '''INSERT INTO {} (channel_id, channel_name, thumb_path) + VALUES(?,?,?) ON CONFLICT(channel_id) DO NOTHING'''.format( + SUB_TABLE ) + cur.execute(upsert_query, [channel_id, name, thumbnail]) 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) else: 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, wx.ID_ANY, bitmap, - style=btn_style, - size=bitmap.GetSize()) + style=btn_logo_style, + size=SIZE) 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, wx.ID_ANY, 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.GetHeight())) 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_text) @@ -255,8 +264,8 @@ def make_bitmap_from_url(logo_url: str, size: wx.Size = SIZE) -> wx.Bitmap: content = res.content content_bytes = io.BytesIO(content) image = wx.Image(content_bytes, type=wx.BITMAP_TYPE_ANY, index=-1) - scale_factor = image.GetWidth() / SCREEN_WIDTH - size.SetWidth(SCREEN_WIDTH) + scale_factor = image.GetWidth() / size.GetWidth() + size.SetWidth(image.GetWidth() / scale_factor) height = image.GetHeight() size.SetHeight(height / scale_factor) 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: image = wx.Image(path, type=wx.BITMAP_TYPE_ANY, index=-1) - # scale_factor = image.GetWidth() / SCREEN_WIDTH - # size.SetWidth(SCREEN_WIDTH) - # height = image.GetHeight() - # size.SetHeight(height/scale_factor) - # image.Rescale(size.GetWidth(), size.GetHeight() ) + scale_factor = image.GetWidth() / size.GetWidth() + size.SetWidth(image.GetWidth() / scale_factor) + height = image.GetHeight() + size.SetHeight(height / scale_factor) + image.Rescale(size.GetWidth(), size.GetHeight()) return wx.Bitmap(image) diff --git a/src/main.py b/src/main.py index 846427c..da155f7 100644 --- a/src/main.py +++ b/src/main.py @@ -12,8 +12,8 @@ from youtubesearchpython import ChannelsSearch from Channel import SVT, YouTube from ChannelProvider import ChannelProvider -from Utils import (get_subscriptions, import_from_newpipe, make_sized_button, - resolve_youtube_link) +from Utils import (get_default_logo, get_subscriptions, import_from_newpipe, make_bitmap_from_file, make_sized_button, + resolve_youtube_link, make_bitmap_from_url) WIDTH = int(720 / 2) HEIGHT = int(1440 / 2) @@ -64,8 +64,10 @@ class Cast(wx.Frame): reply = text.GetLineText(0) text.Clear() channels_search = ChannelsSearch(reply, - limit=1).result()['result'][0] - print(channels_search) + limit=1).result()['result'][0] #type: ignore + 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)) self.m_sizer.Add(text) @@ -177,10 +179,12 @@ class Cast(wx.Frame): subscriptions = get_subscriptions() if 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: + logo = make_bitmap_from_url('https://yt3.ggpht.com/ytc/AKedOLQ5L9xUSDxB2j6V3VC8L_HEwiKeHM21CgbSUyqe=s88-c-k-c0x00ffffff-no-rj') channels.append( - YouTube.YouTube("UCs6A_0Jm21SIvpdKyg9Gmxw", "Pine 64")) + YouTube.YouTube("UCs6A_0Jm21SIvpdKyg9Gmxw", "Pine 64",logo)) youtube = ChannelProvider("YouTube", channels=channels) providers.append(youtube) @@ -201,11 +205,12 @@ class Cast(wx.Frame): # Proceed loading the file chosen by the user subfile = file_dialog.GetPath() channels = list() + logo = get_default_logo('YouTube') if os.path.isfile(subfile): import_from_newpipe(subfile) subscriptions = get_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() channels.append(yt_chan) # Index 1 is YouTube