forked from anorm/plugin.video.tv3play.dk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
addon.py
284 lines (241 loc) · 10.6 KB
/
addon.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Contributors:
# Tommy Winther
# Anders Norman
#
# 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 2, 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; see the file LICENSE.txt. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# http://www.gnu.org/copyleft/gpl.html
#
import os
import sys
import urlparse
import re
import urllib
import xbmc
import xbmcgui
import xbmcaddon
import xbmcplugin
import buggalo
import time
from mtgapi import MtgApi, MtgApiException
def _convert_date(s):
if s:
return '%s.%s.%s' % (s[8:10], s[5:7], s[0:4])
else:
return ''
class TV3PlayAddon(object):
def __init__(self, region):
self.region = region
self.api = MtgApi(region)
def _build_url(self, query):
if not 'region' in query.keys():
query['region'] = self.region
return PATH + '?' + urllib.urlencode(query)
def listRegions(self):
items = list()
for region in MtgApi.REGIONS:
item = xbmcgui.ListItem(region, iconImage=ICON)
item.setProperty('Fanart_Image', FANART)
url = self._build_url({'region': region})
items.append((url, item, True))
xbmcplugin.addDirectoryItems(HANDLE, items)
xbmcplugin.endOfDirectory(HANDLE)
def listChannels(self):
items = list()
for channel_id, channel_name in self.api.get_channels().iteritems():
item = xbmcgui.ListItem(channel_name, iconImage=self.api.get_channel_icon(channel_id))
item.setProperty('Fanart_Image', FANART)
item.setInfo('video', {'title': channel_name})
url = self._build_url({'channel': channel_id})
items.append((url, item, True))
xbmcplugin.addDirectoryItems(HANDLE, items)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_TITLE)
xbmcplugin.endOfDirectory(HANDLE)
def listShows(self, channel):
items = list()
shows = self.api.get_shows(channel)
if not shows:
xbmcplugin.endOfDirectory(HANDLE, succeeded=False)
self.displayError(ADDON.getLocalizedString(30205))
return
for show in shows:
fanart = show['image']
infoLabels = {
'title': show['title'],
'plot': show.get('description', '') or show.get('summary', ''),
'plotoutline': show.get('summary', ''),
'date': _convert_date(show.get('updated_at', ''))
}
item = xbmcgui.ListItem(show['title'], iconImage=fanart)
item.setInfo('video', infoLabels)
item.setProperty('Fanart_Image', fanart)
url = self._build_url({'seasons_url': show['_links']['seasons']['href']})
items.append((url, item, True))
xbmcplugin.addDirectoryItems(HANDLE, items)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_TITLE)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_DATE)
xbmcplugin.endOfDirectory(HANDLE)
def listSeasons(self, seasons_url):
seasons = self.api.get_seasons(seasons_url)
for season in seasons:
infoLabels = {
'title': season['title'],
'plot': season.get('season_summary', ''),
'date': _convert_date(season.get('updated_at', ''))
}
item = xbmcgui.ListItem(season['title'])
item.setInfo('video', infoLabels)
if 'image' in season['_links']:
fanart = season['_links']['image']['href'].format(size='500x500')
item.setProperty('Fanart_Image', fanart)
item.setIconImage(fanart)
url = self._build_url({'episodes_url': season['_links']['videos']['href']})
xbmcplugin.addDirectoryItem(HANDLE,
url,
item, True)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_TITLE)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_DATE)
xbmcplugin.endOfDirectory(HANDLE)
def listEpisodes(self, episodes_url):
items = list()
episodes = self.api.get_episodes(episodes_url)
for episode in episodes:
fanart = episode['_links']['image']['href'].format(size='500x500')
info_labels = {
'title': episode['title'],
'studio': ADDON.getAddonInfo('name'),
'plot': episode['description'] or episode['summary'],
'plotoutline': episode['summary'],
'tvshowtitle': episode['format_title']
}
if 'duration' in episode and episode['duration'] is not None:
info_labels['duration'] = int(episode['duration'])
if 'broadcasts' in episode:
if 'air_at' in episode['broadcasts'] and episode['broadcasts']['air_at'] is not None:
airdate = episode['air_at']
info_labels['date'] = _convert_date(airdate)
info_labels['year'] = int(airdate[0:4])
if 'format_position' in episode:
if episode['format_position']['is_episodic'] == 'true':
if 'episode' in episode['format_position'] and episode['format_position']['episode'] is not None:
info_labels['episode'] = int(episode['episode'])
item = xbmcgui.ListItem(episode['title'], iconImage=fanart)
item.setInfo('video', info_labels)
item.setProperty('IsPlayable', 'true')
item.setProperty('Fanart_Image', fanart)
streams = self.api.get_streams(episode)
url = ""
if 'hls' in streams and streams['hls'] is not None:
url = streams['hls']
elif 'high' in streams and streams['high'] is not None:
url = streams['high']
elif 'medium' in streams and streams['medium'] is not None:
url = streams['medium']
elif 'low' in streams and streams['low'] is not None:
url = streams['low']
url = self._build_url({'action': 'playVideo', 'playVideo': url, 'title': episode['title']})
items.append((url, item))
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_EPISODE)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_TITLE)
xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_DATE)
xbmcplugin.addDirectoryItems(HANDLE, items)
xbmcplugin.endOfDirectory(HANDLE)
def getSubtitles(self, videoUrl):
videoPath = videoUrl.replace('playlist.m3u8', '')
playListFile = urllib.urlopen(videoUrl).read()
playListFile = playListFile.encode('utf-8')
srtFile = None
subsFile = None
vttSubs = None
for line in playListFile.splitlines(True):
subsearch = re.match(r'^#EXT-X-MEDIA:.*ID="subs".*AUTOSELECT=YES.*URI="(\w*.m3u8)', line)
if subsearch:
subsFile = subsearch.group(1)
break
if subsFile:
subsFile = urllib.urlopen(videoPath + subsFile).read()
subsFile = subsFile.encode('utf8')
vttSubs = ''
for line in subsFile.splitlines(True):
chunk = re.match(r"(.*).webvtt", line)
if chunk:
vttSubs = vttSubs + '\n'
vttSubs = vttSubs + urllib.urlopen(videoPath + chunk.group()).read()
if vttSubs:
srtFile = os.path.join(xbmc.translatePath("special://temp"), 'tv3play.srt')
srtSubs = [line.replace('.',',') for line in vttSubs.splitlines(True)]
fh = open(srtFile, 'w')
try:
fh.writelines(srtSubs[3:])
finally:
fh.close()
return srtFile
def playVideo(self, videoUrl, videoTitle):
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist.clear()
playlist.add(videoUrl, xbmcgui.ListItem(videoTitle, iconImage=ICON))
xbmcplugin.setResolvedUrl(HANDLE, True, playlist[0])
player = xbmc.Player();
subs_file = self.getSubtitles(videoUrl)
if subs_file:
start_time = time.time();
while not player.isPlaying() and time.time() - start_time < 30:
time.sleep(1);
if player.isPlaying():
xbmc.Player().setSubtitles(subs_file)
def displayError(self, message='n/a'):
heading = buggalo.getRandomHeading()
line1 = ADDON.getLocalizedString(30200)
line2 = ADDON.getLocalizedString(30201)
xbmcgui.Dialog().ok(heading, line1, line2, message)
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf8')
xbmc.log("{0}{1}".format(sys.argv[0], sys.argv[2]))
ADDON = xbmcaddon.Addon()
PATH = sys.argv[0]
HANDLE = int(sys.argv[1])
PARAMS = urlparse.parse_qs(sys.argv[2][1:])
ICON = os.path.join(ADDON.getAddonInfo('path'), 'icon.png')
FANART = os.path.join(ADDON.getAddonInfo('path'), 'fanart.jpg')
CACHE_PATH = xbmc.translatePath(ADDON.getAddonInfo("Profile"))
if not os.path.exists(CACHE_PATH):
os.makedirs(CACHE_PATH)
r = None
if 'region' in PARAMS:
r = PARAMS['region'][0]
elif ADDON.getSetting('region') in MtgApi.REGIONS:
r = ADDON.getSetting('region')
buggalo.SUBMIT_URL = 'http://buggalo.ext.norman.info/submit.php'
tv3PlayAddon = TV3PlayAddon(r)
try:
if 'playVideo' in PARAMS:
tv3PlayAddon.playVideo(PARAMS['playVideo'][0], PARAMS['title'][0])
elif 'episodes_url' in PARAMS:
tv3PlayAddon.listEpisodes(PARAMS['episodes_url'][0])
elif 'seasons_url' in PARAMS:
tv3PlayAddon.listSeasons(PARAMS['seasons_url'][0])
elif 'channel' in PARAMS:
tv3PlayAddon.listShows(PARAMS['channel'][0])
elif r:
tv3PlayAddon.listChannels()
else:
tv3PlayAddon.listRegions()
except MtgApiException as ex:
tv3PlayAddon.displayError(str(ex))
except Exception:
buggalo.onExceptionRaised()