[75d7d7]: plugin.video.the.daily.show / addon.py Maximize Restore History

Download this file

addon.py    524 lines (431 with data), 17.1 kB

  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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
import os
import re
import sys
import urllib
import urllib2
from BeautifulSoup import BeautifulSoup as BS3
from requests import get
from xbmc import translatePath as tp
from xbmc import log
import xbmc
import xbmcaddon
import xbmcgui
import xbmcplugin
addon = xbmcaddon.Addon('plugin.video.the.daily.show')
pluginhandle = int(sys.argv[1])
image_fanart = tp(os.path.join(addon.getAddonInfo('path'), 'fanart.jpg'))
image_fanart_search = tp(
os.path.join(addon.getAddonInfo('path'),
'fanart-search.jpg'))
xbmcplugin.setPluginFanart(pluginhandle, image_fanart, color2='0xFFFF3300')
TVShowTitle = 'The Daily Show'
if xbmcplugin.getSetting(pluginhandle, "sort") == '0':
SORTORDER = 'date'
elif xbmcplugin.getSetting(pluginhandle, "sort") == '1':
SORTORDER = 'views'
elif xbmcplugin.getSetting(pluginhandle, "sort") == '2':
SORTORDER = 'rating'
class Guest(object):
def __init__(self, data):
self.soup = data
def day(self):
raw_text = self.soup('a', {'class': 'full-episode-url'})[0].getText()
raw_text = raw_text.replace('Full Episode Available', '')
m = re.search(r'(.*) - .*', raw_text)
return m.group(1)
def name(self):
return self.soup('span', {'class': 'title'})[0].getText().replace('Exclusive - ', '')
def url(self):
return self.soup('a', {'class': 'imageHolder'})[0]['href']
class Episode:
def __init__(self, rtmp, bitrate, width, height):
self.rtmp = rtmp
self.bitrate = bitrate
self.width = width
self.height = height
@staticmethod
def fromUrlData(data):
# get attributes
m = re.search(
"""width="([0-9]+).*height="([0-9]+).*bitrate="([0-9]+).*<src>(rtmpe[^<]+)</src>""",
data,
re.S)
if m:
(width, height, bitrate, rtmp) = m.groups()
return Episode(rtmp, int(bitrate), int(width), int(height))
# this should not happen
raise Exception
def __repr__(self):
return "rtmp: %s, bitrate: %i, width: %i, height: %i" % (self.rtmp,
self.bitrate, self.width, self.height)
def get_episodes(data):
# split urldata into renditions
renditions = re.findall("(<rendition.*?</rendition>)", data, re.S)
# return a list of episodes
return [Episode.fromUrlData(r) for r in renditions]
def get_settings_bitrate():
# map plugin settings to actual bitrates
setting2bitrate = {'1': 400, '2': 750, '3': 1200, '4': 1700, '5': 2200,
'6': 3500}
setting = xbmcplugin.getSetting(pluginhandle, "bitrate")
lbitrate = setting2bitrate.get(setting, 0)
return lbitrate
# Common
def get_url(url):
try:
log('The Daily Show --> get_url :: url = ' + url)
txdata = None
txheaders = {
'Referer': 'http://www.thedailyshow.com/videos/',
'X-Forwarded-For': '12.13.14.15',
'User-Agent':
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US;rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3 ( .NET CLR 3.5.30729)',
}
req = urllib2.Request(url, txdata, txheaders)
response = urllib2.urlopen(req)
link = response.read()
response.close()
except urllib2.URLError as e:
log('Error code: ', e.code)
return False
else:
return link
def add_directory_entry(name, identifier):
"""Adds a directory entry to the xbmc ListItem"""
url = "{sysarg}?mode={mode}".format(sysarg=sys.argv[0], mode=identifier)
liz = xbmcgui.ListItem(name, iconImage="DefaultFolder.png")
liz.setInfo(type="Video", infoLabels={"Title": name})
liz.setProperty('fanart_image', image_fanart)
xbmcplugin.addDirectoryItem(handle=pluginhandle, url=url,
listitem=liz, isFolder=True)
# Root listing
def root():
msg = addon.getLocalizedString(30030)
add_directory_entry(msg, 'full')
msg = addon.getLocalizedString(30031)
add_directory_entry(msg, 'guests')
msg = addon.getLocalizedString(30032)
add_directory_entry(msg, 'search')
msg = addon.getLocalizedString(30033)
add_directory_entry(msg, 'browse')
xbmcplugin.endOfDirectory(pluginhandle)
def full_episodes():
xbmcplugin.setContent(pluginhandle, 'episodes')
xbmcplugin.addSortMethod(pluginhandle, xbmcplugin.SORT_METHOD_NONE)
full = 'http://www.thedailyshow.com/full-episodes/'
allData = get_url(full)
episodeURLs = re.compile(
'<a href="(http://www.thedailyshow.com/full-episodes/....+?)"').findall(
allData)
episodeURLSet = set(episodeURLs)
listings = []
for episodeURL in episodeURLs:
if episodeURL in episodeURLSet:
episodeURLSet.remove(episodeURL)
episodeData = get_url(episodeURL)
title = re.compile(
'<meta property="og:title" content="(.+?)"').search(
episodeData)
thumbnail = re.compile(
'<meta property="og:image" content="(.+?)"').search(
episodeData)
description = re.compile(
'<meta property="og:description" content="(.+?)"').search(
episodeData)
airDate = re.compile(
'<meta itemprop="uploadDate" content="(.+?)"').search(
episodeData)
epNumber = re.compile('/season_\d+/(\d+)').search(episodeData)
link = re.compile(
'<link rel="canonical" href="(.+?)"').search(
episodeData)
listing = []
listing.append(title.group(1))
listing.append(link.group(1))
listing.append(thumbnail.group(1))
listing.append(description.group(1))
listing.append(airDate.group(1))
listing.append(epNumber.group(1))
listings.append(listing)
for name, link, thumbnail, plot, date, seasonepisode in listings:
mode = "play"
season = int(seasonepisode[:-3])
episode = int(seasonepisode[-3:])
u = sys.argv[
0] + "?url=" + urllib.quote_plus(
link) + "&mode=" + str(
mode) + "&name=" + urllib.quote_plus(
name)
u += "&season=" + urllib.quote_plus(str(season))
u += "&episode=" + urllib.quote_plus(str(episode))
u += "&premiered=" + urllib.quote_plus(date)
u += "&plot=" + urllib.quote_plus(plot)
u += "&thumbnail=" + urllib.quote_plus(thumbnail)
liz = xbmcgui.ListItem(
name,
iconImage="DefaultFolder.png",
thumbnailImage=thumbnail)
liz.setInfo(
type="Video", infoLabels={"Title": BS3(name, convertEntities=BS3.HTML_ENTITIES),
"Plot":
BS3(plot,
convertEntities=BS3.HTML_ENTITIES),
"Season": season,
"Episode": episode,
"premiered": date,
"TVShowTitle": TVShowTitle})
liz.setProperty('IsPlayable', 'true')
liz.setProperty('fanart_image', image_fanart)
xbmcplugin.addDirectoryItem(handle=pluginhandle, url=u, listitem=liz)
xbmcplugin.endOfDirectory(pluginhandle)
def guests():
gurl = "http://www.thedailyshow.com/feeds/search?keywords=&tags=interviews&sortOrder=desc&sortBy=date&page=1"
data = get_url(gurl).replace('</pre>', '</div>')
soup = BS3(data)
guest_items = soup('div', {'class': 'entry'})
mode = "play"
for item in guest_items:
g = Guest(item)
liz = xbmcgui.ListItem(g.name(), iconImage='', thumbnailImage='')
liz.setInfo(type="Video", infoLabels={"Title": g.name(),
"TVShowTitle": 'The Daily Show'})
liz.setProperty('IsPlayable', 'true')
liz.setProperty('fanart_image', image_fanart)
u = sys.argv[
0] + "?url=" + g.url(
) + "&mode=" + str(
mode) + "&name=" + g.name(
)
xbmcplugin.addDirectoryItem(handle=pluginhandle, url=u, listitem=liz)
xbmcplugin.endOfDirectory(pluginhandle)
def extract_search_results_from_response(response):
"""Creates an xbmc DirectoryItem with ListItems of videos extracted from
the http results.
Returns False if there are no video urls found in the request.
Otherwise, returns True."""
soup = BS3(response.text)
results = soup.find('div', {'class': 'search-results'})
if results is None:
return False
for entry in results.findAll('div', {'class': 'entry'}):
video_page_url = entry.find('meta', dict(itemprop="url"))['content']
name = entry.find('meta', dict(itemprop="name"))['content']
description = entry.find(
'meta',
dict(itemprop="description"))['content']
thumbnail = entry.find(
'meta',
dict(itemprop="thumbnailUrl"))['content']
if thumbnail:
# strip unnecessary ?width= parameters that make the image too
# small
thumbnail = re.search(r"(.*)\?.*", thumbnail).groups()[0]
duration = None
duration_match = re.search(
'T(\d+)M',
entry.find('meta',
dict(itemprop="duration"))['content'])
if duration_match:
duration = duration_match.groups()[0]
upload_date = entry.find(
'meta',
dict(itemprop="uploadDate"))['content']
url = "{sysarg}?mode={mode}&name={name}&url={video_page_url}".format(
sysarg=sys.argv[0],
name=urllib.quote_plus(name),
video_page_url=urllib.quote_plus(video_page_url),
mode="play")
date_and_name = "%s - %s" % (upload_date.replace('-','/'), name)
liz = xbmcgui.ListItem(date_and_name, thumbnailImage=thumbnail)
liz.setInfo(type="Video", infoLabels={"Title": name,
"plot": description,
"premiered": upload_date,
"aired": upload_date,
"duration": duration,
"TVShowTitle": TVShowTitle
})
liz.setProperty('IsPlayable', 'true')
liz.setProperty('fanart_image', image_fanart_search)
xbmcplugin.addDirectoryItem(handle=pluginhandle, url=url, listitem=liz)
return True
def get_user_input(title, default="", hidden=False):
"""Display a virtual keyboard to the user"""
keyboard = xbmc.Keyboard(default, title)
keyboard.setHiddenInput(hidden)
keyboard.doModal()
if keyboard.isConfirmed():
return keyboard.getText()
else:
return None
def search():
msg = addon.getLocalizedString(30032)
query = get_user_input(msg)
if not query:
return
response = get(
"http://www.thedailyshow.com/videos",
params=dict(term=query))
if extract_search_results_from_response(response) is True:
xbmcplugin.endOfDirectory(pluginhandle)
else:
mydialogue = xbmcgui.Dialog()
msg = addon.getLocalizedString(30022)
mydialogue.ok(heading=TVShowTitle,
line1=msg)
def browse():
"""Browse videos by Date"""
mydialogue = xbmcgui.Dialog()
msg = addon.getLocalizedString(30020)
datestring = mydialogue.numeric(type=1, heading=msg)
if not datestring:
return
day, month, year = datestring.split("/")
urlstring = "http://www.thedailyshow.com/feeds/search" + \
"?startDate={year}-{month}-{day}&tags=&keywords=&sortOrder=desc&sortBy=date&page=1".format(
day=day,
month=month,
year=year)
response = get(urlstring)
if extract_search_results_from_response(response) is True:
xbmcplugin.endOfDirectory(pluginhandle)
else:
mydialogue = xbmcgui.Dialog()
msg = addon.getLocalizedString(30021)
mydialogue.ok(heading=TVShowTitle,
line1=msg)
def play_random():
"""Play a random videos"""
raise NotImplementedError
# Do something intelligent to select from all available videos while
# minimizng the wait time prior to play. We suggest selecting a random,
# valid date.
# Play Video
def play_video(name, url):
data = get_url(url)
uri = re.compile('"http://media.mtvnservices.com/(.+?)"/>').findall(
data)[0].replace('fb/', '').replace('.swf', '')
rtmp = grab_rtmp(uri)
item = xbmcgui.ListItem(
name,
iconImage="DefaultVideo.png",
thumbnailImage=thumbnail,
path=rtmp)
item.setInfo(type="Video", infoLabels={"Title": name,
"Plot": plot,
"premiered": premiered,
"Season": int(season),
"Episode": int(episode),
"TVShowTitle": TVShowTitle})
item.setProperty('fanart_image', image_fanart)
xbmcplugin.setResolvedUrl(pluginhandle, True, item)
# Play Full Episode
def play_full_episode(name, url):
data = get_url(url)
uri = re.compile(
'(mgid:cms:episode:thedailyshow.com:\d{6}|mgid:cms:video:thedailyshow.com:\d{6})').findall(data)[0]
url = 'http://shadow.comedycentral.com/feeds/video_player/mrss/?uri=' + uri
data = get_url(url)
uris = re.compile('<guid isPermaLink="false">(.+?)</guid>').findall(data)
stacked_url = 'stack://'
for uri in uris:
rtmp = grab_rtmp(uri)
stacked_url += rtmp.replace(',', ',,') + ' , '
stacked_url = stacked_url[:-3]
log('stacked_url --> %s' % stacked_url)
item = xbmcgui.ListItem(
name,
iconImage="DefaultVideo.png",
thumbnailImage=thumbnail,
path=stacked_url)
log('item --> %s' % item)
item.setInfo(type="Video", infoLabels={"Title": name,
"Plot": plot,
"premiered": premiered,
"Season": int(season),
"Episode": int(episode),
"TVShowTitle": TVShowTitle})
item.setProperty('fanart_image', image_fanart)
xbmcplugin.setResolvedUrl(pluginhandle, True, item)
# Grab rtmp
def grab_rtmp(uri):
url = 'http://www.comedycentral.com/global/feeds/entertainment/media/mediaGenEntertainment.jhtml?uri=' + \
uri + '&showTicker=true'
mp4_url = "http://mtvnmobile.vo.llnwd.net/kip0/_pxn=0+_pxK=18639+_pxE=/44620/mtvnorigin"
data = get_url(url)
episodes = get_episodes(data)
# sort episodes by bitrate ascending
episodes.sort(key=lambda x: x.bitrate)
# chose maximum bitrate by default
ep = episodes[-1]
# check user settings
lbitrate = get_settings_bitrate()
if lbitrate:
# use the largest bitrate smaller-or-equal to the user-chosen value
ep = filter(lambda x: x.bitrate <= lbitrate, episodes)[-1]
furl = mp4_url + ep.rtmp.split('viacomccstrm')[2]
log('furl --> %s' % furl)
return furl
# Since params are defined by us in this module, we could use JSON
# representation here instead of parsing this by hand
def get_params():
param = []
paramstring = sys.argv[2]
if len(paramstring) >= 2:
params = sys.argv[2]
cleanedparams = params.replace('?', '')
if (params[len(params) - 1] == '/'):
params = params[0:len(params) - 2]
pairsofparams = cleanedparams.split('&')
param = {}
for i in range(len(pairsofparams)):
splitparams = {}
splitparams = pairsofparams[i].split('=')
if (len(splitparams)) == 2:
param[splitparams[0]] = splitparams[1]
return param
params = get_params()
url = None
name = None
mode = None
try:
url = urllib.unquote_plus(params["url"])
except:
pass
try:
name = urllib.unquote_plus(params["name"])
except:
pass
try:
mode = params["mode"]
except:
pass
try:
thumbnail = urllib.unquote_plus(params["thumbnail"])
except:
thumbnail = ''
try:
season = int(params["season"])
except:
season = 0
try:
episode = int(params["episode"])
except:
episode = 0
try:
premiered = urllib.unquote_plus(params["premiered"])
except:
premiered = ''
try:
plot = urllib.unquote_plus(params["plot"])
except:
plot = ''
#log("Mode: " + str(mode))
#log("URL: " + str(url))
#log("Name: " + str(name))
mode_handlers = {"browse": browse,
"full": full_episodes,
"guests": guests,
"play": lambda: play_full_episode(name, url),
"search": search,
}
mode_handlers.get(mode, root)()