-
Notifications
You must be signed in to change notification settings - Fork 20
/
iptvmanager.py
60 lines (50 loc) · 2.22 KB
/
iptvmanager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
# Copyright: (c) 2020, Dag Wieers (@dagwieers) <dag@wieers.com>
# GNU General Public License v3.0 (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
"""Implementation of IPTVManager class"""
from __future__ import absolute_import, division, unicode_literals
from kodiutils import log, url_for
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
log(2, "Sending data output to IPTV Manager using port {port}", port=self.port)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('127.0.0.1', self.port))
try:
sock.sendall(json.dumps(func()).encode()) # pylint: disable=not-callable
finally:
sock.close()
return send
@via_socket
def send_channels(): # pylint: disable=no-method-argument
"""Return JSON-M3U formatted information to IPTV Manager"""
from data import CHANNELS
streams = []
for channel in CHANNELS:
if not channel.get('live_stream_id'):
continue
item = {
'id': channel.get('epg_id'),
'name': channel.get('label'),
'logo': channel.get('logo'),
'preset': channel.get('preset'),
'stream': url_for('play_id', video_id=channel.get('live_stream_id')),
}
if channel.get('has_tvguide'):
item.update({'vod': url_for('play_air_date', channel=channel.get('name'), start_date='{{start}}', end_date='{{stop}}')})
streams.append(item)
return {'version': 1, 'streams': streams}
@via_socket
def send_epg(): # pylint: disable=no-method-argument
"""Return JSONTV formatted information to IPTV Manager"""
from tvguide import TVGuide
epg_data = TVGuide().get_epg_data()
return {'version': 1, 'epg': epg_data}