PageRenderTime 41ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/IPTVPlayer/hosts/hostkabarety.py

https://gitlab.com/zatoshi/iptvplayer-for-e2
Python | 345 lines | 318 code | 14 blank | 13 comment | 4 complexity | ae187ca7630e050af50ed70c286d8287 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. # based on (root)/trunk/xbmc-addons/src/plugin.video.polishtv.live/ @ 635 - Wersja 668
  3. ###################################################
  4. # LOCAL import
  5. ###################################################
  6. from Plugins.Extensions.IPTVPlayer.components.ihost import IHost, CDisplayListItem, RetHost, CUrlItem
  7. import Plugins.Extensions.IPTVPlayer.libs.pCommon as pCommon
  8. from Plugins.Extensions.IPTVPlayer.libs.youtubeparser import YouTubeParser
  9. from Plugins.Extensions.IPTVPlayer.tools.iptvtools import printDBG, CSelOneLink, GetLogoDir
  10. ###################################################
  11. # FOREIGN import
  12. ###################################################
  13. import re
  14. from Components.config import config
  15. ###################################################
  16. ###################################################
  17. # Config options for HOST
  18. ###################################################
  19. def GetConfigList():
  20. return []
  21. ###################################################
  22. def gettytul():
  23. return 'Kabarety Odpoczywam'
  24. class Kabarety:
  25. MAINURL = 'http://www.kabarety.odpoczywam.net/'
  26. IMGURL = 'http://i.ytimg.com/vi/'
  27. NAJ_LINK = MAINURL + '/bestof/page:'
  28. NOW_LINK = MAINURL + '/index/page:'
  29. SERVICE_MENU_TABLE = {
  30. 1: "Najnowsze",
  31. 2: "Najlepsze",
  32. 3: "Kategorie",
  33. 4: "Wyszukaj",
  34. }
  35. def __init__(self):
  36. self.cm = pCommon.common()
  37. self.currList = []
  38. self.ytp = YouTubeParser()
  39. self.ytformats = config.plugins.iptvplayer.ytformat.value
  40. def _getFullUrl(self, url, baseUrl=None):
  41. if None == baseUrl: baseUrl = Kabarety.MAINURL
  42. if 0 < len(url) and not url.startswith('http'):
  43. url = baseUrl + url
  44. return url
  45. def getCurrList(self):
  46. return self.currList
  47. def setCurrList(self, list):
  48. self.currList = list
  49. return
  50. def setTable(self):
  51. return self.SERVICE_MENU_TABLE
  52. def listsMainMenu(self, table):
  53. printDBG("Kabarety.listsMainMenu")
  54. self.currList = []
  55. for num, val in table.items():
  56. item = {'type': 'dir', 'name': 'main-menu', 'category': val, 'title': val, 'icon': ''}
  57. self.currList.append(item)
  58. def getCategories(self, url):
  59. printDBG("Kabarety.getCategories")
  60. self.currList = []
  61. query_data = { 'url': url, 'use_host': False, 'use_cookie': False, 'use_post': False, 'return_data': True }
  62. try:
  63. data = self.cm.getURLRequestData(query_data)
  64. except:
  65. printDBG("Kabarety.getCategories exception")
  66. return
  67. match = re.compile('<b>Kategorie</a><br><br>(.+?)<br><br>', re.DOTALL).findall(data)
  68. if len(match) > 0:
  69. match2 = re.compile('href="(.+?)">(.+?)</a>').findall(match[0])
  70. if len(match2) > 0:
  71. for i in range(len(match2)):
  72. title = self.cm.html_entity_decode(match2[i][1])
  73. item = {'type': 'dir', 'name': 'category', 'title': title, 'category': match2[i][0], 'icon': ''}
  74. self.currList.append(item)
  75. def getFilmTab(self, url, page):
  76. printDBG("Kabarety.getFilmTab")
  77. self.currList = []
  78. query_data = { 'url': self._getFullUrl(url)+page, 'use_host': False, 'use_cookie': False, 'use_post': False, 'return_data': True }
  79. try:
  80. data = self.cm.getURLRequestData(query_data)
  81. except:
  82. printDBG("Kabarety.getFilmTab exception")
  83. return
  84. link = data.replace('\n', '') #MOD
  85. match = re.compile('<a class="video-mini" title="(.+?)" href=".+?">.+?<span class="duration".+?<img class="video-mini-img".+?src="http://i.ytimg.com/vi/(.+?)/0.jpg" />').findall(link)
  86. if len(match) > 0:
  87. for i in range(len(match)):
  88. title = self.cm.html_entity_decode(match[i][0])
  89. img = self.IMGURL + match[i][1] + '/0.jpg'
  90. item = {'type': 'video', 'title': title, 'page': match[i][1], 'icon': img}
  91. self.currList.append(item)
  92. match = re.compile('<span><a href=".+?" class="next shadow-main">&raquo;</a></span>').findall(data)
  93. if len(match) > 0:
  94. newpage = str(int(page) + 1)
  95. item = {'type': 'dir', 'name': 'nextpage', 'title': 'Następna strona', 'category': url, 'page': newpage, 'icon': ''}
  96. self.currList.append(item)
  97. def getMovieUrls(self, vID):
  98. printDBG("Kabarety.getMovieUrls vID: " + vID)
  99. url = 'http://www.youtube.com/watch?v=' + vID
  100. tmpTab = self.ytp.getDirectLinks(url, self.ytformats)
  101. movieUrls = []
  102. for item in tmpTab:
  103. movieUrls.append({'name': item['format'] + '\t' + item['ext'] , 'url':item['url']})
  104. return movieUrls
  105. def handleService(self, index, refresh = 0, searchPattern = ''):
  106. printDBG("Kabarety.handleService")
  107. if 0 == refresh:
  108. if len(self.currList) <= index:
  109. printDBG( "handleService wrong index: %s, len(self.currList): %d" % (index, len(self.currList)) )
  110. return
  111. self.name = None
  112. self.title = ''
  113. self.category = ''
  114. self.page = ''
  115. self.icon = ''
  116. self.link = ''
  117. self.action = ''
  118. if -1 == index:
  119. printDBG( "Kinomaniak: handleService for first self.category" )
  120. else:
  121. item = self.currList[index]
  122. if "name" in item: self.name = item['name']
  123. if "title" in item: self.title = item['title']
  124. if "category" in item: self.category = item['category']
  125. if "page" in item: self.page = item['page']
  126. if "icon" in item: self.icon = item['icon']
  127. if "url" in item: self.link = item['url']
  128. if "action" in item: self.action = item['action']
  129. printDBG( "Kabarety: |||||||||||||||||||||||||||||||||||| %s " % item["name"] )
  130. self.currList = []
  131. name = self.name
  132. title = self.title
  133. category = self.category
  134. page = self.page
  135. icon = self.icon
  136. link = self.link
  137. action = self.action
  138. if str(page)=='None' or page=='': page = '1'
  139. #MAIN MENU
  140. if name == None:
  141. self.listsMainMenu(self.SERVICE_MENU_TABLE)
  142. #NAJNOWSZE
  143. elif category == self.setTable()[1]:
  144. self.getFilmTab(self.NOW_LINK, page)
  145. #NAJLEPSZE
  146. elif category == self.setTable()[2]:
  147. self.getFilmTab(self.NAJ_LINK, page)
  148. #KATEGORIE
  149. elif category == self.setTable()[3]:
  150. self.getCategories(self.MAINURL)
  151. #WYSZUKAJ
  152. elif category == self.setTable()[4]:
  153. self.getFilmTab(self.MAINURL + '/search/' + searchPattern + '/page:', page)
  154. #LISTA TYTULOW
  155. elif name == 'category' or name == 'nextpage':
  156. url = category + '/page:'
  157. self.getFilmTab(url, page)
  158. # end Kabarety
  159. def _getLinkQuality( itemLink ):
  160. tab = itemLink['name'].split('x')
  161. return int(tab[0])
  162. class IPTVHost(IHost):
  163. def __init__(self):
  164. self.host = None
  165. self.currIndex = -1
  166. self.listOfprevList = []
  167. self.searchPattern = ''
  168. self.searchType = ''
  169. # return firs available list of item category or video or link
  170. def getInitList(self):
  171. self.isSearch = False
  172. self.host = Kabarety()
  173. self.currIndex = -1
  174. self.listOfprevList = []
  175. self.host.handleService(self.currIndex)
  176. convList = self.convertList(self.host.getCurrList())
  177. return RetHost(RetHost.OK, value = convList)
  178. # return List of item from current List
  179. # for given Index
  180. # 1 == refresh - force to read data from
  181. # server if possible
  182. # server instead of cache
  183. def getListForItem(self, Index = 0, refresh = 0, selItem = None):
  184. self.listOfprevList.append(self.host.getCurrList())
  185. self.currIndex = Index
  186. self.host.handleService(Index, refresh, self.searchPattern)
  187. convList = self.convertList(self.host.getCurrList())
  188. return RetHost(RetHost.OK, value = convList)
  189. # return prev requested List of item
  190. # for given Index
  191. # 1 == refresh - force to read data from
  192. # server if possible
  193. def getPrevList(self, refresh = 0):
  194. if(len(self.listOfprevList) > 0):
  195. hostList = self.listOfprevList.pop()
  196. self.host.setCurrList(hostList)
  197. convList = self.convertList(hostList)
  198. return RetHost(RetHost.OK, value = convList)
  199. else:
  200. return RetHost(RetHost.ERROR, value = [])
  201. # return current List
  202. # for given Index
  203. # 1 == refresh - force to read data from
  204. # server if possible
  205. def getCurrentList(self, refresh = 0):
  206. if refresh == 1:
  207. self.host.handleService(self.currIndex, refresh, self.searchPattern)
  208. convList = self.convertList(self.host.getCurrList())
  209. return RetHost(RetHost.OK, value = convList)
  210. # return list of links for VIDEO with given Index
  211. # for given Index
  212. def getLinksForVideo(self, Index = 0, selItem = None):
  213. listLen = len(self.host.currList)
  214. if listLen < Index and listLen > 0:
  215. printDBG( "ERROR getLinksForVideo - current list is to short len: %d, Index: %d" % (listLen, Index) )
  216. return RetHost(RetHost.ERROR, value = [])
  217. if self.host.currList[Index]["type"] != 'video':
  218. printDBG( "ERROR getLinksForVideo - current item has wrong type" )
  219. return RetHost(RetHost.ERROR, value = [])
  220. retlist = []
  221. urlList = self.host.getMovieUrls(self.host.currList[Index]["page"])
  222. if config.plugins.iptvplayer.ytUseDF.value:
  223. maxRes = int(config.plugins.iptvplayer.ytDefaultformat.value) * 1.1
  224. def __getLinkQuality( itemLink ):
  225. tab = itemLink['name'].split('x')
  226. return int(tab[0])
  227. urlList = CSelOneLink(urlList, __getLinkQuality, maxRes).getOneLink()
  228. for item in urlList:
  229. nameLink = item['name']
  230. url = item['url']
  231. retlist.append(CUrlItem(nameLink, url, 0))
  232. return RetHost(RetHost.OK, value = retlist)
  233. # end getLinksForVideo
  234. # return resolved url
  235. # for given url
  236. def getResolvedURL(self, url):
  237. if url != None and url != '':
  238. ret = self.host.up.getVideoLink( url )
  239. list = []
  240. if ret:
  241. list.append(ret)
  242. return RetHost(RetHost.OK, value = list)
  243. else:
  244. return RetHost(RetHost.NOT_IMPLEMENTED, value = [])
  245. # end getResolvedURL
  246. # return full path to player logo
  247. def getLogoPath(self):
  248. return RetHost(RetHost.OK, value = [ GetLogoDir('kabaretylogo.png') ])
  249. def convertList(self, cList):
  250. hostList = []
  251. searchTypesOptions = [] # ustawione alfabetycznie
  252. for cItem in cList:
  253. hostLinks = []
  254. type = CDisplayListItem.TYPE_UNKNOWN
  255. possibleTypesOfSearch = None
  256. if cItem['type'] == 'dir':
  257. if cItem['title'] == 'Wyszukaj':
  258. type = CDisplayListItem.TYPE_SEARCH
  259. possibleTypesOfSearch = searchTypesOptions
  260. else:
  261. type = CDisplayListItem.TYPE_CATEGORY
  262. elif cItem['type'] == 'video':
  263. type = CDisplayListItem.TYPE_VIDEO
  264. title = ''
  265. if 'title' in cItem: title = cItem['title']
  266. description = ''
  267. if 'plot' in cItem: description = cItem['plot']
  268. icon = ''
  269. if 'icon' in cItem: icon = cItem['icon']
  270. hostItem = CDisplayListItem(name = title,
  271. description = description,
  272. type = type,
  273. urlItems = hostLinks,
  274. urlSeparateRequest = 1,
  275. iconimage = icon,
  276. possibleTypesOfSearch = possibleTypesOfSearch)
  277. hostList.append(hostItem)
  278. return hostList
  279. # end convertList
  280. def getSearchResults(self, searchpattern, searchType = None):
  281. self.isSearch = True
  282. retList = []
  283. self.searchPattern = searchpattern
  284. self.searchType = searchType
  285. #4: Wyszukaj
  286. return self.getListForItem(3)
  287. # end getSearchResults