-
Notifications
You must be signed in to change notification settings - Fork 6
/
hdhomerun.py
380 lines (300 loc) · 12.6 KB
/
hdhomerun.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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
# Written by Vincent Gee
# Derived from the work at https://forum.libreelec.tv/thread/12228-tvheadend-epg-guide-from-hdhomerun/
# 8/26/2018
#
# Description: Downloads the EPG from HdHomeRun's server and converts it to a XMLTV format
# So it can be loaded into Plex.
#// Fairfield Tek L.L.C.
#// Copyright (c) 2016, Fairfield Tek L.L.C.
#//
#//
#// THIS SOFTWARE IS PROVIDED BY FAIRFIELDTEK LLC ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
#// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
#// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FAIRFIELDTEK LLC BE LIABLE FOR ANY DIRECT, INDIRECT,
#// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
#// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
#// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
#// DAMAGE.
#//
#// Licensed under the Apache License, Version 2.0 (the "License");
#// you may not use this file except in compliance with the License.
#// You may obtain a copy of the License at
#//
#// http://www.apache.org/licenses/LICENSE-2.0
#//
#// Unless required by applicable law or agreed to in writing, software
#// distributed under the License is distributed on an "AS IS" BASIS,
#// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#// See the License for the specific language governing permissions and
#// limitations under the License.
import sys, json, urllib3, datetime, subprocess,time,os
import xml.etree.cElementTree as ET
from datetime import datetime
from xml.dom import minidom
from pprint import pprint
def get_utc_offset_str():
"""
Returns a UTC offset string of the current time suitable for use in the
most widely used timestamps (i.e. ISO 8601, RFC 3339). For example:
10 hours ahead, 5 hours behind, and time is UTC: +10:00, -05:00, +00:00
"""
# Calculate the UTC time difference in seconds.
timestamp = time.time()
time_now = datetime.fromtimestamp(timestamp)
time_utc = datetime.utcfromtimestamp(timestamp)
utc_offset_secs = (time_now - time_utc).total_seconds()
# Flag variable to hold if the current time is behind UTC.
is_behind_utc = False
# If the current time is behind UTC convert the offset
# seconds to a positive value and set the flag variable.
if utc_offset_secs < 0:
is_behind_utc = True
utc_offset_secs *= -1
# Build a UTC offset string suitable for use in a timestamp.
if is_behind_utc:
pos_neg_prefix = "-"
else:
pos_neg_prefix = "+"
utc_offset = time.gmtime(utc_offset_secs)
utc_offset_fmt = time.strftime("%H:%M", utc_offset)
utc_offset_str = pos_neg_prefix + utc_offset_fmt
return utc_offset_str
def ProcessProgram(xml, program, guideName):
WriteLog ("Processing Show: " + program['Title'])
timezone_offset = get_utc_offset_str().replace(":","")
#program
#Create the "programme" element and set the Channel attribute to "GuideName" from json
xmlProgram = ET.SubElement(xml, "programme", channel = guideName)
# channel=channel['GuideName'])
#set the start date and time from the feed
xmlProgram.set("start", datetime.fromtimestamp(program['StartTime']).strftime('%Y%m%d%H%M%S') + " " + timezone_offset)
#set the end date and time from the feed
xmlProgram.set("stop", datetime.fromtimestamp(program['EndTime']).strftime('%Y%m%d%H%M%S') + " " + timezone_offset)
#Title
ET.SubElement(xmlProgram, "title", lang="en").text = program['Title']
#Sub Title
if 'EpisodeTitle' in program:
ET.SubElement(xmlProgram, "sub-title", lang="en" ).text = program['EpisodeTitle']
#Description
if 'Synopsis' in program:
ET.SubElement(xmlProgram, "desc").text = program['Synopsis']
#Credits
#We add a blank entry to satisfy Plex
ET.SubElement(xmlProgram,"credits").text = ""
addedEpisode = False
if 'EpisodeNumber' in program:
#add the friendly display
ET.SubElement(xmlProgram, "episode-num", system="onscreen").text = program['EpisodeNumber']
#Fake the xml version
en = program['EpisodeNumber']
parts = en.split("E")
season = parts[0].replace("S","")
episode = parts[1]
#Assign the fake xml version
ET.SubElement(xmlProgram, "episode-num", system="xmltv_ns").text = (season + " . " + episode + " . 0/1")
#set the category flag to series
ET.SubElement(xmlProgram, "category", lang="en" ).text = "series"
addedEpisode = True
if 'OriginalAirdate' in program:
if program['OriginalAirdate'] > 0:
#The 86400 is because the HdHomeRun feed is off by a day, this fixes that.
ET.SubElement(xmlProgram, "previously-shown", start = datetime.fromtimestamp(program['OriginalAirdate'] + 86400 ).strftime('%Y%m%d%H%M%S') + " " + timezone_offset)
if 'ImageURL' in program:
ET.SubElement(xmlProgram, "icon", src=program['ImageURL'])
xmlAudio = ET.SubElement(xmlProgram,"audio")
ET.SubElement( xmlAudio, "stereo").text = "stereo"
ET.SubElement(xmlProgram, "subtitles", type="teletext")
if 'Filter' in program:
#Search the filters and see if it is a movie
FoundMovieCategory = False
for filter in program['Filter']:
filterstringLower = str(filter).lower()
if (filterstringLower == "movies"):
FoundMovieCategory = True
break
#If we didn't find the movie category, and we haven't added an episode flag, lets do it!
if FoundMovieCategory == False and addedEpisode == False:
ET.SubElement(xmlProgram, "category",lang="en").text = "series"
#create a fake episode number for it
ET.SubElement(xmlProgram, "episode-num", system="xmltv_ns").text = DateTimeToEpisode()
ET.SubElement(xmlProgram, "episode-num", system="onscreen").text = DateTimeToEpisodeFriendly()
addedEpisode = True
for filter in program['Filter']:
#Lowercase the filter... apearenttly Plex is case sensitive
filterstringLower = str(filter).lower()
#add the filter as a category
ET.SubElement(xmlProgram, "category",lang="en").text = filterstringLower
#If the filter is news or sports...
# if (filterstringLower == "news" or filterstringLower == "sports"):
# #And the show didn't have it's own episode number...
# if ( addedEpisode == False ):
# #WriteLog("-------> Creating Fake Season and Episode for News or Sports show.")
# #add a category for series
# ET.SubElement(xmlProgram, "category",lang="en").text = "series"
# #create a fake episode number for it
# ET.SubElement(xmlProgram, "episode-num", system="xmltv_ns").text = DateTimeToEpisode()
# ET.SubElement(xmlProgram, "episode-num", system="onscreen").text = DateTimeToEpisodeFriendly()
#Return the endtime so we know where to start from on next loop.
return program['EndTime']
def processChannel(xml, data, deviceAuth):
WriteLog ("Processing Channel: " + data.get('GuideNumber') + " " + data.get('GuideName'))
#channel
xmlChannel = ET.SubElement(xml, "channel", id = data.get('GuideName'))
#display name
ET.SubElement(xmlChannel, "display-name").text = data.get('GuideName')
#display name
ET.SubElement(xmlChannel, "display-name").text = data.get('GuideNumber')
#display name
if 'Affiliate' in data:
ET.SubElement(xmlChannel, "display-name").text = data.get('Affiliate')
if 'ImageURL' in data:
ET.SubElement(xmlChannel, "icon", src= data.get('ImageURL'))
maxTime = 0
for program in data.get("Guide"):
maxTime = ProcessProgram(xml,program, data.get('GuideName'))
maxTime = maxTime + 1
counter = 0
#The first pull is for 4 hours, each of these are 8 hours
#So if we do this 21 times we will have fetched the complete week
try:
while ( counter < 24 ):
chanData = GetHdConnectChannelPrograms( deviceAuth, data.get('GuideNumber'), maxTime)
for chan in chanData:
for program in chan["Guide"]:
maxTime = ProcessProgram( xml, program, data.get('GuideName'))
counter = counter + 1
except:
WriteLog("It appears you do not have the HdHomeRunDvr Service.")
def saveStringToFile(strData, filename):
with open(filename, 'wb') as outfile:
outfile.write(strData)
def loadJsonFromFile(filename):
return json.load(open(filename))
def saveJsonToFile(data, filename):
with open(filename, 'w') as outfile:
json.dump(data, outfile, indent=4)
def GetHdConnectDevices():
WriteLog("Getting Connected Devices.")
http = urllib3.PoolManager()
discover_url_response = http.request('GET',"http://my.hdhomerun.com/discover")
data = discover_url_response.data
#WriteLog(data)
obj = json.loads(data)
return obj
def GetHdConnectDiscover(discover_url):
WriteLog("Discovering...")
http = urllib3.PoolManager()
device_auth_response = http.request('GET',discover_url)
data = device_auth_response.data
#WriteLog(data)
device_auth = json.loads(data)['DeviceAuth']
return device_auth
def GetHdConnectDiscoverLineUpUrl(discover_url):
WriteLog("Getting Lineup Url")
http = urllib3.PoolManager()
device_auth_response = http.request('GET',discover_url)
data = device_auth_response.data
LineupURL = json.loads(data)['LineupURL']
return LineupURL
#public class RootObject
#{
# public string GuideNumber { get; set; }
# public string GuideName { get; set; }
# public string VideoCodec { get; set; }
# public string AudioCodec { get; set; }
# public int HD { get; set; }
# public string URL { get; set; }
#}
def GetHdConnectLineUp(lineupUrl):
WriteLog("Getting Lineup")
http = urllib3.PoolManager()
device_auth_response = http.request('GET',lineupUrl)
data = device_auth_response.data
Lineup = json.loads(data)
return Lineup
def GetHdConnectChannels(device_auth):
WriteLog("Getting Channels.")
http = urllib3.PoolManager()
response = http.request('GET',"http://my.hdhomerun.com/api/guide.php?DeviceAuth=%s" % device_auth)
data = response.data
#WriteLog(data)
return json.loads(data)
def GetHdConnectChannelPrograms(device_auth, guideNumber, timeStamp):
WriteLog("Getting Extended Programs")
http = urllib3.PoolManager()
response = http.request('GET',"http://my.hdhomerun.com/api/guide.php?DeviceAuth=" + device_auth +"&Channel=" + guideNumber +"&Start=" + str(timeStamp) + "&SynopsisLength=160")
data = response.data
#WriteLog(data)
return json.loads(data)
def InList(l , value):
if (l.count(value)>0):
return True
else:
return False
return False
def ClearLog():
if os.path.exists("HdHomerun.log"):
os.remove("HdHomerun.log")
if os.path.exists("hdhomerun.xml"):
os.remove("hdhomerun.xml")
def DateTimeToEpisode():
timestamp = time.time()
time_now = datetime.fromtimestamp(timestamp)
season = time_now.strftime('%Y')
episode = time_now.strftime('%m%d%H')
return (season + " . " + episode + " . 0/1")
def DateTimeToEpisodeFriendly():
timestamp = time.time()
time_now = datetime.fromtimestamp(timestamp)
season = time_now.strftime('%Y')
episode = time_now.strftime('%m%d%H')
return ("S" + season + "E" + episode)
def WriteLog(message):
timestamp = time.time()
time_now = datetime.fromtimestamp(timestamp)
timeString = time_now.strftime('%Y%m%d%H%M%S')
with open ('HdHomerun.log','ab') as logfile:
output = str(timeString) + " " + str(message) + "\n"
logfile.write(output.encode('utf-8') )
print(output.encode('utf-8'))
def main():
ClearLog()
print("Downloading Content... Please wait.")
print("Check the log for progress.")
WriteLog("Starting...")
xml = ET.Element("tv")
try:
devices = GetHdConnectDevices()
except:
WriteLog("No HdHomeRun devices detected.")
exit()
processedChannelList = ["empty","empty"]
for device in devices:
if 'DeviceID' in device:
WriteLog("Processing Device: " + device["DeviceID"])
deviceAuth = GetHdConnectDiscover(device["DiscoverURL"])
lineUpUrl = GetHdConnectDiscoverLineUpUrl(device["DiscoverURL"])
LineUp = GetHdConnectLineUp(lineUpUrl)
if ( len(LineUp) > 0):
WriteLog("Line Up Exists for device")
channels = GetHdConnectChannels(deviceAuth)
for chan in channels:
ch =str( chan.get('GuideName') )
if (InList( processedChannelList, ch) == False):
WriteLog ("Processing Channel: " + ch)
processedChannelList.append(ch)
processChannel( xml, chan, deviceAuth)
else:
WriteLog ("Skipping Channel " + ch + ", already processed.")
else:
WriteLog ("No Lineup for device!")
else:
WriteLog ("Must be storage...")
reformed_xml = minidom.parseString(ET.tostring(xml))
xmltv = reformed_xml.toprettyxml(encoding='utf-8')
WriteLog ("Finished compiling information. Saving...")
saveStringToFile(xmltv, "hdhomerun.xml")
WriteLog ("Finished.")
if __name__== "__main__":
main()