[956993]: plugin.video.pbs / default.py  Maximize  Restore  History

Download this file

432 lines (408 with data), 17.9 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
import xbmc, xbmcgui, xbmcplugin, xbmcaddon, urllib, re, base64, string, sys, os, coveapi, buggalo
import simplejson as json
plugin = "PBS"
__author__ = 'stacked <stacked.xbmc@gmail.com>'
__url__ = 'http://code.google.com/p/plugin/'
__date__ = '02-28-2013'
__version__ = '3.0.11'
settings = xbmcaddon.Addon( id = 'plugin.video.pbs' )
buggalo.SUBMIT_URL = 'http://www.xbmc.byethost17.com/submit.php'
dbg = False
dbglevel = 3
programs_thumb = os.path.join( settings.getAddonInfo( 'path' ), 'resources', 'media', 'programs.png' )
topics_thumb = os.path.join( settings.getAddonInfo( 'path' ), 'resources', 'media', 'topics.png' )
search_thumb = os.path.join( settings.getAddonInfo( 'path' ), 'resources', 'media', 'search.png' )
next_thumb = os.path.join( settings.getAddonInfo( 'path' ), 'resources', 'media', 'next.png' )
pbskids_thumb = os.path.join( settings.getAddonInfo( 'path' ), 'resources', 'media', 'pbskids.png' )
fanart = os.path.join( settings.getAddonInfo( 'path' ), 'fanart.jpg' )
cove = coveapi.connect(base64.b64decode(settings.getLocalizedString( 30010 )),
base64.b64decode(settings.getLocalizedString( 30011 )))
import CommonFunctions
common = CommonFunctions
common.plugin = plugin + ' ' + __version__
from addonfunc import addListItem, playListItem, getUrl, setViewMode, getParameters, retry, useragent
try:
if common.getXBMCVersion() >= 12.0:
HIGH = 'iPad'
else:
HIGH = 'MP4 1200k'
except:
HIGH = 'MP4 1200k'
def clean( string ):
list = [( '&amp;', '&' ), ( '&quot;', '"' ), ( '&#39;', '\'' ), ( '\n','' ), ( '\r', ''), ( '\t', ''), ( '</p>', '' ), ( '<br />', ' ' ),
( '<br/>', ' ' ), ( '<b>', '' ), ( '</b>', '' ), ( '<p>', '' ), ( '<div>', '' ), ( '</div>', '' ), ( '<strong>', ' ' ),
( '<\/strong>', ' ' ), ( '</strong>', ' ' ), ( '&hellip;', '...' ), ( '&ntilde;', 'n' ), ('&ldquo;', '\"'), ('&rdquo;', '\"'),
('<a href=\"', ''), ('</a>', ''), ('\">', ' ')]
for search, replace in list:
string = string.replace( search, replace )
return string
def clean_type( string ):
if string == None: return 'None'
list = [( '-16x9', '' ), ( '-4x3', '' ), ( ' 16x9', '' ), ( ' 4x3','' )]
for search, replace in list:
string = string.replace( search, replace )
return string
def build_main_directory():
main=[
( settings.getLocalizedString( 30000 ), programs_thumb, '1' ),
( settings.getLocalizedString( 30001 ), topics_thumb, '2' ),
( settings.getLocalizedString( 30003 ), search_thumb, '4' ),
( settings.getLocalizedString( 30014 ), pbskids_thumb, '1' )
]
for name, thumbnailImage, mode in main:
u = { 'mode': mode, 'name': name }
addListItem(label = name, image = thumbnailImage, url = u, isFolder = True, infoLabels = False, fanart = fanart)
try:
build_most_watched_directory()
except:
pass
xbmcplugin.addSortMethod( handle = int(sys.argv[1]), sortMethod = xbmcplugin.SORT_METHOD_NONE )
setViewMode("501")
xbmcplugin.endOfDirectory( int( sys.argv[1] ) )
def build_most_watched_directory():
url = 'http://video.pbs.org/'
data = getUrl( url )
list = common.parseDOM(data, "ul", attrs = { "class": "video-list clear clearfix" })
videos = common.parseDOM(list, "span", attrs = { "class": "title clear clearfix" })
img = common.parseDOM(list, "img", ret = "src")
count = 0
for video in videos:
program_id = common.parseDOM(video, "a", ret = "href")[0].rsplit('/')[4]
title = common.parseDOM(video, "a")[0]
label = clean(title)
thumb = img[count]
infoLabels = { "Title": label, "Director": "PBS", "Studio": clean(title.rsplit(' | ')[0]) }
u = { 'mode': '0', 'name': label, 'program_id': program_id, 'topic': 'False', 'page': '0' }
addListItem(label = label, image = thumb, url = u, isFolder = False, infoLabels = infoLabels, fanart = fanart)
count += 1
@retry(TypeError)
def build_programs_directory( name, page ):
checking = True
while checking:
start = str( 200 * page )
#data = cove.programs.filter(fields='associated_images,mediafiles,categories',filter_categories__namespace__name='COVE Taxonomy',order_by='title',limit_start=start)
if name != settings.getLocalizedString( 30014 ) and name != settings.getLocalizedString( 30000 ):
data = cove.programs.filter(fields='associated_images',order_by='title',limit_start=start,filter_title=name)
else:
data = cove.programs.filter(fields='associated_images',order_by='title',limit_start=start)
if ( len(data) ) == 200:
page = page + 1
else:
checking = False
for results in data:
if len(results['nola_root'].strip()) != 0:
if name == settings.getLocalizedString( 30014 ):
if results['tp_account'] == 'PBS DP KidsGo':
process_list = True
else:
process_list = False
else:
if results['tp_account'] == 'PBS DP KidsGo':
process_list = False
else:
process_list = True
if process_list:
if len(results['associated_images']) >= 2:
thumb = results['associated_images'][1]['url']
else:
thumb = programs_thumb
program_id = re.compile( '/cove/v1/programs/(.*?)/' ).findall( results['resource_uri'] )[0]
infoLabels = { "Title": results['title'].encode('utf-8'), "Plot": clean(results['long_description']) }
u = { 'mode': '0', 'name': results['title'].encode('utf-8'), 'program_id': program_id }
addListItem(label = results['title'], image = thumb, url = u, isFolder = True, infoLabels = infoLabels, fanart = fanart)
# if ( len(data) ) == 200:
# u = { 'mode': '1', 'page': str( int( page ) + 1 ) }
# addListItem(label = '[Next Page (' + str( int( page ) + 2 ) + ')]', image = next_thumb, url = u, isFolder = True, infoLabels = False)
xbmcplugin.addSortMethod( handle = int(sys.argv[1]), sortMethod = xbmcplugin.SORT_METHOD_NONE )
if name == settings.getLocalizedString( 30014 ):
setViewMode("500")
else:
setViewMode("503")
xbmcplugin.endOfDirectory( int( sys.argv[1] ) )
def build_topics_directory():
data = cove.categories.filter(order_by='name',filter_namespace__name='COVE Taxonomy')
item = None
for results in data:
if item != results['name']:
u = { 'mode': '0', 'name': results['name'], 'topic': 'True' }
addListItem(label = results['name'], image = topics_thumb, url = u, isFolder = True, infoLabels = False, fanart = fanart)
item = results['name']
xbmcplugin.addSortMethod( handle = int(sys.argv[1]), sortMethod = xbmcplugin.SORT_METHOD_NONE )
setViewMode("515")
xbmcplugin.endOfDirectory( int( sys.argv[1] ) )
def build_search_keyboard():
keyboard = xbmc.Keyboard( '', settings.getLocalizedString( 30003 ) + ' ' + settings.getLocalizedString( 30013 ) )
keyboard.doModal()
if ( keyboard.isConfirmed() == False ):
return
search_string = keyboard.getText()
if len( search_string ) == 0:
return
#build_search_directory( search_string, page )
find_videos( search_string, 'program_id', 'search', 0 )
def build_search_directory( url, page ):
save_url = url.replace( ' ', '%20' )
url = 'http://www.pbs.org/search/?q=' + url.replace( ' ', '%20' ) + '&ss=pbs&mediatype=Video&start=' + str( page * 10 )
data = getUrl( url )
title_id_thumb = re.compile('<a title="(.*?)" target="" rel="nofollow" onclick="EZDATA\.trackGaEvent\(\'search\', \'navigation\', \'external\'\);" href="(.*?)"><img src="(.*?)" class="ez-primaryThumb"').findall(data)
program = re.compile('<p class="ez-metaextra1 ez-icon">(.*?)</p>').findall(data)
plot = re.compile('<p class="ez-desc">(.*?)<div class="(ez-snippets|ez-itemUrl)">', re.DOTALL).findall(data)
video_count = re.compile('<b class="ez-total">(.*?)</b>').findall(data)
if len(title_id_thumb) == 0:
dialog = xbmcgui.Dialog()
ok = dialog.ok( plugin , settings.getLocalizedString( 30004 ) + '\n' + settings.getLocalizedString( 30005 ) )
return
item_count = 0
for title, id, thumb in title_id_thumb:
infoLabels = { "Title": clean( title ) , "Director": "PBS", "Studio": clean( program[item_count] ), "Plot": clean( plot[item_count][0] ) }
u = { 'mode': '0', 'name': clean( program[item_count] ), 'program_id': id.rsplit('/')[4], 'topic': 'False' }
addListItem(label = clean( title ), image = thumb, url = u, isFolder = False, infoLabels = infoLabels, fanart = fanart)
item_count += 1
xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_STUDIO )
xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_LABEL )
if page == 0:
build_programs_directory( save_url.replace( '%20', ' ' ), 0 )
if ( int( video_count[0] ) - ( 10 * int( page ) ) ) > 10:
u = { 'mode': '6', 'page': str( int( page ) + 1 ), 'url': save_url }
addListItem(label = '[Next Page (' + str( int( page ) + 2 ) + ')]', image = next_thumb, url = u, isFolder = True, infoLabels = False, fanart = fanart)
xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_STUDIO )
xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_LABEL )
setViewMode("503")
xbmcplugin.endOfDirectory( int( sys.argv[1] ) )
@retry(TypeError)
def find_videos( name, program_id, topic, page ):
if settings.getSetting("video") == '0':
type = ['MP4 800k', 'Legacy KIDS encoding', HIGH]
else:
type = [HIGH, 'MP4 800k', 'Legacy KIDS encoding']
start = str( 200 * page )
url = 'None'
backup_url = None
if topic == 'True':
program_id = 'program_id'
data = cove.videos.filter(fields='associated_images,mediafiles,categories',filter_categories__name=name,order_by='-airdate',filter_availability_status='Available',limit_start=start,exclude_type__in='Chapter,Promotion')
elif topic == 'False':
data = cove.videos.filter(fields='associated_images,mediafiles',filter_tp_media_object_id=program_id,limit_start=start)
print "PBS - Video ID: " + program_id
if len(data) == 0:
dialog = xbmcgui.Dialog()
ok = dialog.ok( plugin , settings.getLocalizedString( 30009 ) + '\n' + 'http://video.pbs.org/video/' + program_id )
return
elif topic == 'search':
data = cove.videos.filter(fields='associated_images,mediafiles',filter_title__contains=name,order_by='-airdate',filter_availability_status='Available',limit_start=start)
if len(data) == 0:
data = cove.videos.filter(fields='associated_images,mediafiles',filter_title__contains=string.capwords(name),order_by='-airdate',filter_availability_status='Available',limit_start=start)
else:
topic = 'topic'
data = cove.videos.filter(fields='associated_images,mediafiles',filter_program=program_id,order_by='-airdate',filter_availability_status='Available',limit_start=start,exclude_type__in='Chapter,Promotion')
if len(data) <= 1:
data = cove.videos.filter(fields='associated_images,mediafiles',filter_program=program_id,order_by='-airdate',filter_availability_status='Available',limit_start=start)
for results in data:
if len(results['mediafiles']) != 0:
encoding = {}
if results['associated_images'] != None and len(results['associated_images']) > 0:
thumb = results['associated_images'][0]['url']
else:
thumb = 'None'
for videos in results['mediafiles']:
encoding[clean_type(videos['video_encoding']['name'])] = { 'url': videos['video_data_url'], 'backup_url': videos['video_download_url'] }
cycle = 0
while cycle <= 2:
if type[cycle] in encoding:
url = str(encoding[type[cycle]]['url'])
backup_url = str(encoding[type[cycle]]['backup_url'])
break
cycle += 1
if cycle == 3:
url = str(encoding.items()[0][1]['url'])
backup_url = str(encoding.items()[0][1]['backup_url'])
infoLabels = { "Title": results['title'].encode('utf-8'), "Director": "PBS", "Studio": name, "Plot": results['long_description'].encode('utf-8'), "Aired": results['airdate'].rsplit(' ')[0], "Duration": str((int(results['mediafiles'][0]['length_mseconds'])/1000)/60) }
u = { 'mode': '5', 'name': results['title'].encode('utf-8'), 'url': url, 'thumb': thumb, 'plot': results['long_description'].encode('utf-8'), 'studio': name, 'backup_url': backup_url }
addListItem(label = results['title'].encode('utf-8'), image = thumb, url = u, isFolder = False, infoLabels = infoLabels, fanart = fanart, duration = str(int(results['mediafiles'][0]['length_mseconds'])/1000))
if topic == 'False':
play_video( results['title'].encode('utf-8'), url, thumb, results['long_description'].encode('utf-8'), name.encode('utf-8'), None, backup_url )
return
if len(data) == 0:
dialog = xbmcgui.Dialog()
ok = dialog.ok( plugin , settings.getLocalizedString( 30012 ) + ' ' + name + '.' )
return
if ( len(data) ) == 200:
u = { 'mode': '0', 'name': name, 'program_id': program_id, 'topic': topic, 'page': str( int( page ) + 1 ) }
addListItem(label = '[Next Page (' + str( int( page ) + 2 ) + ')]', image = next_thumb, url = u, isFolder = True, infoLabels = False, fanart = fanart)
if topic == 'search':
build_programs_directory( name, 0 )
xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_UNSORTED )
xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_VIDEO_RUNTIME )
xbmcplugin.addSortMethod( handle=int( sys.argv[ 1 ] ), sortMethod=xbmcplugin.SORT_METHOD_LABEL )
setViewMode("503")
xbmcplugin.endOfDirectory( int( sys.argv[1] ) )
def play_video( name, url, thumb, plot, studio, starttime, backup_url ):
print 'PBS - ' + studio + ' - ' + name
print url
playpath = False
defaulturl = url
#Release Urls
if 'http://release.theplatform.com/' in url:
data = getUrl( url + '&format=SMIL' )
try:
msg = common.parseDOM(data, "ref", ret = "title")[0]
if msg == 'Unauthorized':
dialog = xbmcgui.Dialog()
ok = dialog.ok( plugin , settings.getLocalizedString( 30008 ) )
return
if msg == 'Unavailable':
dialog = xbmcgui.Dialog()
ok = dialog.ok( plugin , settings.getLocalizedString( 30015 ) + '\n' + settings.getLocalizedString( 30016 ) )
return
except:
print 'PBS - Failed to check video status'
try:
base = re.compile( '<meta base="(.+?)" />' ).findall( data )[0]
src = re.compile( '<ref src="(.+?)" title="(.+?)" (author)?' ).findall( data )[0][0]
if base == 'http://ad.doubleclick.net/adx/':
src_data = src.split( "&lt;break&gt;" )
url = src_data[0] + "mp4:" + src_data[1].replace('mp4:','')
elif base != 'http://ad.doubleclick.net/adx/' and 'http://' in base:
url = clean(base + src.replace('mp4:',''))
else:
url = base
playpath = "mp4:" + src.replace('mp4:','')
except:
print 'PBS - Release backup_url'
if backup_url != 'None':
url = backup_url
backup_url = 'None'
else:
url = 'None'
backup_url = 'None'
#Empty Urls
if url == 'None':
print 'PBS - Empty backup_url'
if backup_url != 'None':
url = backup_url
backup_url = 'None'
else:
url = 'None'
backup_url = 'None'
#Redirect Urls
if 'http://urs.pbs.org/redirect/' in url:
redirect = True
while redirect:
try:
content = getUrl( url + '?format=json' )
query = json.loads(content)
if query['http_code'] != 302 or query['url'] == None:
if query['http_code'] == 403:
dialog = xbmcgui.Dialog()
ok = dialog.ok( plugin , settings.getLocalizedString( 30015 ) + '\n' + settings.getLocalizedString( 30016 ) )
return
else:
raise Exception("redirect Error")
if 'mp4:' in query['url']:
url = query['url'].rsplit('mp4:')[0]
playpath = 'mp4:' + query['url'].rsplit('mp4:')[1]
else:
url = query['url']
redirect = False
except Exception, e:
print str(e)
print 'PBS - Redirect backup_url'
if backup_url != 'None':
if 'http://urs.pbs.org/redirect/' in backup_url:
redirect = True
else:
redirect = False
url = backup_url
backup_url = 'None'
else:
url = 'None'
backup_url = 'None'
redirect = False
#Fails
if url == 'None' and backup_url == 'None':
dialog = xbmcgui.Dialog()
ok = dialog.ok(plugin , settings.getLocalizedString( 30008 ))
ok = dialog.ok(plugin, settings.getLocalizedString( 30051 ))
buggalo.addExtraData('defaulturl', defaulturl)
buggalo.addExtraData('url', url)
buggalo.addExtraData('info', studio + ' - ' + name)
raise Exception("backup_url ERROR")
infoLabels = { "Title": name , "Studio": "PBS: " + studio, "Plot": plot }
playListItem(label = name, image = thumb, path = url, infoLabels = infoLabels, PlayPath = playpath)
params = getParameters(sys.argv[2])
starttime = None
mode = None
name = None
url = None
thumb = None
plot = None
studio = None
program_id = None
backup_url = None
topic = None
page = 0
try:
topic = urllib.unquote_plus( params["topic"] )
except:
pass
try:
backup_url = urllib.unquote_plus( params["backup_url"] )
except:
pass
try:
program_id = urllib.unquote_plus( params["program_id"] )
except:
pass
try:
url = urllib.unquote_plus( params["url"] )
except:
pass
try:
name = urllib.unquote_plus( params["name"] )
except:
pass
try:
mode = int( params["mode"] )
except:
pass
try:
page = int( params["page"] )
except:
pass
try:
thumb = urllib.unquote_plus( params["thumb"] )
except:
pass
try:
plot = urllib.unquote_plus( params["plot"] )
except:
pass
try:
studio = urllib.unquote_plus( params["studio"] )
except:
pass
try:
starttime = int( params["starttime"] )
except:
pass
try:
if mode == None:
print plugin + ' ' + __version__ + ' ' + __date__
if sys.argv[2].startswith('?play='):
find_videos( 'Video', sys.argv[2][6:].strip(), 'False', page )
else:
build_main_directory()
elif mode == 0:
find_videos( name, program_id, topic, page )
elif mode == 1:
build_programs_directory( name, page )
elif mode == 2:
build_topics_directory()
elif mode == 4:
build_search_keyboard()
elif mode == 5:
play_video( name, url, thumb, plot, studio, starttime, backup_url )
elif mode == 6:
build_search_directory( url, page )
except Exception:
buggalo.onExceptionRaised()

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.





No, thanks