forked from jkaberg/tvhProxy
-
Notifications
You must be signed in to change notification settings - Fork 9
/
tvhProxy.py
327 lines (293 loc) · 13.4 KB
/
tvhProxy.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from gevent import monkey
monkey.patch_all()
import json
from dotenv import load_dotenv
from ssdp import SSDPServer
from flask import Flask, Response, request, jsonify, abort, render_template
from gevent.pywsgi import WSGIServer
import xml.etree.ElementTree as ElementTree
from datetime import timedelta, datetime, time
import logging
import socket
import threading
from requests.auth import HTTPDigestAuth
import requests
import os
import sched
logging.basicConfig(level=logging.INFO)
load_dotenv(verbose=True)
app = Flask(__name__)
scheduler = sched.scheduler()
logger = logging.getLogger()
host_name = socket.gethostname()
host_ip = socket.gethostbyname(host_name)
# URL format: <protocol>://<username>:<password>@<hostname>:<port>, example: https://test:1234@localhost:9981
config = {
'deviceID': os.environ.get('DEVICE_ID') or '12345678',
'bindAddr': os.environ.get('TVH_BINDADDR') or '',
# only used if set (in case of forward-proxy)
'tvhURL': os.environ.get('TVH_URL') or 'http://localhost:9981',
'tvhProxyURL': os.environ.get('TVH_PROXY_URL'),
'tvhProxyHost': os.environ.get('TVH_PROXY_HOST') or host_ip,
'tvhProxyPort': os.environ.get('TVH_PROXY_PORT') or 5004,
'tvhUser': os.environ.get('TVH_USER') or '',
'tvhPassword': os.environ.get('TVH_PASSWORD') or '',
# number of tuners in tvh
'tunerCount': os.environ.get('TVH_TUNER_COUNT') or 6,
'tvhWeight': os.environ.get('TVH_WEIGHT') or 300, # subscription priority
# usually you don't need to edit this
'chunkSize': os.environ.get('TVH_CHUNK_SIZE') or 1024*1024,
# specifiy a stream profile that you want to use for adhoc transcoding in tvh, e.g. mp4
'streamProfile': os.environ.get('TVH_PROFILE') or 'pass'
}
discoverData = {
'FriendlyName': 'tvhProxy',
'Manufacturer': 'Silicondust',
'ModelNumber': 'HDTC-2US',
'FirmwareName': 'hdhomeruntc_atsc',
'TunerCount': int(config['tunerCount']),
'FirmwareVersion': '20150826',
'DeviceID': config['deviceID'],
'DeviceAuth': 'test1234',
'BaseURL': '%s' % (config['tvhProxyURL'] or "http://" + config['tvhProxyHost'] + ":" + str(config['tvhProxyPort'])),
'LineupURL': '%s/lineup.json' % (config['tvhProxyURL'] or "http://" + config['tvhProxyHost'] + ":" + str(config['tvhProxyPort']))
}
@app.route('/discover.json')
def discover():
return jsonify(discoverData)
@app.route('/lineup_status.json')
def status():
return jsonify({
'ScanInProgress': 0,
'ScanPossible': 0,
'Source': "Cable",
'SourceList': ['Cable']
})
@app.route('/lineup.json')
def lineup():
lineup = []
for c in _get_channels():
if c['enabled']:
url = '%s/stream/channel/%s?profile=%s&weight=%s' % (
config['tvhURL'], c['uuid'], config['streamProfile'], int(config['tvhWeight']))
lineup.append({'GuideNumber': str(c['number']),
'GuideName': c['name'],
'URL': url
})
return jsonify(lineup)
@app.route('/lineup.post', methods=['GET', 'POST'])
def lineup_post():
return ''
@app.route('/')
@app.route('/device.xml')
def device():
return render_template('device.xml', data=discoverData), {'Content-Type': 'application/xml'}
@app.route('/epg.xml')
def epg():
return _get_xmltv(), {'Content-Type': 'application/xml'}
def _get_channels():
url = '%s/api/channel/grid' % config['tvhURL']
params = {
'limit': 999999,
'start': 0
}
try:
r = requests.get(url, params=params, auth=HTTPDigestAuth(
config['tvhUser'], config['tvhPassword']))
return r.json(strict=False)['entries']
except Exception as e:
logger.error('An error occured: %s' + repr(e))
def _get_genres():
def _findMainCategory(majorCategories, minorCategory):
prevKey, currentKey = None, None
for currentKey in sorted(majorCategories.keys()):
if(currentKey > minorCategory):
return majorCategories[prevKey]
prevKey = currentKey
return majorCategories[prevKey]
url = '%s/api/epg/content_type/list' % config['tvhURL']
params = {'full': 1}
try:
r = requests.get(url, auth=HTTPDigestAuth(
config['tvhUser'], config['tvhPassword']))
entries = r.json(strict=False)['entries']
r = requests.get(url, params=params, auth=HTTPDigestAuth(
config['tvhUser'], config['tvhPassword']))
entries_full = r.json(strict=False)['entries']
majorCategories = {}
genres = {}
for entry in entries:
majorCategories[entry['key']] = entry['val']
for entry in entries_full:
if not entry['key'] in majorCategories:
mainCategory = _findMainCategory(majorCategories, entry['key'])
if(mainCategory != entry['val']):
genres[entry['key']] = [mainCategory, entry['val']]
else:
genres[entry['key']] = [entry['val']]
else:
genres[entry['key']] = [entry['val']]
return genres
except Exception as e:
logger.error('An error occured: %s' + repr(e))
def _get_xmltv():
try:
url = '%s/xmltv/channels' % config['tvhURL']
r = requests.get(url, auth=HTTPDigestAuth(
config['tvhUser'], config['tvhPassword']))
logger.info('downloading xmltv from %s', r.url)
tree = ElementTree.ElementTree(
ElementTree.fromstring(requests.get(url, auth=HTTPDigestAuth(config['tvhUser'], config['tvhPassword'])).content))
root = tree.getroot()
url = '%s/api/epg/events/grid' % config['tvhURL']
params = {
'limit': 999999,
'filter': json.dumps([
{
"field": "start",
"type": "numeric",
"value": int(round(datetime.timestamp(datetime.now() + timedelta(hours=72)))),
"comparison": "lt"
}
])
}
r = requests.get(url, params=params, auth=HTTPDigestAuth(
config['tvhUser'], config['tvhPassword']))
logger.info('downloading epg grid from %s', r.url)
epg_events_grid = r.json(strict=False)['entries']
epg_events = {}
event_keys = {}
for epg_event in epg_events_grid:
if epg_event['channelUuid'] not in epg_events:
epg_events[epg_event['channelUuid']] = {}
epg_events[epg_event['channelUuid']
][epg_event['start']] = epg_event
for key in epg_event.keys():
event_keys[key] = True
channelNumberMapping = {}
channelsInEPG = {}
genres = _get_genres()
for child in root:
if child.tag == 'channel':
channelId = child.attrib['id']
channelNo = child[1].text
if not channelNo:
logger.error("No channel number for: %s", channelId)
channelNo = "00"
if not child[0].text:
logger.error("No channel name for: %s", channelNo)
child[0].text = "No Name"
channelNumberMapping[channelId] = channelNo
if channelNo in channelsInEPG:
logger.error("duplicate channelNo: %s", channelNo)
channelsInEPG[channelNo] = False
channelName = ElementTree.Element('display-name')
channelName.text = str(channelNo) + " " + child[0].text
child.insert(0, channelName)
for icon in child.iter('icon'):
# check if icon exists (tvh always returns an URL even if there is no channel icon)
iconUrl = icon.attrib['src']
r = requests.head(iconUrl)
if r.status_code == requests.codes.ok:
icon.attrib['src'] = iconUrl
else:
logger.error("remove icon: %s", iconUrl)
child.remove(icon)
child.attrib['id'] = channelNo
if child.tag == 'programme':
channelUuid = child.attrib['channel']
channelNumber = channelNumberMapping[channelUuid]
channelsInEPG[channelNumber] = True
child.attrib['channel'] = channelNumber
start_datetime = datetime.strptime(
child.attrib['start'], "%Y%m%d%H%M%S %z").astimezone(tz=None).replace(tzinfo=None)
stop_datetime = datetime.strptime(
child.attrib['stop'], "%Y%m%d%H%M%S %z").astimezone(tz=None).replace(tzinfo=None)
if start_datetime >= datetime.now() + timedelta(hours=72):
# Plex doesn't like extremely large XML files, we'll remove the details from entries more than 72h in the future
# Fixed w/ plex server 1.19.2.2673
# for desc in child.iter('desc'):
# child.remove(desc)
pass
elif stop_datetime > datetime.now() and start_datetime < datetime.now() + timedelta(hours=72):
# add extra details for programs in the next 72hs
start_timestamp = int(
round(datetime.timestamp(start_datetime)))
epg_event = epg_events[channelUuid][start_timestamp]
if ('image' in epg_event):
programmeImage = ElementTree.SubElement(child, 'icon')
imageUrl = str(epg_event['image'])
if(imageUrl.startswith('imagecache')):
imageUrl = config['tvhURL'] + \
"/" + imageUrl + ".png"
programmeImage.attrib['src'] = imageUrl
if ('genre' in epg_event):
for genreId in epg_event['genre']:
for category in genres[genreId]:
programmeCategory = ElementTree.SubElement(
child, 'category')
programmeCategory.text = category
if ('episodeOnscreen' in epg_event):
episodeNum = ElementTree.SubElement(
child, 'episode-num')
episodeNum.attrib['system'] = 'onscreen'
episodeNum.text = epg_event['episodeOnscreen']
if('hd' in epg_event):
video = ElementTree.SubElement(child, 'video')
quality = ElementTree.SubElement(video, 'quality')
quality.text = "HDTV"
if('new' in epg_event):
ElementTree.SubElement(child, 'new')
else:
ElementTree.SubElement(child, 'previously-shown')
if('copyright_year' in epg_event):
date = ElementTree.SubElement(child, 'date')
date.text = str(epg_event['copyright_year'])
del epg_events[channelUuid][start_timestamp]
for key in sorted(channelsInEPG):
if channelsInEPG[key]:
logger.debug("Programmes found for channel %s", key)
else:
channelName = root.find(
'channel[@id="'+key+'"]/display-name').text
logger.error("No programme for channel %s: %s",
key, channelName)
# create 2h programmes for 72 hours
yesterday_midnight = datetime.combine(
datetime.today(), time.min) - timedelta(days=1)
date_format = '%Y%m%d%H%M%S'
for x in range(0, 36):
dummyProgramme = ElementTree.SubElement(root, 'programme')
dummyProgramme.attrib['channel'] = str(key)
dummyProgramme.attrib['start'] = (
yesterday_midnight + timedelta(hours=x*2)).strftime(date_format)
dummyProgramme.attrib['stop'] = (
yesterday_midnight + timedelta(hours=(x*2)+2)).strftime(date_format)
dummyTitle = ElementTree.SubElement(
dummyProgramme, 'title')
dummyTitle.attrib['lang'] = 'eng'
dummyTitle.text = channelName
dummyDesc = ElementTree.SubElement(dummyProgramme, 'desc')
dummyDesc.attrib['lang'] = 'eng'
dummyDesc.text = "No programming information"
logger.info("returning epg")
return ElementTree.tostring(root)
except requests.exceptions.RequestException as e: # This is the correct syntax
logger.error('An error occured: ' + repr(e))
raise e
def _start_ssdp():
ssdp = SSDPServer()
thread_ssdp = threading.Thread(target=ssdp.run, args=())
thread_ssdp.daemon = True # Daemonize thread
thread_ssdp.start()
ssdp.register('local',
'uuid:{}::upnp:rootdevice'.format(discoverData['DeviceID']),
'upnp:rootdevice',
'http://{}:{}/device.xml'.format(
config['tvhProxyHost'], config['tvhProxyPort']),
'SSDP Server for tvhProxy')
if __name__ == '__main__':
http = WSGIServer((config['bindAddr'], int(config['tvhProxyPort'])),
app.wsgi_app, log=logger, error_log=logger)
_start_ssdp()
http.serve_forever()