Advertisement
Guest User

Untitled

a guest
May 31st, 2011
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.72 KB | None | 0 0
  1. import urllib,urllib2,re,xbmcplugin,xbmcgui,xbmcaddon
  2. import os,datetime
  3. import demjson
  4.  
  5. DATELOOKUP = "http://www.thedailyshow.com/feeds/timeline/coordinates/"
  6.  
  7. pluginhandle = int(sys.argv[1])
  8. shownail = xbmc.translatePath(os.path.join(os.getcwd().replace(';', ''),"icon.png"))
  9. fanart = xbmc.translatePath(os.path.join(os.getcwd().replace(';', ''),'fanart.jpg'))
  10. xbmcplugin.setPluginFanart(pluginhandle, fanart, color2='0xFFFF3300')
  11. TVShowTitle = 'The Daily Show'
  12.  
  13. addon = xbmcaddon.Addon(id='plugin.video.the.daily.show')
  14. test_setting = addon.getSetting('sort')
  15. if test_setting == '0':
  16. SORTORDER = 'date'
  17. elif test_setting == '1':
  18. SORTORDER = 'views'
  19. elif test_setting == '2':
  20. SORTORDER = 'rating'
  21.  
  22. ################################ Common
  23. def getURL( url ):
  24. try:
  25. print 'The Daily Show --> getURL :: url = '+url
  26. txdata = None
  27. txheaders = {
  28. 'Referer': 'http://www.thedailyshow.com/videos/',
  29. 'X-Forwarded-For': '12.13.14.15',
  30. '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)',
  31. }
  32. req = urllib2.Request(url, txdata, txheaders)
  33. #req = urllib2.Request(url)
  34. #req.addheaders = [('Referer', 'http://www.thedailyshow.com/videos'),
  35. # ('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)')]
  36. response = urllib2.urlopen(req)
  37. link=response.read()
  38. response.close()
  39. except urllib2.URLError, e:
  40. error = 'Error code: '+ str(e.code)
  41. xbmcgui.Dialog().ok(error,error)
  42. print 'Error code: ', e.code
  43. return False
  44. else:
  45. return link
  46.  
  47. def addLink(name,url,iconimage='',plot=''):
  48. ok=True
  49. liz=xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=iconimage)
  50. liz.setInfo( type="Video", infoLabels={ "Title": name, "Plot":plot, "TVShowTitle":TVShowTitle})
  51. liz.setProperty('fanart_image',fanart)
  52. ok=xbmcplugin.addDirectoryItem(handle=pluginhandle,url=url,listitem=liz)
  53. return ok
  54.  
  55. def addDir(name,url,mode,iconimage=shownail,plot=''):
  56. u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
  57. ok=True
  58. liz=xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=iconimage)
  59. liz.setInfo( type="Video", infoLabels={ "Title": name, "Plot":plot, "TVShowTitle":TVShowTitle})
  60. liz.setProperty('fanart_image',fanart)
  61. ok=xbmcplugin.addDirectoryItem(handle=pluginhandle,url=u,listitem=liz,isFolder=True)
  62. return ok
  63.  
  64. def pageFragments(url):
  65. pageNum = int(url[-1])
  66. nextPage = pageNum + 1
  67. nurl = url.replace('page='+str(pageNum),'page='+str(nextPage))
  68. prevPage = pageNum - 1
  69. purl = url.replace('page='+str(pageNum),'page='+str(prevPage))
  70. if '/box' in nurl or '/box' in purl:
  71. nurl = nurl.replace('/box','')
  72. purl = purl.replace('/box','')
  73. data = getURL(nurl)
  74. if 'Your search returned zero results' not in data:
  75. addDir('Next Page ('+str(nextPage)+')',nurl,7)
  76. if prevPage >= 1:
  77. addDir('Previous Page ('+str(prevPage)+')',purl,7)
  78. LISTVIDEOS(url)
  79. xbmcplugin.endOfDirectory(pluginhandle,updateListing=True)
  80.  
  81. ################################ Root listing
  82. def ROOT():
  83. addDir('Full Episodes','full',5)
  84. addDir('Browse by Date','date',1)
  85. addDir('News Team','newsteam',2)
  86. addDir('Segments','segments',4)
  87. addDir('Guests','guests',3)
  88. xbmcplugin.endOfDirectory(pluginhandle)
  89.  
  90. def FULLEPISODES():
  91. xbmcplugin.setContent(pluginhandle, 'episodes')
  92. xbmcplugin.addSortMethod(pluginhandle, xbmcplugin.SORT_METHOD_NONE)
  93. full = 'http://www.thedailyshow.com/full-episodes/'
  94. data = getURL(full)
  95. weeks = re.compile('<a id="(.+?)" class="seaso.+?" href="#">(.+?)</a>').findall(data)
  96. for url, week in weeks:
  97. data = getURL(url)
  98. episodes=re.compile('<span class="date"><a href="(.+?)">(.+?)</a></span>').findall(data)
  99. thumbnails=re.compile("<img width='.+?' height='.+?' src='(.+?)'.+?/>").findall(data)
  100. descriptions=re.compile('<span class="description">(.+?)</span>').findall(data)
  101. airdates=re.compile('<span class="date">Aired: (.+?)</span>').findall(data)
  102. epNumbers=re.compile('<span class="id">Episode (.+?)</span>').findall(data)
  103. listings = []
  104. for link, name in episodes:
  105. listing = []
  106. listing.append(name)
  107. listing.append(link)
  108. listings.append(listing)
  109. for thumbnail in thumbnails:
  110. marker = thumbnails.index(thumbnail)
  111. listings[marker].append(thumbnail)
  112. for description in descriptions:
  113. marker = descriptions.index(description)
  114. listings[marker].append(description)
  115. for airdate in airdates:
  116. marker = airdates.index(airdate)
  117. listings[marker].append(airdate)
  118. for epNumber in epNumbers:
  119. marker = epNumbers.index(epNumber)
  120. listings[marker].append(epNumber)
  121. print listings
  122. for name, link, thumbnail, plot, date, seasonepisode in listings:
  123. mode = 10
  124. season = int(seasonepisode[:-3])
  125. episode = int(seasonepisode[-3:])
  126. u=sys.argv[0]+"?url="+urllib.quote_plus(link)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
  127. u += "&season="+urllib.quote_plus(str(season))
  128. u += "&episode="+urllib.quote_plus(str(episode))
  129. u += "&premiered="+urllib.quote_plus(date)
  130. u += "&plot="+urllib.quote_plus(plot)
  131. u += "&thumbnail="+urllib.quote_plus(thumbnail)
  132. liz=xbmcgui.ListItem(name, iconImage="DefaultFolder.png", thumbnailImage=thumbnail)
  133. liz.setInfo( type="Video", infoLabels={ "Title": name,
  134. "Plot":plot,
  135. "Season":season,
  136. "Episode": episode,
  137. "premiered":date,
  138. "TVShowTitle":TVShowTitle})
  139. liz.setProperty('IsPlayable', 'true')
  140. liz.setProperty('fanart_image',fanart)
  141. xbmcplugin.addDirectoryItem(handle=pluginhandle,url=u,listitem=liz)
  142. xbmcplugin.endOfDirectory(pluginhandle)
  143.  
  144.  
  145. def NEWS_TEAM():
  146. nurl= 'http://www.thedailyshow.com/news-team'
  147. data = getURL(nurl)
  148. people=re.compile('href="/news-team/(.+?)"').findall(data)
  149. names = []
  150. for name in people:
  151. name = name.replace('-',' ').title()
  152. if name not in names:
  153. names.append(name)
  154. for name in names:
  155. link = name.replace(' ','+')
  156. furl = 'http://www.thedailyshow.com/feeds/search?keywords=&tags='+link+'&sortOrder=desc&sortBy='+SORTORDER+'&page=1'
  157. addDir(name,furl,7)
  158. xbmcplugin.endOfDirectory(pluginhandle)
  159.  
  160.  
  161. def GUESTS():
  162. gurl = "http://www.thedailyshow.com/guests"
  163. data = getURL(gurl)
  164. cats=re.compile('<option value="(.+?)">(.+?)</option>').findall(data)
  165. for link,name in cats:
  166. furl = 'http://www.thedailyshow.com/feeds/search?guestCategory='+link+'&sortOrder=desc&sortBy='+SORTORDER+'&page=1'
  167. addDir(name,furl,7)
  168. xbmcplugin.endOfDirectory(pluginhandle)
  169.  
  170. def SEGMENTS():
  171. segments=[('Even Stevphen','Even+Stevphen'),
  172. ('This Week in God','This+Week+in+God'),
  173. ("Mess O'Potamia",'Mess+O%27Potamia'),
  174. ('Clusterf#@k to the Poor House','Clusterf%23%40k+to+the+Poor+House'),
  175. ('Moment of Zen','Moment+of+Zen'),
  176. ('Back in Black','Back+in+Black')
  177. ]
  178. for name, tag in segments:
  179. url = 'http://www.thedailyshow.com/feeds/search?keywords=&tags='+link+'&sortOrder=desc&sortBy='+SORTORDER+'&page=1'
  180. addDir(name,url,7)
  181. xbmcplugin.endOfDirectory(pluginhandle)
  182.  
  183.  
  184. ################################ Browse by Date
  185.  
  186. def YEARS():
  187. now = datetime.datetime.now()
  188. maxyear = int(now.year)+1
  189. for year in range(1999,maxyear):
  190. year = str(year)
  191. ycode = str(int(year)- 1996)
  192. addDir(year,ycode,11)
  193. xbmcplugin.endOfDirectory(pluginhandle)
  194.  
  195. def MONTHES(ycode):
  196. MONTHES = ['January','February','March','April','May','June','July','August','September','October','November','December']
  197. year = str(int(ycode) + 1996)
  198. mcode = '11'
  199. dcode = '31'
  200. url = DATELOOKUP+ycode+','+mcode+','+dcode
  201. items = demjson.decode(getURL(url))
  202. mticks = items['ticks']['month']
  203. for tick in mticks:
  204. mcode = mticks.index(tick)
  205. if tick['on'] == True:
  206. pname = MONTHES[mcode]+' '+year
  207. pcode = ycode+','+str(mcode)
  208. addDir(pname,pcode,12)
  209. xbmcplugin.endOfDirectory(pluginhandle)
  210.  
  211. def DATES(ymcode):
  212. dcode = '31'
  213. url = DATELOOKUP+ymcode+','+dcode
  214. items = demjson.decode(getURL(url))
  215. dticks = items['ticks']['day']
  216. namesplit = name.split(' ')
  217. month = namesplit[0]
  218. year = namesplit[1]
  219. for tick in dticks:
  220. if tick['on'] == True:
  221. d = dticks.index(tick)
  222. if d == 31:
  223. continue
  224. day = str(d+1)
  225. dcode = str(d)
  226. addDir((month+' '+day+', '+year),(ymcode+','+dcode),8)
  227. xbmcplugin.endOfDirectory(pluginhandle)
  228.  
  229. def LISTVIDEODATE(ymdcode):
  230. xbmcplugin.setContent(pluginhandle, 'episodes')
  231. url = DATELOOKUP+ymdcode
  232. items = demjson.decode(getURL(url))
  233. dataurl = items['feedURL']
  234. LISTVIDEOS(dataurl)
  235. xbmcplugin.endOfDirectory(pluginhandle)
  236.  
  237.  
  238. ################################ List Videos
  239.  
  240. def LISTVIDEOS(url):
  241. xbmcplugin.setContent(pluginhandle, 'episodes')
  242. data = getURL(url)
  243. playbackUrls=re.compile('<a href="http://www.thedailyshow.com/watch/(.+?)".+?>').findall(data)
  244. thumbnails=re.compile("<img width='.+?' height='.+?' src='(.+?)'").findall(data)
  245. names=re.compile('<span class="title"><a href=".+?">(.+?)</a></span>').findall(data)
  246. descriptions=re.compile('<span class="description">(.+?)\(.+?</span>').findall(data)
  247. durations=re.compile('<span class="description">.+?\((.+?)</span>').findall(data)
  248. epNumbers=re.compile('<span class="episode">Episode #(.+?)</span>').findall(data)
  249. airdates=re.compile('<span>Aired.+?</span>(.+?)</div>').findall(data)
  250. for pb in playbackUrls:
  251. url = "http://www.thedailyshow.com/watch/"+pb
  252. marker = playbackUrls.index(pb)
  253. thumbnail = thumbnails[marker]
  254. fname = names[marker]
  255. description = descriptions[marker]
  256. duration = durations[marker].replace(')','')
  257. try:
  258. seasonepisode = epNumbers[marker]
  259. season = int(seasonepisode[:-3])
  260. episode = int(seasonepisode[-3:])
  261. except:
  262. season = 0
  263. episode = 0
  264. date = airdates[marker]
  265. u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(13)+"&name="+urllib.quote_plus(fname)
  266. u += "&season="+urllib.quote_plus(str(season))
  267. u += "&episode="+urllib.quote_plus(str(episode))
  268. u += "&premiered="+urllib.quote_plus(date)
  269. u += "&plot="+urllib.quote_plus(plot)
  270. u += "&thumbnail="+urllib.quote_plus(thumbnail)
  271. liz=xbmcgui.ListItem(fname, iconImage="DefaultVideo.png", thumbnailImage=thumbnail)
  272. liz.setInfo( type="Video", infoLabels={ "Title": fname,
  273. "Episode":episode,
  274. "Season":season,
  275. "Plot":description,
  276. "premiered":date,
  277. "Duration":duration,
  278. "TVShowTitle":TVShowTitle})
  279. liz.setProperty('IsPlayable', 'true')
  280. liz.setProperty('fanart_image',fanart)
  281. xbmcplugin.addDirectoryItem(handle=pluginhandle,url=u,listitem=liz)
  282.  
  283.  
  284.  
  285. ################################ Play Video
  286.  
  287. def PLAYVIDEO(name,url):
  288. data = getURL(url)
  289. uri = re.compile('"http://media.mtvnservices.com/(.+?)"/>').findall(data)[0]
  290. rtmp = GRAB_RTMP(uri)
  291. item = xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=thumbnail, path=rtmp)
  292. item.setInfo( type="Video", infoLabels={ "Title": name,
  293. "Plot":plot,
  294. "premiered":premiered,
  295. "Season":int(season),
  296. "Episode":int(episode),
  297. "TVShowTitle":TVShowTitle})
  298. item.setProperty('fanart_image',fanart)
  299. xbmcplugin.setResolvedUrl(pluginhandle, True, item)
  300.  
  301. ################################ Play Full Episode
  302.  
  303. def PLAYFULLEPISODE(name,url):
  304. data = getURL(url)
  305. uri=re.compile('<param name="movie" value="http://media.mtvnservices.com/(.+?)"').findall(data)[0]
  306. #url = 'http://media.mtvnservices.com/player/config.jhtml?uri='+uri+'&group=entertainment&type=network&site=thedailyshow.com'
  307. url = 'http://shadow.comedycentral.com/feeds/video_player/mrss/?uri='+uri
  308. data = getURL(url)
  309. uris=re.compile('<guid isPermaLink="false">(.+?)</guid>').findall(data)
  310. stacked_url = 'stack://'
  311. for uri in uris:
  312. rtmp = GRAB_RTMP(uri)
  313. stacked_url += rtmp.replace(',',',,')+' , '
  314. stacked_url = stacked_url[:-3]
  315. item = xbmcgui.ListItem(name, iconImage="DefaultVideo.png", thumbnailImage=thumbnail, path=stacked_url)
  316. item.setInfo( type="Video", infoLabels={ "Title": name,
  317. "Plot":plot,
  318. "premiered":premiered,
  319. "Season":int(season),
  320. "Episode":int(episode),
  321. "TVShowTitle":TVShowTitle})
  322. item.setProperty('fanart_image',fanart)
  323. print stacked_url
  324. xbmcplugin.setResolvedUrl(pluginhandle, True, item)
  325.  
  326. ################################ Grab rtmp
  327.  
  328. def GRAB_RTMP(uri):
  329. swfurl = 'http://media.mtvnservices.com/player/release/?v=4.5.3'
  330. url = 'http://www.comedycentral.com/global/feeds/entertainment/media/mediaGenEntertainment.jhtml?uri='+uri+'&showTicker=true'
  331. data = getURL(url)
  332. widths = re.compile('width="(.+?)"').findall(data)
  333. heights = re.compile('height="(.+?)"').findall(data)
  334. bitrates = re.compile('bitrate="(.+?)"').findall(data)
  335. rtmps = re.compile('<src>rtmp(.+?)</src>').findall(data)
  336. mpixels = 0
  337. mbitrate = 0
  338. lbitrate = 0
  339. rate_setting = addon.getSetting("bitrate")
  340. if rate_setting == '0':
  341. lbitrate = 0
  342. elif rate_setting == '1':
  343. lbitrate = 1720
  344. elif rate_setting == '2':
  345. lbitrate = 1300
  346. elif rate_setting == '3':
  347. lbitrate = 960
  348. elif rate_setting == '4':
  349. lbitrate = 640
  350. elif rate_setting == '5':
  351. lbitrate = 450
  352. for rtmp in rtmps:
  353. marker = rtmps.index(rtmp)
  354. w = int(widths[marker])
  355. h = int(heights[marker])
  356. bitrate = int(bitrates[marker])
  357. if bitrate == 0:
  358. continue
  359. elif bitrate > lbitrate and lbitrate <> 0:
  360. continue
  361. elif lbitrate <= bitrate or lbitrate == 0:
  362. pixels = w * h
  363. if pixels > mpixels or bitrate > mbitrate:
  364. mpixels = pixels
  365. mbitrate = bitrate
  366. furl = 'rtmp'+ rtmp + " swfurl=" + swfurl + " swfvfy=true"
  367. #rtmpsplit = rtmp.split('/ondemand')
  368. #server = rtmpsplit[0]
  369. #path = rtmpsplit[1].replace('.flv','')
  370. #if '.mp4' in path:
  371. # path = 'mp4:' + path
  372. #port = ':1935'
  373. #app = '/ondemand?ovpfv=2.1.4'
  374. #furl = 'rtmp'+server+port+app+path+" playpath="+path+" swfurl="+swfurl+" swfvfy=true"
  375. return furl
  376.  
  377.  
  378. def get_params():
  379. param=[]
  380. paramstring=sys.argv[2]
  381. if len(paramstring)>=2:
  382. params=sys.argv[2]
  383. cleanedparams=params.replace('?','')
  384. if (params[len(params)-1]=='/'):
  385. params=params[0:len(params)-2]
  386. pairsofparams=cleanedparams.split('&')
  387. param={}
  388. for i in range(len(pairsofparams)):
  389. splitparams={}
  390. splitparams=pairsofparams[i].split('=')
  391. if (len(splitparams))==2:
  392. param[splitparams[0]]=splitparams[1]
  393.  
  394. return param
  395.  
  396.  
  397. params=get_params()
  398. url=None
  399. name=None
  400. mode=None
  401.  
  402. try:
  403. url=urllib.unquote_plus(params["url"])
  404. except:
  405. pass
  406. try:
  407. name=urllib.unquote_plus(params["name"])
  408. except:
  409. pass
  410. try:
  411. mode=int(params["mode"])
  412. except:
  413. pass
  414. try:
  415. thumbnail=urllib.unquote_plus(params["thumbnail"])
  416. except:
  417. thumbnail=''
  418. try:
  419. season=int(params["season"])
  420. except:
  421. season=0
  422. try:
  423. episode=int(params["episode"])
  424. except:
  425. episode=0
  426. try:
  427. premiered=urllib.unquote_plus(params["premiered"])
  428. except:
  429. premiered=''
  430. try:
  431. plot=urllib.unquote_plus(params["plot"])
  432. except:
  433. plot=''
  434.  
  435. print "Mode: "+str(mode)
  436. print "URL: "+str(url)
  437. print "Name: "+str(name)
  438.  
  439.  
  440. if mode==None or url==None or len(url)<1:
  441. ROOT()
  442. elif mode==1:
  443. YEARS()
  444. elif mode==11:
  445. MONTHES(url)
  446. elif mode==12:
  447. DATES(url)
  448. elif mode==2:
  449. NEWS_TEAM()
  450. elif mode==3:
  451. GUESTS()
  452. elif mode==4:
  453. SEGMENTS()
  454. elif mode==5:
  455. FULLEPISODES()
  456. elif mode==7:
  457. pageFragments(url)
  458. elif mode==8:
  459. LISTVIDEODATE(url)
  460. elif mode==9:
  461. LISTVIDEOS(url)
  462. elif mode==10:
  463. PLAYFULLEPISODE(name,url)
  464. elif mode==13:
  465. PLAYVIDEO(name,url)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement