Use a uniform sizer with two buttons for everything

Also add png images locally
main
Micke Nordin 3 years ago
parent 8407d10cae
commit 735ca3ef7e

@ -26,7 +26,9 @@ class SVT(Channel):
def __init__(self, svt_id: str) -> None: def __init__(self, svt_id: str) -> None:
chan_dict = resolve_svt_channel(svt_id) chan_dict = resolve_svt_channel(svt_id)
super().__init__('SVT', default_rss_url, chan_dict['thumbnail_url']) logo = chan_dict['thumbnail']
name = chan_dict['name']
super().__init__('SVT', default_rss_url,logo,name)
if os.path.exists(self.m_cachefile): if os.path.exists(self.m_cachefile):
with open(self.m_cachefile, 'rb') as cachehandle: with open(self.m_cachefile, 'rb') as cachehandle:
@ -97,7 +99,7 @@ class SVT(Channel):
title = chan_dict['name'] title = chan_dict['name']
published_parsed = datetime.now() published_parsed = datetime.now()
description = "Live channel stream" description = "Live channel stream"
thumbnail = make_bitmap_from_url(chan_dict['thumbnail_url']) thumbnail = chan_dict['thumbnail']
if resolved_link: if resolved_link:
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)

@ -13,7 +13,7 @@ from youtube_dl import YoutubeDL as yt
from Channel import Channel from Channel import Channel
from Items import Item from Items import Item
from Utils import get_default_log_url, make_bitmap_from_url from Utils import get_default_logo, make_bitmap_from_url
class YouTube(Channel): class YouTube(Channel):
@ -22,11 +22,11 @@ class YouTube(Channel):
def __init__(self, channel_id) -> None: def __init__(self, channel_id) -> None:
self.m_channel_id = channel_id self.m_channel_id = channel_id
self.m_info = get_info(channel_id)
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)
logo_url = get_default_log_url('YouTube') self.m_logo = get_default_logo('YouTube')
super().__init__(channel_id, rss_url, logo_url) super().__init__(channel_id, rss_url, self.m_logo, self.m_info['title'])
self.set_avatar()
self.m_items: Union[list[Item], None] = None self.m_items: Union[list[Item], None] = None
if os.path.exists(self.m_cachefile): if os.path.exists(self.m_cachefile):
@ -37,22 +37,6 @@ class YouTube(Channel):
kwargs={}) kwargs={})
self.m_thr.start() self.m_thr.start()
def set_avatar(self) -> None:
info = get_info(self.m_channel_id)
bmap = self.get_logo_as_bitmap()
title = info['title']
dc = wx.MemoryDC(bmap)
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(title)
dc.DrawText(title, (w - tw) / 2, (h - th) / 2) #display text in center
del dc
self.m_logo = bmap
def wait(self) -> bool: def wait(self) -> bool:
return self.m_thr.is_alive() return self.m_thr.is_alive()
@ -112,7 +96,8 @@ class YouTube(Channel):
pickle.dump(self.m_cache, cachehandle) pickle.dump(self.m_cache, cachehandle)
def get_info(channel_id: str) -> str: def get_info(channel_id: str) -> dict:
info: dict = {'title': 'unknown'}
link = 'https://www.youtube.com/channel/{}'.format(channel_id) link = 'https://www.youtube.com/channel/{}'.format(channel_id)
ydl_opts = {'extract_flat': True, 'skip_download': True} ydl_opts = {'extract_flat': True, 'skip_download': True}
with yt(ydl_opts) as ydl: with yt(ydl_opts) as ydl:

@ -1,25 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import io
from typing import Union from typing import Union
import requests
import wx import wx
from Items import Item from Items import Item
from Utils import get_default_log_url, make_bitmap_from_url
class Channel: class Channel:
def __init__(self, def __init__(self,
provider_name: str, provider_name: str,
feed: str, feed: str,
logo_url: str = '') -> None: logo: wx.Bitmap,
if not logo_url: name: str) -> None:
logo_url = get_default_logo_url() self.m_logo = logo
self.m_logo_url = logo_url
self.m_logo = make_bitmap_from_url(self.m_logo_url)
self.m_provider_name = provider_name self.m_provider_name = provider_name
self.m_name = name
self.m_feed = feed self.m_feed = feed
self.m_items: Union[list[Item], None] = None self.m_items: Union[list[Item], None] = None
@ -32,6 +28,9 @@ class Channel:
def get_items(self) -> Union[list[Item], None]: def get_items(self) -> Union[list[Item], None]:
return self.m_items return self.m_items
def get_name(self) -> str:
return self.m_name
def get_provider_name(self) -> str: def get_provider_name(self) -> str:
return self.m_provider_name return self.m_provider_name
@ -42,7 +41,7 @@ class Channel:
return False return False
def get_latest_item(self) -> Item: def get_latest_item(self) -> Item:
return self.m_items[0] return self.m_items[0] # type: ignore
def set_items(self, items: list[Item]) -> None: def set_items(self, items: list[Item]) -> None:
self.m_items = items self.m_items = items

