From 1ce7bc3da8daa0a2ada5377d976d5443eae9943f Mon Sep 17 00:00:00 2001 From: Micke Nordin Date: Tue, 28 Dec 2021 18:42:11 +0100 Subject: [PATCH] Create a custom channel for all providers with the latest video from all channels --- Channel/YouTube/__init__.py | 4 +--- Channel/__init__.py | 9 +++++++++ ChannelProvider/__init__.py | 24 ++++++++++++++++++++++-- Utils/__init__.py | 16 ++++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/Channel/YouTube/__init__.py b/Channel/YouTube/__init__.py index a2ede30..0c266ad 100644 --- a/Channel/YouTube/__init__.py +++ b/Channel/YouTube/__init__.py @@ -37,7 +37,7 @@ class YouTube(Channel): kwargs={}) self.m_thr.start() - def set_avatar(self) -> str: + def set_avatar(self) -> None: info = get_info(self.m_channel_id) bmap = self.get_logo_as_bitmap() title = info['title'] @@ -54,8 +54,6 @@ class YouTube(Channel): del dc self.m_logo = bmap - return "" - def wait(self) -> bool: return self.m_thr.is_alive() diff --git a/Channel/__init__.py b/Channel/__init__.py index a9f95ad..4bf793c 100644 --- a/Channel/__init__.py +++ b/Channel/__init__.py @@ -40,3 +40,12 @@ class Channel: def wait(self) -> bool: return False + + def get_latest_item(self) -> Item: + return self.m_items[0] + + def set_items(self, items: list[Item]) -> None: + self.m_items = items + + def set_logo(self, logo: wx.Bitmap) -> None: + self.m_logo = logo diff --git a/ChannelProvider/__init__.py b/ChannelProvider/__init__.py index 57365ab..a2df2fe 100644 --- a/ChannelProvider/__init__.py +++ b/ChannelProvider/__init__.py @@ -1,11 +1,13 @@ #/usr/bin/env python3 import io +import threading +import time import requests import wx from Channel import Channel -from Utils import get_default_log_url, make_bitmap_from_url +from Utils import get_default_log_url, make_bitmap_from_url, write_on_bitmap class ChannelProvider: @@ -14,6 +16,11 @@ class ChannelProvider: self.m_logo_url = get_default_log_url(providerid) self.m_logo: wx.Bitmap = make_bitmap_from_url(self.m_logo_url) self.m_channels: list[Channel] = channels + if len(self.m_channels) > 0: + self.m_thr = threading.Thread(target=self.make_latest, + args=(), + kwargs={}) + self.m_thr.start() def append_channel(self, channel: Channel) -> int: self.m_channels.append(channel) @@ -29,7 +36,20 @@ class ChannelProvider: return self.m_logo def get_name(self) -> str: - return self.m_id_ + return self.m_id def get_logo_url(self) -> str: links: dict = {'ch-svt1'} + + def make_latest(self) -> None: + items = list() + for chan in self.m_channels: + while chan.wait(): + time.sleep(1) + items.append(chan.get_latest_item()) + + channel = Channel(self.get_name, None, self.m_logo_url) + channel.set_items(items) + temp = self.get_logo_as_bitmap() + channel.set_logo(write_on_bitmap("Latest videos from all channels",temp)) + self.append_channel(channel) diff --git a/Utils/__init__.py b/Utils/__init__.py index dd44837..ee8366f 100644 --- a/Utils/__init__.py +++ b/Utils/__init__.py @@ -3,6 +3,7 @@ import io import requests import wx +from copy import copy def get_default_log_url(providerid: str = 'default') -> str: @@ -62,3 +63,18 @@ def resolve_svt_channel(svt_id: str) -> dict: } return channels[svt_id] + +def write_on_bitmap(text: str, bmap: wx.Bitmap) -> wx.Bitmap: + bitmap = wx.Bitmap(bmap) + dc = wx.MemoryDC(bitmap) + cblack = wx.Colour(0, 0, 0) + cwhite = wx.Colour(255, 255, 255) + dc.SetTextForeground(cwhite) + dc.SetTextBackground(cblack) + dc.SetFont(wx.Font().Bold()) + dc.SetBackgroundMode(wx.BRUSHSTYLE_SOLID) + w, h = dc.GetSize() + tw, th = dc.GetTextExtent(text) + dc.DrawText(text, (w - tw) / 2, (h - th) / 2) #display text in center + del dc + return bitmap