Skip to content

Commit

Permalink
Add TV-Guide information to live stream plot (#234)
Browse files Browse the repository at this point in the history
This adds the previous, current and upcoming live show to the live
stream plot.

This also includes a fix where we would show today's TV-Guide, while the
EPG information goes from 6AM until 6AM. So before 6AM we would show the
previous day's EPG.
  • Loading branch information
dagwieers authored May 16, 2019
1 parent f6f19b7 commit 0eb75a1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
42 changes: 42 additions & 0 deletions resources/lib/vrtplayer/tvguide.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def show_tvguide(self, params):

def show_date_menu(self):
now = datetime.now(dateutil.tz.tzlocal())
# Daily EPG information shows information from 6AM until 6AM
if now.hour < 6:
now += timedelta(days=-1)
date_items = []
for i in range(7, -31, -1):
day = now + timedelta(days=i)
Expand Down Expand Up @@ -171,6 +174,45 @@ def show_episodes(self, date, channel):
))
return episode_items

def episode_description(self, episode):
return '{start} - {end}\n{title}'.format(**episode)

def live_description(self, channel):
now = datetime.now(dateutil.tz.tzlocal())
# Daily EPG information shows information from 6AM until 6AM
if now.hour < 6:
now += timedelta(days=-1)
api_url = now.strftime(self.VRT_TVGUIDE)
self._kodi.log_notice('URL get: ' + api_url, 'Verbose')
schedule = json.loads(urlopen(api_url).read())
name = channel
try:
channel = next(c for c in CHANNELS if c.get('name') == name)
episodes = iter(schedule[channel.get('id')])
except StopIteration:
return ''

description = ''
prev_episode = None
while True:
try:
episode = next(episodes)
except StopIteration:
break
start_date = dateutil.parser.parse(episode.get('startTime'))
end_date = dateutil.parser.parse(episode.get('endTime'))
if start_date < now <= end_date: # Now playing
if prev_episode:
description += '[COLOR gray]%s[/COLOR]\n' % self.episode_description(prev_episode)
description += '[COLOR yellow][B]%s[/B][/COLOR]\n' % self.episode_description(episode)
break
prev_episode = episode
try:
description += '%s\n' % self.episode_description(next(episodes))
except StopIteration:
pass
return description

def parse(self, date, now):
if date == 'today':
return now
Expand Down
6 changes: 3 additions & 3 deletions resources/lib/vrtplayer/vrtplayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def show_livestream_items(self):
self.show_channels(action=actions.PLAY, channels=['een', 'canvas', 'sporza', 'ketnet-jr', 'ketnet', 'stubru', 'mnm'])

def show_channels(self, action=actions.PLAY, channels=None):
from resources.lib.vrtplayer import CHANNELS
from resources.lib.vrtplayer import CHANNELS, tvguide
_tvguide = tvguide.TVGuide(self._kodi)

fanart_path = 'resource://resource.images.studios.white/%(studio)s.png'
icon_path = 'resource://resource.images.studios.white/%(studio)s.png'
Expand All @@ -139,8 +140,7 @@ def show_channels(self, action=actions.PLAY, channels=None):
is_playable = True
if channel.get('name') in ['een', 'canvas', 'ketnet']:
fanart = self._apihelper.get_live_screenshot(channel.get('name'))
plot = '%s\n%s' % (self._kodi.localize(30201),
self._kodi.localize(30102) % channel.get('label'))
plot = _tvguide.live_description(channel.get('name')) or self._kodi.localize(30102) % channel.get('label')
else:
plot = self._kodi.localize(30102) % channel.get('label')
if channel.get('live_stream_url'):
Expand Down
8 changes: 8 additions & 0 deletions test/tvguidetests.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ def test_tvguide_episode_menu(self):
episode_items = self._tvguide.show_episodes(date, channel)
self.assertTrue(episode_items)

def test_livetv_description(self):
description = self._tvguide.live_description('een')
print(description)
description = self._tvguide.live_description('canvas')
print(description)
description = self._tvguide.live_description('ketnet')
print(description)


if __name__ == '__main__':
unittest.main()

0 comments on commit 0eb75a1

Please sign in to comment.