82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
|
# -*- coding: utf-8 -*-
|
||
|
"""Implementation of IPTVManager class"""
|
||
|
|
||
|
from __future__ import absolute_import, division, unicode_literals
|
||
|
|
||
|
import logging
|
||
|
from datetime import timedelta
|
||
|
|
||
|
from resources.lib import kodiutils
|
||
|
from resources.lib.viervijfzes import CHANNELS
|
||
|
from resources.lib.viervijfzes.epg import EpgApi
|
||
|
|
||
|
_LOGGER = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
|
class IPTVManager:
|
||
|
"""Interface to IPTV Manager"""
|
||
|
|
||
|
def __init__(self, port):
|
||
|
"""Initialize IPTV Manager object"""
|
||
|
self.port = port
|
||
|
|
||
|
def via_socket(func): # pylint: disable=no-self-argument
|
||
|
"""Send the output of the wrapped function to socket"""
|
||
|
|
||
|
def send(self):
|
||
|
"""Decorator to send over a socket"""
|
||
|
import json
|
||
|
import socket
|
||
|
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||
|
sock.connect(('127.0.0.1', self.port))
|
||
|
try:
|
||
|
sock.send(json.dumps(func())) # pylint: disable=not-callable
|
||
|
finally:
|
||
|
sock.close()
|
||
|
|
||
|
return send
|
||
|
|
||
|
@via_socket
|
||
|
def send_channels(): # pylint: disable=no-method-argument
|
||
|
"""Return JSON-STREAMS formatted information to IPTV Manager"""
|
||
|
streams = []
|
||
|
for key, channel in CHANNELS.items():
|
||
|
item = dict(
|
||
|
id=channel.get('iptv_id'),
|
||
|
name=channel.get('name'),
|
||
|
logo='special://home/addons/{addon}/resources/logos/{logo}'.format(addon=kodiutils.addon_id(), logo=channel.get('logo')),
|
||
|
preset=channel.get('iptv_preset'),
|
||
|
stream='plugin://plugin.video.viervijfzes/play/live/{channel}'.format(channel=key),
|
||
|
vod='plugin://plugin.video.viervijfzes/play/epg/{channel}/{{date}}'.format(channel=key)
|
||
|
)
|
||
|
|
||
|
streams.append(item)
|
||
|
|
||
|
return dict(version=1, streams=streams)
|
||
|
|
||
|
@via_socket
|
||
|
def send_epg(): # pylint: disable=no-method-argument
|
||
|
"""Return JSON-EPG formatted information to IPTV Manager"""
|
||
|
epg_api = EpgApi()
|
||
|
|
||
|
results = dict()
|
||
|
for key, channel in CHANNELS.items():
|
||
|
iptv_id = channel.get('iptv_id')
|
||
|
|
||
|
results[iptv_id] = []
|
||
|
for date in ['yesterday', 'today', 'tomorrow']:
|
||
|
epg = epg_api.get_epg(key, date)
|
||
|
|
||
|
results[iptv_id].extend([
|
||
|
dict(
|
||
|
start=program.start.isoformat(),
|
||
|
stop=(program.start + timedelta(seconds=program.duration)).isoformat(),
|
||
|
title=program.program_title,
|
||
|
description=program.description,
|
||
|
image=program.cover,
|
||
|
available=bool(program.video_url))
|
||
|
for program in epg if program.duration
|
||
|
])
|
||
|
|
||
|
return dict(version=1, epg=results)
|