-
Notifications
You must be signed in to change notification settings - Fork 26
/
default.py
321 lines (302 loc) · 13.7 KB
/
default.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
# zap2epg tv schedule grabber for kodi
################################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################################
import xbmc, xbmcaddon, xbmcvfs, xbmcgui
from tvh import tvh_connect, tvh_getData, tvh_logsetup
from xbmcswift2 import Plugin
import os
import re
import logging
import zap2epg
import urllib.request, urllib.error, urllib.parse
import json
from collections import OrderedDict
import time
import datetime
#import web_pdb; web_pdb.set_trace()
userdata = xbmcvfs.translatePath(xbmcaddon.Addon().getAddonInfo('profile'))
tvhoff = True if xbmcaddon.Addon().getSetting('tvhoff') == 'true' else False
if not os.path.exists(userdata):
os.mkdir(userdata)
log = os.path.join(userdata, 'zap2epg.log')
Clist = os.path.join(userdata, 'channels.json')
tvhList = os.path.join(userdata, 'TVHchannels.json')
cacheDir = os.path.join(userdata, 'cache')
plugin = Plugin()
dialog = xbmcgui.Dialog()
gridtime = (int(time.mktime(time.strptime(str(datetime.datetime.now().replace(microsecond=0,second=0,minute=0)), '%Y-%m-%d %H:%M:%S'))))
connection = None
def connectTVH(updateSetting = False):
global tvhoff
if tvhoff is True:
tvh_url = xbmcaddon.Addon().getSetting('tvhurl')
tvh_port = xbmcaddon.Addon().getSetting('tvhport')
tvh_usern = xbmcaddon.Addon().getSetting('usern')
tvh_passw = xbmcaddon.Addon().getSetting('passw')
pvr = {}
try:
pvr['ipaddress'] = xbmcaddon.Addon('pvr.hts').getSetting("host")
pvr['port'] = xbmcaddon.Addon('pvr.hts').getSetting("http_port")
pvr['user'] = tvh_usern
pvr['password'] = tvh_passw
except:
pvr.clear()
global connection
connection = tvh_connect(tvh_url, tvh_port, tvh_usern, tvh_passw, pvr)
tvhoff = True if connection is not None else False
if tvhoff is True:
if tvh_url != connection['ipaddress']:
response = dialog.yesno('Update TVH Settings?', f'{tvh_url} TVH server was not found.\n\nWould you like to use the IP address found in the TVH PVR?\n{connection["ipaddress"]}')
if response is True:
xbmcaddon.Addon().setSetting(id='tvhurl', value=connection['ipaddress'])
xbmcaddon.Addon().setSetting(id='tvhport', value=connection['port'])
else:
if updateSetting:
connection = None
tvhoff = False
xbmcaddon.Addon().setSetting(id='tvhoff', value='false')
else:
dialog.ok("TVHeadend Server", f'The TVH server {tvh_url} was not found or username / password was incorrect. Please check TVH settings')
if updateSetting:
connection = None
tvhoff = False
xbmcaddon.Addon().setSetting(id='tvhoff', value='false')
def getTVHChannels():
global tvhoff, connection
if connection is None:
connectTVH()
if connection is not None:
response = tvh_getData('/api/channel/grid?all=1&limit=999999999&sort=name')
try:
logging.info('Accessing Tvheadend channel list from: %s', connection['ipaddress'])
channels = response.json()
with open(tvhList,"w") as f:
json.dump(channels,f)
except urllib.error.HTTPError as e:
logging.exception('Exception: tvhClist - %s', e.strerror)
tvhoff = False
def get_icon_path(icon_name):
addon_path = xbmcaddon.Addon().getAddonInfo("path")
return os.path.join(addon_path, 'resources', 'img', icon_name+".png")
def create_cList():
tvhClist = []
if tvhoff is True and not os.path.isfile(tvhList):
getTVHChannels()
if tvhoff is True:
with open(tvhList) as tvhData:
tvhDict = json.load(tvhData)
for ch in tvhDict['entries']:
channelEnabled = ch['enabled']
if channelEnabled == True:
tvhClist.append(ch['number'])
lineupcode = xbmcaddon.Addon().getSetting('lineupcode')
url = 'http://tvlistings.zap2it.com/api/grid?lineupId=×pan=3&headendId=' + lineupcode + '&country=' + country + '&device=' + device + '&postalCode=' + zipcode + '&time=' + str(gridtime) + '&pref=-&userId=-'
content = urllib.request.urlopen(url).read()
contentDict = json.loads(content)
stationDict = {}
if 'channels' in contentDict:
for channel in contentDict['channels']:
skey = channel.get('channelId')
stationDict[skey] = {}
stationDict[skey]['name'] = channel.get('callSign')
stationDict[skey]['num'] = channel.get('channelNo')
if channel.get('channelNo') in tvhClist:
stationDict[skey]['include'] = 'True'
else:
stationDict[skey]['include'] = 'False'
stationDictSort = OrderedDict(sorted(iter(stationDict.items()), key=lambda i: (float(i[1]['num']))))
#Search the stations for duplicate channel numbers. Get rid of the non 'DT' channel(s) if so.
for station in stationDictSort:
myStations = {k: v for k, v in stationDictSort.items() if v['num'] == stationDictSort[station]['num']}
if len(myStations) > 1:
for st in myStations:
if myStations[st]['name'].find('DT') < 0:
stationDictSort[st]['include'] = 'False'
with open(Clist,"w") as f:
json.dump(stationDictSort,f)
@plugin.route('/channels')
def channels():
lineupcode = xbmcaddon.Addon().getSetting('lineupcode')
if lineup is None or zipcode is None:
dialog.ok('Location not configured!', 'Please setup your location before configuring channels.')
if not os.path.isfile(Clist):
create_cList()
else:
newList = dialog.yesno('Existing Channel List Found', 'Would you like to download a new channel list or review your current list?', 'Review', 'Download')
if newList:
os.remove(Clist)
create_cList()
with open(Clist) as data:
stationDict = json.load(data)
stationDict = OrderedDict(sorted(iter(stationDict.items()), key=lambda i: (float(i[1]['num']))))
stationCode = []
stationListName = []
stationListNum = []
stationListInclude = []
for station in stationDict:
stationCode.append(station)
stationListName.append(stationDict[station]['name'])
stationListNum.append(stationDict[station]['num'])
stationListInclude.append(stationDict[station]['include'])
stationPre = [i for i, x in enumerate(stationListInclude) if x == 'True']
stationListFull = list(zip(stationListNum, stationListName))
stationList = ["%s %s" % x for x in stationListFull]
selCh = dialog.multiselect('Click to Select Channels to Include', stationList, preselect=stationPre)
for station in stationDict:
stationDict[station]['include'] = 'False'
stationListCodes = []
if selCh:
for channel in selCh:
skey = stationCode[channel]
stationDict[skey]['include'] = 'True'
stationListCodes.append(skey)
with open(Clist,"w") as f:
json.dump(stationDict,f)
xbmcaddon.Addon().setSetting(id='slist', value=','.join(stationListCodes))
@plugin.route('/location')
def location():
global country
countryPick = ['USA', 'CAN']
countryNew = dialog.select('Select your country', list=countryPick)
if countryNew == 0:
zipcodeNew = dialog.input('Enter your zipcode', defaultt=zipcode, type=xbmcgui.INPUT_NUMERIC)
if countryNew == 1:
zipcodeNew = dialog.input('Enter your zipcode', defaultt=zipcode, type=xbmcgui.INPUT_ALPHANUM)
if not 'zipcodeNew' in vars() or 'zipcodeNew' in globals():
return
zipcodeNew = re.sub(' ', '', zipcodeNew)
zipcodeNew = zipcodeNew.upper()
xbmcaddon.Addon().setSetting(id='zipcode', value=zipcodeNew)
if countryNew == 0:
country = 'USA'
url = 'https://tvlistings.zap2it.com/gapzap_webapi/api/Providers/getPostalCodeProviders/USA/' + zipcodeNew + '/gapzap/en'
lineupsN = ['AVAILABLE LINEUPS', 'TIMEZONE - Eastern', 'TIMEZONE - Central', 'TIMEZONE - Mountain', 'TIMEZONE - Pacific', 'TIMEZONE - Alaskan', 'TIMEZONE - Hawaiian']
lineupsC = ['NONE', 'DFLTE', 'DFLTC', 'DFLTM', 'DFLTP', 'DFLTA', 'DFLTH']
deviceX = ['-', '-', '-', '-', '-', '-', '-']
if countryNew == 1:
country = 'CAN'
url = 'https://tvlistings.zap2it.com/gapzap_webapi/api/Providers/getPostalCodeProviders/CAN/' + zipcodeNew + '/gapzap/en'
lineupsN = ['AVAILABLE LINEUPS', 'TIMEZONE - Eastern', 'TIMEZONE - Central', 'TIMEZONE - Mountain', 'TIMEZONE - Pacific']
lineupsC = ['NONE', 'DFLTEC', 'DFLTCC', 'DFLTMC', 'DFLTPC']
deviceX = ['-', '-', '-', '-', '-']
content = urllib.request.urlopen(url).read()
lineupDict = json.loads(content)
if 'Providers' in lineupDict:
for provider in lineupDict['Providers']:
lineupName = provider.get('name')
lineupLocation = provider.get('location')
if lineupLocation != '':
lineupCombo = lineupName + ' (' + lineupLocation + ')'
lineupsN.append(lineupCombo)
else:
lineupsN.append(lineupName)
lineupsC.append(provider.get('headendId'))
deviceGet = provider.get('device')
if deviceGet == '' or deviceGet == ' ':
deviceGet = '-'
deviceX.append(deviceGet)
else:
dialog.ok('Error - No Providers!', 'No providers were found - please check zipcode and try again.')
return
lineupSel = dialog.select('Select a lineup', list=lineupsN)
if lineupSel:
lineupSelCode = lineupsC[lineupSel]
lineupSelName = lineupsN[lineupSel]
deviceSel = deviceX[lineupSel]
xbmcaddon.Addon().setSetting(id='lineupcode', value=lineupSelCode)
xbmcaddon.Addon().setSetting(id='lineup', value=lineupSelName)
xbmcaddon.Addon().setSetting(id='device', value=deviceSel)
if os.path.exists(cacheDir):
entries = os.listdir(cacheDir)
for entry in entries:
oldfile = entry.split('.')[0]
if oldfile.isdigit():
fn = os.path.join(cacheDir, entry)
try:
os.remove(fn)
except:
pass
xbmc.executebuiltin('Container.Refresh')
else:
xbmc.executebuiltin('Container.Refresh')
return
@plugin.route('/run')
def run():
logging.basicConfig(filename=log, filemode='w', format='%(asctime)s %(message)s', datefmt='%Y/%m/%d %H:%M:%S', level=logging.DEBUG)
tvh_logsetup(filename=log, filemode='w', format='%(asctime)s %(message)s', datefmt='%Y/%m/%d %H:%M:%S', level=logging.DEBUG)
status = zap2epg.mainRun(userdata)
dialog.ok('zap2epg Finished!', 'zap2epg completed in ' + str(status[0]) + ' seconds.\n' + str(status[1]) + ' Stations and ' + str(status[2]) + ' Episodes written to xmltv.xml file.')
@plugin.route('/open_settings')
def open_settings():
plugin.open_settings()
global tvhoff, connection
# Test the connection to TVH if tvhoff is true
tvhoff = True if xbmcaddon.Addon().getSetting('tvhoff') == 'true' else False
if tvhoff is True:
connectTVH(True)
try:
os.remove(tvhList)
except:
pass
if connection is not None:
getTVHChannels()
else:
tvhoff = False
xbmcaddon.Addon().setSetting(id='tvhoff', value='false')
@plugin.route('/')
def index():
items = []
items.append(
{
'label': 'Run zap2epg and Update Guide Data',
'path': plugin.url_for('run'),
'thumbnail':get_icon_path('run'),
})
items.append(
{
'label': 'Change Current Location | Zipcode: ' + zipcode + ' & Lineup: ' + lineup,
'path': plugin.url_for('location'),
'thumbnail':get_icon_path('antenna'),
})
items.append(
{
'label': 'Configure Channel List',
'path': plugin.url_for('channels'),
'thumbnail':get_icon_path('channel'),
})
items.append(
{
'label': 'Configure Settings and Options',
'path': plugin.url_for('open_settings'),
'thumbnail':get_icon_path('settings'),
})
return items
if __name__ == '__main__':
try:
zipcode = xbmcaddon.Addon().getSetting('zipcode')
if zipcode.isdigit():
country = 'USA'
else:
country = 'CAN'
lineup = xbmcaddon.Addon().getSetting('lineup')
device = xbmcaddon.Addon().getSetting('device')
if zipcode == '' or lineup == '':
zipConfig = dialog.yesno('No Lineup Configured!', 'You need to configure your lineup location before running zap2epg.\n\nWould you like to setup your lineup?')
if zipConfig:
location()
xbmc.executebuiltin('Container.Refresh')
except:
dialog.ok('No Lineup Configured!', '', 'Please configure your zipcode and lineup under Change Current Location.')
plugin.run()