@ -1,20 +1,19 @@
#/usr/bin/env python3 #/usr/bin/env python3
import io
import threading import threading
import time import time
from os import path
import requests
import wx import wx
from Channel import Channel from Channel import Channel
from Utils import get_default_log_url, make_bitmap_from_url, write_on_bitmap from Utils import get_default_logo
MYPATH = path.dirname(path.abspath(__file__))
class ChannelProvider: class ChannelProvider:
def __init__(self, providerid: str, channels=list()): def __init__(self, providerid: str, channels=list()):
self.m_id = providerid self.m_id = providerid
self.m_logo_url = get_default_log_url(providerid) self.m_logo: wx.Bitmap = get_default_logo(providerid)
self.m_logo: wx.Bitmap = make_bitmap_from_url(self.m_logo_url)
self.m_channels: list[Channel] = channels self.m_channels: list[Channel] = channels
if len(self.m_channels) > 0: if len(self.m_channels) > 0:
self.m_thr = threading.Thread(target=self.make_latest, self.m_thr = threading.Thread(target=self.make_latest,
@ -38,9 +37,6 @@ class ChannelProvider:
def get_name(self) -> str: 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: def make_latest(self) -> None:
items = list() items = list()
for chan in self.m_channels: for chan in self.m_channels:
@ -48,8 +44,6 @@ class ChannelProvider:
time.sleep(1) time.sleep(1)
items.append(chan.get_latest_item()) items.append(chan.get_latest_item())
channel = Channel(self.get_name, None, self.m_logo_url) channel = Channel(self.get_name, None, self.m_logo, "Latest videos")
channel.set_items(items) 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) self.append_channel(channel)

@ -1,27 +1,54 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import io import io
from os import path
import requests import requests
import wx import wx
from copy import copy from typing import Union, Callable
SIZE = wx.Size(200,135)
MYPATH = path.dirname(path.abspath(__file__))
def get_default_log_url(providerid: str = 'default') -> str: def get_default_logo(providerid: str = 'default') -> wx.Bitmap:
if providerid == 'SVT': if providerid == 'SVT':
return 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Logotyp_SVT_Play.png/480px-Logotyp_SVT_Play.jpg' return wx.Bitmap('{}/assets/SVT.png'.format(MYPATH))
if providerid == 'YouTube': if providerid == 'YouTube':
return 'https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Youtube.png/480px-Youtube.jpg' return wx.Bitmap('{}/assets/YouTube.png'.format(MYPATH))
else: else:
return 'https://upload.wikimedia.org/wikipedia/commons/thumb/f/fd/Cartoon_Hand_Playing_Multiple_Online_Videos.svg/480px-Cartoon_Hand_Playing_Multiple_Online_Videos.svg.jpg' return wx.Bitmap('{}/assets/Default.png'.format(MYPATH))
def make_sized_button(parent_pnl: wx.Panel, bitmap_or_str: Union[wx.Bitmap,str], text: str, callback: Callable) -> wx.BoxSizer:
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
if type(bitmap_or_str) == type(str):
if bitmap_or_str.startswith('http'): # type: ignore
bitmap = make_bitmap_from_url(bitmap_or_str) # type: ignore
else:
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 = wx.BitmapButton(parent_pnl, wx.ID_ANY, bitmap , style=btn_style)
btn_logo.SetMinSize(SIZE)
btn_logo.SetToolTip(text)
btn_sizer.Add(btn_logo, 0, wx.BOTTOM | wx.EXPAND | wx.LEFT | wx.TOP, 1)
btn_text = wx.Button(parent_pnl, wx.ID_ANY, text, style=wx.BORDER_NONE | wx.BU_AUTODRAW)
btn_text.SetMinSize(SIZE)
btn_text.SetToolTip(text)
btn_sizer.Add(btn_text, 0, wx.BOTTOM | wx.RIGHT | wx.TOP, 1)
parent_pnl.Bind(wx.EVT_BUTTON, callback, btn_logo)
parent_pnl.Bind(wx.EVT_BUTTON, callback, btn_text)
return btn_sizer
def make_bitmap_from_url(logo_url: str = get_default_log_url()) -> wx.Bitmap: def make_bitmap_from_url(logo_url: str ) -> wx.Bitmap:
res = requests.get(logo_url) res = requests.get(logo_url)
content = res.content content = res.content
content_bytes = io.BytesIO(content) content_bytes = io.BytesIO(content)
logo = wx.Image(content_bytes, type=wx.BITMAP_TYPE_ANY, index=-1) image = wx.Image(content_bytes, type=wx.BITMAP_TYPE_ANY, index=-1)
return wx.Bitmap(logo) image = image.Resize(SIZE, (0,0))
return wx.Bitmap(image)
def resolve_svt_channel(svt_id: str) -> dict: def resolve_svt_channel(svt_id: str) -> dict:
@ -29,36 +56,31 @@ def resolve_svt_channel(svt_id: str) -> dict:
"ch-barnkanalen": { "ch-barnkanalen": {
"name": "name":
"Barnkanalen", "Barnkanalen",
"thumbnail_url": "thumbnail": wx.Bitmap('{}/assets/Barnkanalen.png'.format(MYPATH))
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/52/SVT_Barnkanalen_logo_2008%E2%80%932012.svg/480px-SVT_Barnkanalen_logo_2008%E2%80%932012.svg.jpg"
}, },
"ch-svt1": { "ch-svt1": {
"name": "name":
"SVT 1", "SVT 1",
"thumbnail_url": "thumbnail": wx.Bitmap('{}/assets/SVT1.png'.format(MYPATH))
"https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/SVT1_logo_2012.svg/480px-SVT1_logo_2012.svg.jpg"
}, },
"ch-svt2": { "ch-svt2": {
"name": "name":
"SVT 2", "SVT 2",
"thumbnail_url": "thumbnail": wx.Bitmap('{}/assets/SVT2.png'.format(MYPATH))
"https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/SVT2_logo_2012.svg/480px-SVT2_logo_2012.svg.jpg"
}, },
"ch-svt24": { "ch-svt24": {
"name": "name":
"SVT 24", "SVT 24",
"thumbnail_url": "thumbnail": wx.Bitmap('{}/assets/SVT24.png'.format(MYPATH))
"https://upload.wikimedia.org/wikipedia/commons/thumb/8/8f/SVT24_logo.svg/480px-SVT24_logo.svg.jpg"
}, },
"kunskapskanalen": { "kunskapskanalen": {
"name": "name":
"Kunskapskanalen", "Kunskapskanalen",
"thumbnail_url": "thumbnail": wx.Bitmap('{}/assets/Kunskapskanalen.png'.format(MYPATH))
"https://upload.wikimedia.org/wikipedia/commons/thumb/b/b9/Kunskapskanalen_logo.png/480px-Kunskapskanalen_logo.jpg"
}, },
"feed": { "feed": {
"name": "Senaste program", "name": "Senaste program",
"thumbnail_url": get_default_log_url('SVT') "thumbnail": wx.Bitmap('{}/assets/SVT.png'.format(MYPATH))
}, },
} }

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

