PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/addons/plugin.video.tv-here.ru/default.py

http://seppius-xbmc-repo.googlecode.com/
Python | 212 lines | 188 code | 4 blank | 20 comment | 0 complexity | 98f6a6042f83fb062617f57d2bad3e41 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. #!/usr/bin/python
  2. #/*
  3. # * Copyright (c) 2011-2012 XBMC-Russia, HD-lab Team, E-mail: dev@hd-lab.ru
  4. # * Writer (c) 2011, Kostynoy S.A., E-mail: seppius2@gmail.com
  5. # *
  6. # * This Program is free software; you can redistribute it and/or modify
  7. # * it under the terms of the GNU General Public License as published by
  8. # * the Free Software Foundation; either version 2, or (at your option)
  9. # * any later version.
  10. # *
  11. # * This Program is distributed in the hope that it will be useful,
  12. # * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # * GNU General Public License for more details.
  15. # *
  16. # * You should have received a copy of the GNU General Public License
  17. # * along with this program; see the file COPYING. If not, write to
  18. # * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19. # * http://www.gnu.org/copyleft/gpl.html
  20. # */
  21. import urllib2, re, string, xbmc, xbmcgui, xbmcplugin, os, urllib, cookielib
  22. handle = int(sys.argv[1])
  23. PLUGIN_NAME = 'TV-HERE.RU'
  24. SITE_HOSTNAME = 'tv-here.ru'
  25. SITEPREF = 'http://%s' % SITE_HOSTNAME
  26. SITE_URL = SITEPREF + '/'
  27. phpsessid_file = os.path.join(xbmc.translatePath('special://temp/'), 'plugin_video_tvhere.sess')
  28. thumb = os.path.join( os.getcwd(), "icon.png" )
  29. def clean(name):
  30. remove=[(' ', ''), ('—', '-'), ('…', '.'), ('–', '-'), ('«', '"'), ('“', '"'), ('“', '"'), ('»', '"'), ('"', '"')]
  31. for trash, crap in remove:
  32. name=name.replace(trash, crap)
  33. return name
  34. def Get(url, ref=None):
  35. cj = cookielib.CookieJar()
  36. h = urllib2.HTTPCookieProcessor(cj)
  37. opener = urllib2.build_opener(h)
  38. urllib2.install_opener(opener)
  39. post = None
  40. request = urllib2.Request(url, post)
  41. request.add_header('User-Agent', 'Opera/10.60 (X11; openSUSE 11.3/Linux i686; U; ru) Presto/2.6.30 Version/10.60')
  42. request.add_header('Host', SITE_HOSTNAME)
  43. request.add_header('Accept', 'text/html, application/xml, application/xhtml+xml, */*')
  44. request.add_header('Accept-Language', 'ru,en;q=0.9')
  45. if ref != None:
  46. request.add_header('Referer', ref)
  47. if os.path.isfile(phpsessid_file):
  48. fh = open(phpsessid_file, 'r')
  49. phpsessid = fh.read()
  50. fh.close()
  51. request.add_header('Cookie', 'PHPSESSID=' + phpsessid)
  52. o = urllib2.urlopen(request)
  53. for index, cookie in enumerate(cj):
  54. cookraw = re.compile('<Cookie PHPSESSID=(.*?) for.*/>').findall(str(cookie))
  55. if len(cookraw) > 0:
  56. fh = open(phpsessid_file, 'w')
  57. fh.write(cookraw[0])
  58. fh.close()
  59. http = o.read()
  60. o.close()
  61. return http
  62. def get_params():
  63. param=[]
  64. paramstring=sys.argv[2]
  65. if len(paramstring)>=2:
  66. params=sys.argv[2]
  67. cleanedparams=params.replace('?','')
  68. if (params[len(params)-1]=='/'):
  69. params=params[0:len(params)-2]
  70. pairsofparams=cleanedparams.split('&')
  71. param={}
  72. for i in range(len(pairsofparams)):
  73. splitparams={}
  74. splitparams=pairsofparams[i].split('=')
  75. if (len(splitparams))==2:
  76. param[splitparams[0]]=splitparams[1]
  77. return param
  78. def CheckHttp(data):
  79. if (data.find('http://') == -1):
  80. data = SITEPREF + data
  81. return data
  82. def ShowRoot(url):
  83. http = Get(url)
  84. if http == None:
  85. xbmc.output('[%s] ShowRoot() Error 1: http == None URL=%s' % (PLUGIN_NAME, url))
  86. return
  87. raw1 = re.compile('<div class="menuheader expandable">(.+?)</div>(.*?)\s*</div>\s', re.DOTALL).findall(http)
  88. if len(raw1) == 0:
  89. xbmc.output('[%s] ShowRoot() Error 2: http == None URL=%s' % (PLUGIN_NAME, url))
  90. return
  91. for raw_block in raw1:
  92. block_name = raw_block[0]
  93. raw2 = re.compile('<a href="(.*?)" title="(.*?)">(.*?)</a>').findall(raw_block[1])
  94. if len(raw2) != 0:
  95. for row_url, row_title, row_name in raw2:
  96. row_url = CheckHttp(row_url)
  97. Title = block_name + ' : ' + row_title
  98. listitem = xbmcgui.ListItem(Title)
  99. listitem.setInfo(type = "Video", infoLabels = {"Title": Title} )
  100. if (row_url.find('.html') == -1):
  101. purl = sys.argv[0] + '?mode=OpenCat'\
  102. + '&url=' + urllib.quote_plus(row_url)\
  103. + '&title=' + urllib.quote_plus(Title)
  104. xbmcplugin.addDirectoryItem(handle, purl, listitem, True)
  105. else:
  106. purl = sys.argv[0] + '?mode=OpenPage'\
  107. + '&url=' + urllib.quote_plus(row_url)\
  108. + '&title=' + urllib.quote_plus(Title)
  109. xbmcplugin.addDirectoryItem(handle, purl, listitem, False)
  110. else:
  111. xbmc.output('[%s] ShowRoot() Error 3:len(raw2) == 0 raw_block[1]=%s' % (PLUGIN_NAME, raw_block[1]))
  112. def OpenCat(url, name):
  113. xbmc.output('def OpenCat(%s, %s):' % (url, name))
  114. http = Get(url)
  115. if http == None:
  116. xbmc.output('[%s] OpenCat() Error 1: http == None URL=%s' % (PLUGIN_NAME, url))
  117. return
  118. http = http.replace('</tr><tr>', '\n')
  119. raw1 = re.compile('<img src=\"(.*?)\".*>(.*?)</td>.*<a href=\"(.*?)\".*title=\"(.*?)\">(.*?)</a>(.*?)</b></td>').findall(http)
  120. if len(raw1) == 0:
  121. xbmc.output('[%s] OpenCat() Error 2: len(raw1) == 0 URL=%s' % (PLUGIN_NAME, url))
  122. xbmc.output(http)
  123. return
  124. for row_img, row_blob, row_url, row_des, row_name, row_plot in raw1:
  125. row_url = CheckHttp(row_url)
  126. Title = name + ' : ' + row_name
  127. Plot = clean(row_plot)
  128. Genre = clean(row_des)
  129. listitem = xbmcgui.ListItem(row_name)
  130. listitem.setInfo(type = "Video", infoLabels = {
  131. "Title": row_name,
  132. "Studio": row_url,
  133. "Plot": Plot,
  134. "Genre": Genre } )
  135. purl = sys.argv[0] + '?mode=OpenPage'\
  136. + '&url=' + urllib.quote_plus(row_url)\
  137. + '&title=' + urllib.quote_plus(Title)
  138. xbmcplugin.addDirectoryItem(handle, purl, listitem, False)
  139. def OpenPage(url, name):
  140. xbmc.output('def OpenPage(%s, %s):' % (url, name))
  141. http = Get(url)
  142. if http == None:
  143. xbmc.output('[%s] OpenPage() Error 1: http == None URL=%s' % (PLUGIN_NAME, url))
  144. return
  145. raw1 = re.compile('<script language=\"JavaScript\">(.*?)\(\"(.*?)\"\);</script>').findall(http)
  146. if len(raw1) == 0:
  147. xbmc.output('[%s] OpenPage() Error 2: len(raw1) == 0 URL=%s' % (PLUGIN_NAME, url))
  148. xbmc.output(http)
  149. return
  150. play_link = raw1[0][1]
  151. if play_link[0] == '/':
  152. play_link = CheckHttp(play_link)
  153. if (raw1[0][0] == 'fr'):
  154. play_link = re.compile('__streamurl__=(.*?)\"').findall(raw1[0][1])
  155. xbmc.output('*** Play_MODE = %s' % raw1[0][0])
  156. xbmc.output('*** Play_LINK = %s' % play_link)
  157. item = xbmcgui.ListItem(name, iconImage = thumb, thumbnailImage = thumb)
  158. item.setInfo(type="Video", infoLabels={"Title": name})
  159. xbmc.Player(xbmc.PLAYER_CORE_DVDPLAYER).play(play_link, item)
  160. params = get_params()
  161. mode = None
  162. url = ''
  163. title = ''
  164. ref = ''
  165. img = ''
  166. try:
  167. mode = urllib.unquote_plus(params["mode"])
  168. except:
  169. pass
  170. try:
  171. url = urllib.unquote_plus(params["url"])
  172. except:
  173. pass
  174. try:
  175. title = urllib.unquote_plus(params["title"])
  176. except:
  177. pass
  178. try:
  179. img = urllib.unquote_plus(params["img"])
  180. except:
  181. pass
  182. if mode == None:
  183. ShowRoot(SITE_URL)
  184. xbmcplugin.setPluginCategory(handle, PLUGIN_NAME)
  185. xbmcplugin.endOfDirectory(handle)
  186. elif mode == 'OpenCat':
  187. OpenCat(url, title)
  188. xbmcplugin.setPluginCategory(handle, PLUGIN_NAME)
  189. xbmcplugin.endOfDirectory(handle)
  190. elif mode == 'OpenPage':
  191. OpenPage(url, title)