PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/apps/tv/modules/hulu/hulu.py

https://github.com/barttenbrinke/Bartsidee-Repository
Python | 124 lines | 120 code | 4 blank | 0 comment | 1 complexity | e9d80363e58c63afb8b5b9ca2aa90396 MD5 | raw file
  1. import mc, re, os, sys
  2. sys.path.append(os.path.join(mc.GetApp().GetAppDir(), 'libs'))
  3. import ba, md5, time, base64
  4. from beautifulsoup.BeautifulSoup import BeautifulSoup
  5. from urllib import quote_plus
  6. class Module(object):
  7. def __init__(self):
  8. self.name = "Hulu" #Name of the channel
  9. self.type = ['search'] #Choose between 'search', 'list', 'genre'
  10. self.episode = True #True if the list has episodes
  11. self.filter = [] #Option to set a filter to the list
  12. self.genre = [] #Array to add a genres to the genre section [type genre must be enabled]
  13. self.content_type = 'video/x-flv' #Mime type of the content to be played
  14. self.country = 'US' #2 character country id code
  15. self.url_base = 'http://www.hulu.com'
  16. def Search(self, search):
  17. url = self.url_base + '/browse/search?alphabet=All&family_friendly=0&closed_captioned=0&has_free=1&has_huluplus=0&has_hd=0&channel=All&subchannel=&network=All&display=Shows%20with%20full%20episodes%20only&decade=All&type=tv&view_as_thumbnail=false&block_num=0&keyword=' + quote_plus(search)
  18. data = ba.FetchUrl(url)
  19. data = re.compile('"show_list", "(.*?)"\)', re.DOTALL + re.IGNORECASE).search(str(data)).group(1)
  20. data = data.replace('\\u003c','<').replace('\\u003e','>').replace('\\','').replace('\\n','').replace('\\t','')
  21. soup = BeautifulSoup(data, convertEntities="xml", smartQuotesTo="xml")
  22. streamlist = list()
  23. for info in soup.findAll('a', {'onclick':True}):
  24. stream = ba.CreateStream()
  25. stream.SetName(info.contents[0])
  26. stream.SetId(info['href'])
  27. streamlist.append(stream)
  28. return streamlist
  29. def Episode(self, stream_name, stream_id, page, totalpage):
  30. data = ba.FetchUrl(stream_id, 3600)
  31. if data == "":
  32. mc.ShowDialogNotification("No episode found for " + str(stream_name))
  33. return ba.CreateEpisode()
  34. soup = BeautifulSoup(data, convertEntities="xml", smartQuotesTo="xml")
  35. totalpage = len(soup.findAll('tr', 'srh'))
  36. try:
  37. episode_url = re.compile('VideoExpander.subheadingClicked\((.*?)\)"', re.DOTALL + re.IGNORECASE).search(str(data)).group(1)
  38. except:
  39. mc.ShowDialogNotification("No episode found for " + str(stream_name))
  40. return ba.CreateEpisode()
  41. season_number = re.compile('season_number=(.*?)\&', re.DOTALL + re.IGNORECASE).search(str(episode_url)).group(1)
  42. show_id = re.compile('show_id=(.*?)\&', re.DOTALL + re.IGNORECASE).search(str(episode_url)).group(1)
  43. pp = []
  44. for i in range(0,totalpage):
  45. pp.append(str(int(season_number) - i))
  46. intpage = int(page) - 1
  47. url = "http://www.hulu.com/videos/season_expander?order=desc&page=1&season_number=" + str(pp[intpage]) + "&show_id=" + str(show_id) + "&sort=season&video_type=episode"
  48. data = ba.FetchUrl(url)
  49. data = re.compile('srh-bottom-' + pp[intpage] +'", "(.*?)"\);', re.DOTALL + re.IGNORECASE).search(str(data)).group(1)
  50. data = data.replace('\\u003c','<').replace('\\u003e','>').replace('\\','')
  51. soup = BeautifulSoup(data)
  52. episodelist = list()
  53. name = []
  54. link = []
  55. number = []
  56. thumb = []
  57. for tmp in soup.findAll('td', {'class':'c0'}):
  58. number.append(tmp.contents[0])
  59. i = 0
  60. b = 0
  61. for tmp in soup.findAll('td', {'class':'c1'}):
  62. name.append(tmp.a.contents[0])
  63. link.append(tmp.a['href'])
  64. try:
  65. thumb.append(self.GetThumb(re.compile('/watch/(.*?)/', re.DOTALL + re.IGNORECASE).search(str(tmp.a['href'])).group(1)))
  66. except:
  67. thumb.append('')
  68. b += 1
  69. if len(tmp.findAll('div', 'vex-h')) == 0:
  70. i += 1
  71. if i != b: totalpage = page
  72. for x in range(0,i):
  73. episode = ba.CreateEpisode()
  74. episode.SetName(stream_name)
  75. episode.SetId(link[x])
  76. episode.SetDescription('Episode: ' + number[x] + ' - ' + name[x])
  77. episode.SetThumbnails(thumb[x])
  78. episode.SetDate('Season: ' + pp[intpage])
  79. episode.SetPage(page)
  80. episode.SetTotalpage(totalpage)
  81. episodelist.append(episode)
  82. return episodelist
  83. def Play(self, stream_name, stream_id, subtitle):
  84. path = self.tinyurl(stream_id)
  85. play = ba.CreatePlay()
  86. play.SetPath(quote_plus(path))
  87. play.SetDomain('bartsidee.nl')
  88. play.SetJSactions(quote_plus('http://bartsidee.nl/boxee/apps/js/hulu.js'))
  89. return play
  90. def tinyurl(self, params):
  91. url = "http://tinyurl.com/api-create.php?url=" + str(params)
  92. return ba.FetchUrl(url)
  93. def GetThumb(self, id):
  94. url = "http://www.hulu.com/videos/info/" + str(id)
  95. data = ba.FetchUrl(url,0,True)
  96. try:
  97. return re.compile('"thumbnail_url":"(.*?)"', re.DOTALL + re.IGNORECASE).search(str(data)).group(1)
  98. except:
  99. try:
  100. return re.compile('"thumbnail_url": "(.*?)"', re.DOTALL + re.IGNORECASE).search(str(data)).group(1)
  101. except:
  102. return str('')