plugin.video.viervijfzes/resources/lib/modules/tvguide.py

184 lines
6.3 KiB
Python
Raw Permalink Normal View History

2020-03-19 16:45:31 +01:00
# -*- coding: utf-8 -*-
""" Menu code related to channels """
from __future__ import absolute_import, division, unicode_literals
import logging
from datetime import datetime, timedelta
from resources.lib import kodiutils
from resources.lib.kodiutils import TitleItem
from resources.lib.modules.player import Player
2020-03-19 16:45:31 +01:00
from resources.lib.viervijfzes import STREAM_DICT
from resources.lib.viervijfzes.content import UnavailableException
from resources.lib.viervijfzes.epg import EpgApi
try: # Python 3
from urllib.parse import quote
except ImportError: # Python 2
from urllib import quote
_LOGGER = logging.getLogger(__name__)
2020-03-19 16:45:31 +01:00
class TvGuide:
""" Menu code related to the TV Guide """
def __init__(self):
""" Initialise object """
self._epg = EpgApi()
@staticmethod
def get_dates(date_format):
""" Return a dict of dates.
:rtype: list[dict]
"""
dates = []
today = datetime.today()
# The API provides 7 days in the past and 13 days in the future
for i in range(-7, 13):
day = today + timedelta(days=i)
if i == -1:
dates.append({
'title': '%s, %s' % (kodiutils.localize(30301), day.strftime(date_format)), # Yesterday
'key': 'yesterday',
'date': day.strftime('%d.%m.%Y'),
'highlight': False,
})
elif i == 0:
dates.append({
'title': '%s, %s' % (kodiutils.localize(30302), day.strftime(date_format)), # Today
'key': 'today',
'date': day.strftime('%d.%m.%Y'),
'highlight': True,
})
elif i == 1:
dates.append({
'title': '%s, %s' % (kodiutils.localize(30303), day.strftime(date_format)), # Tomorrow
'key': 'tomorrow',
'date': day.strftime('%d.%m.%Y'),
'highlight': False,
})
else:
dates.append({
'title': day.strftime(date_format),
'key': day.strftime('%Y-%m-%d'),
'date': day.strftime('%d.%m.%Y'),
'highlight': False,
})
return dates
2020-04-20 08:59:10 +02:00
def show_channel(self, channel):
2020-03-19 16:45:31 +01:00
""" Shows the dates in the tv guide
:type channel: str
"""
listing = []
for day in self.get_dates('%A %d %B %Y'):
if day.get('highlight'):
title = '[B]{title}[/B]'.format(title=day.get('title'))
else:
title = day.get('title')
listing.append(
TitleItem(title=title,
2020-04-20 08:59:10 +02:00
path=kodiutils.url_for('show_channel_tvguide_detail', channel=channel, date=day.get('key')),
2020-03-19 16:45:31 +01:00
art_dict={
'icon': 'DefaultYear.png',
'thumb': 'DefaultYear.png',
},
info_dict={
'plot': None,
'date': day.get('date'),
})
)
kodiutils.show_listing(listing, 30013, content='files', sort=['date'])
2020-04-20 08:59:10 +02:00
def show_detail(self, channel=None, date=None):
2020-03-19 16:45:31 +01:00
""" Shows the programs of a specific date in the tv guide
:type channel: str
:type date: str
"""
try:
programs = self._epg.get_epg(channel=channel, date=date)
except UnavailableException as ex:
kodiutils.notification(message=str(ex))
kodiutils.end_of_directory()
return
listing = []
for program in programs:
if program.program_url:
context_menu = [(
kodiutils.localize(30102), # Go to Program
'Container.Update(%s)' %
kodiutils.url_for('show_catalog_program', channel=channel, program=program.program_url)
)]
else:
context_menu = None
title = '{time} - {title}'.format(
time=program.start.strftime('%H:%M'),
title=program.program_title
)
if program.airing:
title = '[B]{title}[/B]'.format(title=title)
if program.video_url:
path = kodiutils.url_for('play_from_page', channel=channel, page=quote(program.video_url, safe=''))
else:
path = None
title = '[COLOR gray]' + title + '[/COLOR]'
2020-03-24 23:29:16 +01:00
stream_dict = STREAM_DICT.copy()
stream_dict.update({
'duration': program.duration,
})
info_dict = {
'title': title,
'plot': program.description,
'studio': program.channel,
'duration': program.duration,
'tvshowtitle': program.program_title,
'season': program.season,
'episode': program.number,
'mediatype': 'episode',
}
2020-03-19 16:45:31 +01:00
listing.append(
TitleItem(title=title,
path=path,
art_dict={
'thumb': program.thumb,
2020-03-19 16:45:31 +01:00
},
2020-03-24 23:29:16 +01:00
info_dict=info_dict,
2020-03-20 15:05:49 +01:00
stream_dict=stream_dict,
2020-03-19 16:45:31 +01:00
context_menu=context_menu,
is_playable=True)
)
kodiutils.show_listing(listing, 30013, content='episodes', sort=['unsorted'])
def play_epg_datetime(self, channel, timestamp):
""" Play a program based on the channel and the timestamp when it was aired
:type channel: str
:type timestamp: str
"""
broadcast = self._epg.get_broadcast(channel, timestamp)
if not broadcast:
kodiutils.ok_dialog(message=kodiutils.localize(30713)) # The requested video was not found in the guide.
2020-03-19 16:45:31 +01:00
kodiutils.end_of_directory()
return
if not broadcast.video_url:
kodiutils.ok_dialog(message=kodiutils.localize(30712)) # The video is unavailable and can't be played right now.
kodiutils.end_of_directory()
return
Player().play_from_page(broadcast.video_url)