@ -10,7 +10,7 @@ import wx.media
from Channel import SVT, Channel, YouTube from Channel import SVT, Channel, YouTube
from ChannelProvider import ChannelProvider from ChannelProvider import ChannelProvider
from Utils import make_sized_button
class Cast(wx.Frame): class Cast(wx.Frame):
def __init__(self, *args, **kw): def __init__(self, *args, **kw):
@ -71,14 +71,9 @@ class Cast(wx.Frame):
provider_index = 0 provider_index = 0
for provider in self.m_providers: for provider in self.m_providers:
bitmap = provider.get_logo_as_bitmap() bitmap = provider.get_logo_as_bitmap()
btn = wx.BitmapButton(self.m_panel, id=provider_index, bitmap=bitmap) callback = lambda event, index=provider_index: self.show_channel_list(event, index)
btn.Bind( btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel,bitmap,provider.get_name(),callback)
wx.EVT_BUTTON, self.m_sizer.Add(btn_sizer)
lambda event, index=provider_index: self.show_channel_list(
event, index
),
)
self.m_sizer.Add(btn)
provider_index += 1 provider_index += 1
self.m_panel.SetSizer(self.m_sizer) self.m_panel.SetSizer(self.m_sizer)
@ -97,12 +92,9 @@ class Cast(wx.Frame):
channel_index = 0 channel_index = 0
for channel in self.m_selected_provider.get_channels(): for channel in self.m_selected_provider.get_channels():
bitmap = channel.get_logo_as_bitmap() bitmap = channel.get_logo_as_bitmap()
btn = wx.BitmapButton(self.m_panel, id=channel_index, bitmap=bitmap) callback = lambda event, index=channel_index: self.show_video_list(event, index)
btn.Bind( btn_sizer: wx.BoxSizer = make_sized_button(self.m_panel,bitmap,channel.get_name(),callback)
wx.EVT_BUTTON, self.m_sizer.Add(btn_sizer)
lambda event, index=channel_index: self.show_video_list(event, index),
)
self.m_sizer.Add(btn)
channel_index += 1 channel_index += 1
self.m_panel.SetSizer(self.m_sizer) self.m_panel.SetSizer(self.m_sizer)

Loading…
Cancel
Save