You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
cast/src/Utils/__init__.py

105 lines
3.7 KiB

#!/usr/bin/env python3
import io
from os import path
3 years ago
from posixpath import relpath
import requests
import wx
from typing import Union, Callable
3 years ago
SIZE = wx.Size(100,68)
MYPATH = path.dirname(path.abspath(__file__))
3 years ago
SCREEN_WIDTH = 720 /2
def get_default_logo(providerid: str = 'default') -> wx.Bitmap:
if providerid == 'SVT':
return wx.Bitmap('{}/assets/SVT.png'.format(MYPATH))
if providerid == 'YouTube':
return wx.Bitmap('{}/assets/YouTube.png'.format(MYPATH))
else:
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)
3 years ago
btn_text.SetMinSize(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)
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, size: wx.Size = SIZE ) -> wx.Bitmap:
res = requests.get(logo_url)
content = res.content
content_bytes = io.BytesIO(content)
image = wx.Image(content_bytes, type=wx.BITMAP_TYPE_ANY, index=-1)
3 years ago
scale_factor = image.GetWidth() / SCREEN_WIDTH
size.SetWidth(SCREEN_WIDTH)
height = image.GetHeight()
size.SetHeight(height/scale_factor)
image.Rescale(size.GetWidth(), size.GetHeight() )
return wx.Bitmap(image)
3 years ago
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() )
return wx.Bitmap(image)
def resolve_svt_channel(svt_id: str) -> dict:
3 years ago
channels = {
"ch-barnkanalen": {
"name":
"Barnkanalen",
3 years ago
"thumbnail": make_bitmap_from_file('{}/assets/Barnkanalen.png'.format(MYPATH))
},
"ch-svt1": {
"name":
"SVT 1",
3 years ago
"thumbnail": make_bitmap_from_file('{}/assets/SVT1.png'.format(MYPATH))
},
"ch-svt2": {
"name":
"SVT 2",
3 years ago
"thumbnail": make_bitmap_from_file('{}/assets/SVT2.png'.format(MYPATH))
},
"ch-svt24": {
"name":
"SVT 24",
3 years ago
"thumbnail": make_bitmap_from_file('{}/assets/SVT24.png'.format(MYPATH))
},
"ch-kunskapskanalen": {
"name":
"Kunskapskanalen",
3 years ago
"thumbnail": make_bitmap_from_file('{}/assets/Kunskapskanalen.png'.format(MYPATH))
},
"feed": {
"name": "Senaste program",
3 years ago
"thumbnail": make_bitmap_from_file('{}/assets/SVT.png'.format(MYPATH))
},
}
return channels[svt_id]