PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/addons/plugin.video.ulitka.tv/resources/lib/addon.py

http://seppius-xbmc-repo.googlecode.com/
Python | 251 lines | 246 code | 3 blank | 2 comment | 0 complexity | 784bdb8555270ce4958a729c7b99a2b1 MD5 | raw file
Possible License(s): GPL-3.0, AGPL-1.0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import xbmc, xbmcgui, xbmcplugin, xbmcaddon
  4. import urllib2, urllib, os, xml.dom.minidom, cookielib, base64
  5. import socket, sys
  6. socket.setdefaulttimeout(15)
  7. h = int(sys.argv[1])
  8. icon = xbmc.translatePath(os.path.join(os.getcwd().replace(';', ''),'icon.jpg'))
  9. fanart = xbmc.translatePath(os.path.join(os.getcwd().replace(';', ''),'fanart.jpg'))
  10. xbmcplugin.setPluginFanart(h, fanart)
  11. __addon__ = xbmcaddon.Addon(id = 'plugin.video.ulitka.tv')
  12. addon_icon = __addon__.getAddonInfo('icon')
  13. addon_fanart = __addon__.getAddonInfo('fanart')
  14. addon_path = __addon__.getAddonInfo('path')
  15. addon_type = __addon__.getAddonInfo('type')
  16. addon_id = __addon__.getAddonInfo('id')
  17. addon_author = __addon__.getAddonInfo('author')
  18. addon_name = __addon__.getAddonInfo('name')
  19. addon_version = __addon__.getAddonInfo('version')
  20. addon_profile = __addon__.getAddonInfo('profile')
  21. icon = xbmc.translatePath(addon_icon)
  22. fanart = xbmc.translatePath(addon_fanart)
  23. profile = xbmc.translatePath(addon_profile)
  24. def showMessage(heading, message, times = 3000):
  25. xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s, "%s")'%(heading, message, times, icon))
  26. def GET(tu, post=None):
  27. try:
  28. username = __addon__.getSetting('username')
  29. password = __addon__.getSetting('password')
  30. if (not len(username)) and (not len(password)):
  31. __addon__.openSettings()
  32. return None
  33. CJ = cookielib.CookieJar()
  34. urllib2.install_opener(urllib2.build_opener(urllib2.HTTPCookieProcessor(CJ)))
  35. if post:
  36. data = urllib.urlencode(post)
  37. req = urllib2.Request(tu, data)
  38. else:
  39. req = urllib2.Request(tu)
  40. req.add_header('User-Agent', '%s/%s %s/%s/%s' % (addon_type, addon_id, addon_author, addon_version, urllib.quote_plus(addon_name)))
  41. #if post:
  42. #req.add_header('Content-Type', 'application/x-www-form-urlencoded')
  43. req.add_header('Authorization', 'Basic %s' % base64.b64encode('%s:%s' % (username, password)))
  44. cookie_path = os.path.join(profile, 'cookie')
  45. if not os.path.exists(cookie_path):
  46. os.makedirs(cookie_path)
  47. print '[%s]: os.makedirs(cookie_path=%s)' % (addon_id, cookie_path)
  48. cookie_send = {}
  49. for cookie_fname in os.listdir(cookie_path):
  50. cookie_file = os.path.join(cookie_path, cookie_fname)
  51. if os.path.isfile(cookie_file):
  52. cf = open(cookie_file, 'r')
  53. cookie_send[os.path.basename(cookie_file)] = cf.read()
  54. cf.close()
  55. else: print '[%s]: NOT os.path.isfile(cookie_file=%s)' % (addon_id, cookie_file)
  56. cookie_string = urllib.urlencode(cookie_send).replace('&','; ')
  57. req.add_header('Cookie', cookie_string)
  58. f = urllib2.urlopen(req)
  59. for Cook in CJ:
  60. cookie_file = os.path.join(cookie_path, Cook.name)
  61. cf = open(cookie_file, 'w')
  62. cf.write(Cook.value)
  63. cf.close()
  64. a = f.read()
  65. f.close()
  66. return a
  67. except Exception, e:
  68. print '[%s]: GET EXCEPTION: %s' % (addon_id, e)
  69. showMessage(tu, e, 5000)
  70. return None
  71. def getitems(params):
  72. http = GET(params['url'])
  73. if http == None: return False
  74. document = xml.dom.minidom.parseString(http)
  75. for item in document.getElementsByTagName('item'):
  76. info = {'title': None}
  77. img = None
  78. try:
  79. url = item.getElementsByTagName('url')[0].firstChild.data.encode('utf-8')
  80. except: url = None
  81. try: info['title'] = item.getElementsByTagName('title')[0].firstChild.data
  82. except:
  83. try: info['title'] = item.getElementsByTagName('label')[0].firstChild.data
  84. except: pass
  85. if info['title'] and url:
  86. IsFolder = False
  87. try: IsFolder = (int(item.getElementsByTagName(r'isFolder')[0].firstChild.data) == 1)
  88. except: pass
  89. try: img = item.getElementsByTagName('thumbnailImage')[0].firstChild.data
  90. except:
  91. try: img = item.getElementsByTagName('thumbnailImage')[0].firstChild.data
  92. except: pass
  93. try: info['plot'] = item.getElementsByTagName('plot')[0].firstChild.data
  94. except: pass
  95. try: info['plotoutline'] = item.getElementsByTagName('plotoutline')[0].firstChild.data
  96. except: pass
  97. try: info['duration'] = item.getElementsByTagName('duration')[0].firstChild.data
  98. except: pass
  99. try: info['tagline'] = item.getElementsByTagName('tagline')[0].firstChild.data
  100. except: pass
  101. try: info['genre'] = item.getElementsByTagName('genre')[0].firstChild.data
  102. except: pass
  103. try: info['tvshowtitle'] = item.getElementsByTagName('tvshowtitle')[0].firstChild.data
  104. except: pass
  105. try: info['season'] = int(item.getElementsByTagName('season')[0].firstChild.data)
  106. except: pass
  107. try: info['episode'] = int(item.getElementsByTagName('episode')[0].firstChild.data)
  108. except: pass
  109. try: vtype = item.getElementsByTagName('type')[0].firstChild.data
  110. except: vtype = 'video'
  111. li = xbmcgui.ListItem(info['title'], iconImage = img, thumbnailImage = img)
  112. if IsFolder:
  113. uri = '%s?func=getitems&url=%s' % (sys.argv[0], urllib.quote_plus(url))
  114. else:
  115. uri = '%s?func=play&url=%s' % (sys.argv[0], urllib.quote_plus(url))
  116. try: uri += '&sub=%s' % urllib.quote_plus(str(item.getElementsByTagName('sub')[0].firstChild.data.encode('utf-8')))
  117. except: pass
  118. li.setProperty('IsPlayable', 'true')
  119. li.setInfo(type = vtype, infoLabels = info)
  120. #li.setProperty('fanart_image', ifanart)
  121. xbmcplugin.addDirectoryItem(h, uri, li, IsFolder)
  122. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_UNSORTED)
  123. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_DATE)
  124. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_DURATION)
  125. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_GENRE)
  126. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_TITLE)
  127. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_VIDEO_RATING)
  128. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_VIDEO_RUNTIME)
  129. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_VIDEO_TITLE)
  130. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_VIDEO_YEAR)
  131. xbmcplugin.addSortMethod(h, xbmcplugin.SORT_METHOD_LABEL)
  132. xbmcplugin.endOfDirectory(h)
  133. def play(params):
  134. url = params['url']
  135. if not '?' in url:
  136. url += '?version=4.3.132&target=xbmc'
  137. i = xbmcgui.ListItem( path = url )
  138. xbmcplugin.setResolvedUrl(h, True, i)
  139. try:
  140. sub_url = params['sub']
  141. http = GET(sub_url)
  142. if http:
  143. suba = []
  144. x = 1
  145. document = xml.dom.minidom.parseString(http)
  146. for item in document.getElementsByTagName('p'):
  147. if item.getAttribute('begin') and item.getAttribute('end'):
  148. sub_begin = item.getAttribute('begin').encode('utf-8')
  149. sub_end = item.getAttribute('end').encode('utf-8')
  150. sub_data = item.firstChild.data.encode('utf-8')
  151. sub_data = sub_data.replace('<br />', '\n')
  152. suba.append(str(x))
  153. suba.append('%s --> %s' % (sub_begin, sub_end))
  154. suba.append(sub_data)
  155. suba.append('\n')
  156. x += 1
  157. sub_fdata = '\n'.join(suba)
  158. sub_url = sub_url.replace('.xml','').replace('.mp4','')
  159. sf = xbmc.translatePath('special://temp/%s.srt' % sub_url.split('/')[-1])
  160. subf = open(sf, 'w')
  161. subf.write(sub_fdata)
  162. subf.close()
  163. xbmc.sleep(2000)
  164. xbmc.Player().setSubtitles(sf)
  165. except: pass
  166. def get_params(paramstring):
  167. param=[]
  168. if len(paramstring)>=2:
  169. params=paramstring
  170. cleanedparams=params.replace('?','')
  171. if (params[len(params)-1]=='/'):
  172. params=params[0:len(params)-2]
  173. pairsofparams=cleanedparams.split('&')
  174. param={}
  175. for i in range(len(pairsofparams)):
  176. splitparams={}
  177. splitparams=pairsofparams[i].split('=')
  178. if (len(splitparams))==2:
  179. param[splitparams[0]]=splitparams[1]
  180. if len(param) > 0:
  181. for cur in param:
  182. param[cur] = urllib.unquote_plus(param[cur])
  183. return param
  184. def auth():
  185. cookie_path = os.path.join(profile, 'cookie')
  186. if os.path.exists(cookie_path):
  187. for cookie_fname in os.listdir(cookie_path):
  188. cookie_file = os.path.join(cookie_path, cookie_fname)
  189. try:
  190. os.remove(cookie_file)
  191. except:
  192. print '[%s]: GET EXCEPTION: %s' % (addon_id, e)
  193. showMessage(tu, e, 5000)
  194. token = GET('http://www.ulitka.tv/xbmc/token.php')
  195. #post form with auth
  196. username = __addon__.getSetting('username')
  197. password = __addon__.getSetting('password')
  198. post = {
  199. 'username': username,
  200. 'passwd': password,
  201. 'remeber': 'yes',
  202. 'option': 'com_user',
  203. 'task': 'login',
  204. 'return': 'L3hibWMvaW5kZXgueG1s',
  205. token: '1',
  206. }
  207. GET('http://www.ulitka.tv/component/user/', post)
  208. def addon_main():
  209. auth()
  210. params = get_params(sys.argv[2])
  211. try:
  212. func = params['func']
  213. except:
  214. getitems({'url':'http://www.ulitka.tv/xbmc/index.xml'})
  215. func = None
  216. if func != None:
  217. try: pfunc = globals()[func]
  218. except:
  219. pfunc = None
  220. print '[%s]: Function "%s" not found' % (addon_id, func)
  221. showMessage('Internal addon error', 'Function "%s" not found' % func, 2000)
  222. if pfunc: pfunc(params)