PageRenderTime 61ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/IPTVPlayer/hosts/hostiplex.py

https://gitlab.com/darezik/iptvplayer-for-e2
Python | 296 lines | 266 code | 12 blank | 18 comment | 3 complexity | 807073ce60385d56b100d27f39946499 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # Based on information provided by @jatrn: http://sd-xbmc.org/pl/content/iplexpl-zrodlo-apki-z-samsunga-smart-tv
  3. ###################################################
  4. # LOCAL import
  5. ###################################################
  6. from Plugins.Extensions.IPTVPlayer.components.iptvplayerinit import TranslateTXT as _
  7. from Plugins.Extensions.IPTVPlayer.components.ihost import CHostBase, CBaseHostClass, CDisplayListItem, ArticleContent, RetHost, CUrlItem
  8. from Plugins.Extensions.IPTVPlayer.tools.iptvtools import CSelOneLink, printDBG, printExc, CSearchHistoryHelper, GetLogoDir, GetCookieDir
  9. from Plugins.Extensions.IPTVPlayer.libs.youtube_dl.utils import clean_html
  10. from Plugins.Extensions.IPTVPlayer.libs.crypto.cipher import blowfish
  11. ###################################################
  12. ###################################################
  13. # FOREIGN import
  14. ###################################################
  15. from Components.config import config, ConfigSelection, ConfigYesNo, ConfigText, getConfigListEntry
  16. from datetime import timedelta
  17. import re
  18. import urllib
  19. import time
  20. import binascii
  21. import base64
  22. import codecs
  23. try: import simplejson as json
  24. except: import json
  25. from os import urandom as os_urandom
  26. try:
  27. from hashlib import sha1
  28. except ImportError:
  29. import sha
  30. sha1 = sha.new
  31. ###################################################
  32. ###################################################
  33. # E2 GUI COMMPONENTS
  34. ###################################################
  35. ###################################################
  36. ###################################################
  37. # Config options for HOST
  38. ###################################################
  39. config.plugins.iptvplayer.iplex_proxy = ConfigYesNo(default = False)
  40. def GetConfigList():
  41. optionList = []
  42. optionList.append(getConfigListEntry("Iplex korzystaj z proxy?", config.plugins.iptvplayer.iplex_proxy))
  43. return optionList
  44. ###################################################
  45. def gettytul():
  46. return 'Iplex'
  47. class Iplex(CBaseHostClass):
  48. USER_AGENT = 'Mozilla/5.0 (SmartHub; SMART-TV; U; Linux/SmartTV; Maple2012) AppleWebKit/534.7 (KHTML, like Gecko) SmartTV Safari/534.7'
  49. def __init__(self):
  50. printDBG("Iplex.__init__")
  51. CBaseHostClass.__init__(self, {'proxyURL': config.plugins.iptvplayer.proxyurl.value, 'useProxy': config.plugins.iptvplayer.iplex_proxy.value})
  52. self.cm.HEADER = {'User-Agent': Iplex.USER_AGENT, 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'}
  53. def _getJItemStr(self, item, key, default=''):
  54. v = item.get(key, None)
  55. if None == v:
  56. return default
  57. return clean_html(u'%s' % v).encode('utf-8')
  58. def _getJItemNum(self, item, key, default=0):
  59. v = item.get(key, None)
  60. if None != v:
  61. try:
  62. NumberTypes = (int, long, float, complex)
  63. except NameError:
  64. NumberTypes = (int, long, float)
  65. if isinstance(v, NumberTypes):
  66. return v
  67. return default
  68. def _decodeUrl(self, url, key='-S75dbb-QB?<nE_['):
  69. '''
  70. Author: http://sd-xbmc.org/, @jatrn
  71. '''
  72. ret = ''
  73. #-------- decoding -----------
  74. match = re.search('(http:\/\/.*)\/(\d{1,5}|pre_adv|post_adv)\/(.*)\.mp4', url)
  75. #check if valid url
  76. if match:
  77. url_path = match.group(1) + '/' + match.group(2) + '/'
  78. s1 = codecs.encode(match.group(3), 'rot_13')
  79. s2 = base64.b64decode(s1)
  80. s3 = ''
  81. cipher = blowfish.Blowfish(key)
  82. for index in range(0, len(s2)/16):
  83. chunk = s2[index*16:index*16+16]
  84. s3 += cipher.decrypt(chunk.decode("hex"))
  85. s4 = s3.replace("$","")
  86. ret = url_path + s4 + '.mp4'
  87. return ret
  88. def listsCategories(self, url):
  89. printDBG("Iplex.listsCategories url[%s]" % url)
  90. sts, data = self.cm.getPage(url)
  91. if not sts:
  92. printExc()
  93. return
  94. try:
  95. data = json.loads(data)
  96. if 'series' in data:
  97. sts, data = self.cm.getPage(data['series'])
  98. if not sts:
  99. printExc()
  100. return
  101. data = json.loads(data)
  102. for item in data['feeds']:
  103. if item.get('license', '') in ['FREE', '']: #'PAID'
  104. type = self._getJItemStr(item, 'type')
  105. if 'account' == type: continue
  106. url = self._getJItemStr(item, 'url')
  107. title = self._getJItemStr(item, 'caption')
  108. if '' == title: title = self._getJItemStr(item, 'title')
  109. icon = self._getJItemStr(item, 'img')
  110. desc = ''
  111. duration = self._getJItemNum(item, 'duration', -1)
  112. rating = self._getJItemStr(item, 'rating', 'brak oceny')
  113. if 0 < duration: desc += str(timedelta(minutes=duration)) + '|'
  114. if 'brak oceny' != rating: desc += rating + '|'
  115. params = { 'url' : url,
  116. 'title' : title,
  117. 'desc' : desc,
  118. 'icon' : icon,
  119. 'category' : type,
  120. }
  121. if 'movie_card' == type:
  122. self.addVideo(params)
  123. else:
  124. self.addDir(params)
  125. except:
  126. printExc()
  127. def resolveLink(self, url):
  128. printDBG("Iplex.resolveLink url[%s]" % url)
  129. sts, data = self.cm.getPage(url)
  130. if not sts:
  131. return
  132. try:
  133. data = json.loads(data)
  134. url = self._decodeUrl(self._getJItemStr(data['movie'], 'url'))
  135. if type(url) == type(u''):
  136. url = url.encode('utf-8')
  137. return url
  138. except:
  139. printExc()
  140. return ''
  141. def getLinks(self, url):
  142. printDBG("Iplex.getLinks url[%r]" % url )
  143. videoUrls = []
  144. sts, data = self.cm.getPage(url)
  145. if sts:
  146. try:
  147. data = json.loads(data)
  148. printDBG(data)
  149. VER_TABLE = [{'type':'lector', 'name':'Lektor'}, {'type':'subtitles', 'name':'Napisy'}]
  150. for item in VER_TABLE:
  151. if item['type'] in data:
  152. videoUrls.append({'name': item['name'], 'url':self._getJItemStr(data, item['type'])})
  153. except:
  154. printExc()
  155. return videoUrls
  156. def getDescription(self, url):
  157. printDBG("Iplex.getDescription url[%r]" % url )
  158. content = {}
  159. sts, data = self.cm.getPage(url)
  160. if sts:
  161. try:
  162. data = json.loads(data)
  163. printDBG(data)
  164. content = { 'title': self._getJItemStr(data, 'title'),
  165. 'desc' : self._getJItemStr(data, 'description'),
  166. 'icon' : self._getJItemStr(data, 'img'),
  167. }
  168. if 10 < len(content['desc']):
  169. return content
  170. except:
  171. printExc()
  172. return content
  173. def handleService(self, index, refresh = 0, searchPattern = '', searchType = ''):
  174. printDBG('Iplex..handleService start')
  175. CBaseHostClass.handleService(self, index, refresh, searchPattern, searchType)
  176. name = self.currItem.get("name", '')
  177. category = self.currItem.get("category", '')
  178. url = self.currItem.get("url", 'http://samsung.iplex.pl/tv/main.menu?api=v4')
  179. printDBG( "Iplex.handleService: ---------> name[%s], category[%s] " % (name, category) )
  180. self.currList = []
  181. self.listsCategories(url)
  182. class IPTVHost(CHostBase):
  183. def __init__(self):
  184. CHostBase.__init__(self, Iplex(), False)
  185. def getLogoPath(self):
  186. return RetHost(RetHost.OK, value = [GetLogoDir('iplexlogo.png')])
  187. def getArticleContent(self, Index = 0):
  188. listLen = len(self.host.currList)
  189. if listLen < Index and listLen > 0:
  190. printDBG( "ERROR getArticleContent - current list is to short len: %d, Index: %d" % (listLen, Index) )
  191. return RetHost(RetHost.ERROR, value = [])
  192. content = self.host.getDescription(self.host.currList[Index]['url'])
  193. title = content.get('title', '')
  194. text = content.get('desc' '')
  195. images = [ {'title':'', 'author': '', 'url': content.get('icon', '')} ]
  196. return RetHost(RetHost.OK, value = [ArticleContent(title = title, text = text, images = images)])
  197. def getLinksForVideo(self, Index = 0, selItem = None):
  198. listLen = len(self.host.currList)
  199. if listLen < Index and listLen > 0:
  200. printDBG( "ERROR getLinksForVideo - current list is to short len: %d, Index: %d" % (listLen, Index) )
  201. return RetHost(RetHost.ERROR, value = [])
  202. if self.host.currList[Index]["type"] != 'video':
  203. printDBG( "ERROR getLinksForVideo - current item has wrong type" )
  204. return RetHost(RetHost.ERROR, value = [])
  205. retlist = []
  206. urlList = self.host.getLinks(self.host.currList[Index]['url'])
  207. for item in urlList:
  208. need_resolve = 1
  209. retlist.append(CUrlItem(item["name"], item["url"], need_resolve))
  210. return RetHost(RetHost.OK, value = retlist)
  211. # end getLinksForVideo
  212. def getResolvedURL(self, url):
  213. # resolve url to get direct url to video file
  214. url = self.host.resolveLink(url)
  215. urlTab = []
  216. if isinstance(url, basestring) and url.startswith('http'):
  217. urlTab.append(url)
  218. return RetHost(RetHost.OK, value = urlTab)
  219. def convertList(self, cList):
  220. hostList = []
  221. searchTypesOptions = []
  222. for cItem in cList:
  223. hostLinks = []
  224. type = CDisplayListItem.TYPE_UNKNOWN
  225. possibleTypesOfSearch = None
  226. if cItem['type'] == 'category':
  227. if cItem['title'] == 'Wyszukaj':
  228. type = CDisplayListItem.TYPE_SEARCH
  229. possibleTypesOfSearch = searchTypesOptions
  230. else:
  231. type = CDisplayListItem.TYPE_CATEGORY
  232. elif cItem['type'] == 'video':
  233. type = CDisplayListItem.TYPE_VIDEO
  234. url = cItem.get('url', '')
  235. if '' != url:
  236. hostLinks.append(CUrlItem("Link", url, 1))
  237. title = cItem.get('title', '')
  238. description = clean_html(cItem.get('desc', ''))
  239. icon = cItem.get('icon', '')
  240. hostItem = CDisplayListItem(name = title,
  241. description = description,
  242. type = type,
  243. urlItems = hostLinks,
  244. urlSeparateRequest = 1,
  245. iconimage = icon,
  246. possibleTypesOfSearch = possibleTypesOfSearch)
  247. hostList.append(hostItem)
  248. return hostList
  249. # end convertList