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

/IPTVPlayer/hosts/hostfighttube.py

https://gitlab.com/kamoula77/iptvplayer-for-e2
Python | 338 lines | 309 code | 15 blank | 14 comment | 3 complexity | 6b585c7130531beafd22f62ca4dbb183 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. ###################################################
  3. # LOCAL import
  4. ###################################################
  5. from Plugins.Extensions.IPTVPlayer.components.ihost import IHost, CDisplayListItem, RetHost, CUrlItem
  6. from Plugins.Extensions.IPTVPlayer.libs.pCommon import common, CParsingHelper
  7. from Plugins.Extensions.IPTVPlayer.libs.urlparser import urlparser
  8. from Plugins.Extensions.IPTVPlayer.tools.iptvtools import printDBG, CSearchHistoryHelper, GetLogoDir, CSelOneLink
  9. from Plugins.Extensions.IPTVPlayer.libs.youtubeparser import YouTubeParser
  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 'FightTube'
  24. class FightTube:
  25. MAINURL = 'http://www.fighttube.pl/'
  26. SEARCHURL = MAINURL + 'search/?keywords='
  27. def __init__(self):
  28. self.cm = common()
  29. self.up = urlparser()
  30. self.history = CSearchHistoryHelper('fighttube')
  31. self.ytp = YouTubeParser()
  32. self.ytformats = config.plugins.iptvplayer.ytformat.value
  33. # temporary data
  34. self.currList = []
  35. self.currItem = {}
  36. def getCurrList(self):
  37. return self.currList
  38. def setCurrList(self, list):
  39. self.currList = list
  40. def getCurrItem(self):
  41. return self.currItem
  42. def setCurrItem(self, item):
  43. self.currItem = item
  44. @staticmethod
  45. def printDBG( strDBG ):
  46. printDBG('[IPTV FightTube] ' + strDBG)
  47. def getVideoUrl(self, url):
  48. FightTube.printDBG("getVideoUrl url[%s]" % url)
  49. sts, data = self.cm.getPage(url)
  50. if not sts: return []
  51. #data = self.cm.ph.getDataBeetwenMarkers(data, '<div class="content">', "<ul")[1]
  52. vidUrl = self.cm.ph.getSearchGroups(data, '<embed src="([^"]+?)" type="application/x-shockwave-flash"')[0]
  53. if '' == vidUrl: vidUrl = self.cm.ph.getSearchGroups(data, 'src="([^"]+?/video/embed[^"]+?)"')[0]
  54. return self.up.getVideoLinkExt(vidUrl)
  55. def getYTVideoUrl(self, url):
  56. FightTube.printDBG("getYTVideoUrl url[%s]" % url)
  57. tmpTab = self.ytp.getDirectLinks(url, self.ytformats)
  58. movieUrls = []
  59. for item in tmpTab:
  60. movieUrls.append({'name': item['format'] + '\t' + item['ext'] , 'url':item['url']})
  61. return movieUrls
  62. def listsMainMenu(self):
  63. FightTube.printDBG('listsMainMenu start')
  64. query_data = {'url': self.MAINURL, 'return_data': True}
  65. try:
  66. data = self.cm.getURLRequestData(query_data)
  67. except Exception:
  68. FightTube.printDBG('listsMainMenu exception')
  69. return
  70. sts, data = CParsingHelper.getDataBeetwenMarkers(data, 'Kategorie video', '</ul>', False)
  71. if not sts:
  72. printDBG('listsMainMenu: menu marker cannot be found!')
  73. return
  74. match = re.compile("<a href='([^']+?)' class='level0'[^>]+?>([^<]+?)</a>").findall(data)
  75. if len(match) > 0:
  76. for i in range(len(match)):
  77. params = {'type': 'category', 'title': match[i][1], 'page': match[i][0], 'icon': ''}
  78. self.currList.append(params)
  79. params = {'type': 'category', 'title': 'Wyszukaj', 'page': self.SEARCHURL, 'icon': ''}
  80. self.currList.append(params)
  81. return
  82. def getMovieTab(self, url):
  83. FightTube.printDBG('getMovieTab start')
  84. query_data = { 'url': url, 'return_data': True }
  85. try:
  86. data = self.cm.getURLRequestData(query_data)
  87. except Exception:
  88. FightTube.printDBG('getMovieTab exception')
  89. return
  90. # get next page url
  91. nexPageUrl = ''
  92. sts, tmp = CParsingHelper.getDataBeetwenMarkers(data, "<nav class='pagination'>", "</nav>", False)
  93. if sts:
  94. match = re.search("<li><a href='([^']+?)'>&gt;</a></li>", tmp)
  95. if match: nexPageUrl = match.group(1)
  96. # separete vidTab
  97. sts, data = CParsingHelper.getDataBeetwenMarkers(data, "<ul class='videos-listing'>", "</ul>", False)
  98. if not sts:
  99. printDBG('getMovieTab: main markers cannot be found!')
  100. return
  101. # separate videos data
  102. data = data.split('</li>')
  103. for vidItem in data:
  104. url = ''
  105. title = ''
  106. icon = ''
  107. ocena = ''
  108. wyswietlen = ''
  109. match = re.search("<a href='([^']+?)'", vidItem)
  110. if match:
  111. url = match.group(1)
  112. match = re.search("<img src='([^']+?)' alt='([^']+?)'", vidItem)
  113. if match:
  114. icon = match.group(1)
  115. title = match.group(2)
  116. if '' != url and '' != title:
  117. params = {'type': 'video', 'title': title, 'page': url, 'icon': icon}
  118. self.currList.append(params)
  119. if nexPageUrl.startswith("http://"):
  120. params = {'type': 'category', 'name': 'nextpage', 'title': 'Następna strona', 'page': nexPageUrl, 'icon': ''}
  121. self.currList.append(params)
  122. return
  123. def searchTab(self, text):
  124. FightTube.printDBG('searchTab start')
  125. self.getMovieTab(self.SEARCHURL + text)
  126. def handleService(self, index, refresh = 0, searchPattern = ''):
  127. FightTube.printDBG('handleService start')
  128. if 0 == refresh:
  129. if len(self.currList) <= index:
  130. FightTube.printDBG( "handleService wrong index: %s, len(self.currList): %d" % (index, len(self.currList)) )
  131. return
  132. if -1 == index:
  133. # use default value
  134. self.currItem = { "name": None }
  135. FightTube.printDBG( "handleService for first self.category" )
  136. else:
  137. self.currItem = self.currList[index]
  138. name = self.currItem.get("name", '')
  139. title = self.currItem.get("title", '')
  140. category = self.currItem.get("category", '')
  141. page = self.currItem.get("page", '')
  142. icon = self.currItem.get("icon", '')
  143. type = self.currItem.get("type", '')
  144. FightTube.printDBG( "handleService: |||||||||||||||||||||||||||||||||||| [%s] " % name )
  145. self.currList = []
  146. #MAIN MENU == KATEGORIE
  147. if name == None:
  148. self.listsMainMenu()
  149. elif type != 'category':
  150. return
  151. #WYSZUKAJ
  152. elif title == 'Wyszukaj':
  153. if searchPattern != None:
  154. self.searchTab(searchPattern)
  155. else:
  156. self.getMovieTab(page)
  157. class IPTVHost(IHost):
  158. def __init__(self):
  159. self.host = None
  160. self.currIndex = -1
  161. self.listOfprevList = []
  162. self.listOfprevItems = []
  163. self.searchPattern = ''
  164. # return firs available list of item category or video or link
  165. def getInitList(self):
  166. self.isSearch = False
  167. self.host = FightTube()
  168. self.currIndex = -1
  169. self.listOfprevList = []
  170. self.listOfprevItems = []
  171. self.host.handleService(self.currIndex)
  172. convList = self.convertList(self.host.getCurrList())
  173. return RetHost(RetHost.OK, value = convList)
  174. # return List of item from current List
  175. # for given Index
  176. # 1 == refresh - force to read data from
  177. # server if possible
  178. # server instead of cache
  179. def getListForItem(self, Index = 0, refresh = 0, selItem = None):
  180. self.listOfprevList.append(self.host.getCurrList())
  181. self.listOfprevItems.append(self.host.getCurrItem())
  182. self.currIndex = Index
  183. self.host.handleService(Index, refresh, self.searchPattern)
  184. convList = self.convertList(self.host.getCurrList())
  185. return RetHost(RetHost.OK, value = convList)
  186. # return prev requested List of item
  187. # for given Index
  188. # 1 == refresh - force to read data from
  189. # server if possible
  190. def getPrevList(self, refresh = 0):
  191. if(len(self.listOfprevList) > 0):
  192. hostList = self.listOfprevList.pop()
  193. hostCurrItem = self.listOfprevItems.pop()
  194. self.host.setCurrList(hostList)
  195. self.host.setCurrItem(hostCurrItem)
  196. convList = self.convertList(hostList)
  197. return RetHost(RetHost.OK, value = convList)
  198. else:
  199. return RetHost(RetHost.ERROR, value = [])
  200. # return current List
  201. # for given Index
  202. # 1 == refresh - force to read data from
  203. # server if possible
  204. def getCurrentList(self, refresh = 0):
  205. if refresh == 1:
  206. self.host.handleService(self.currIndex, refresh, self.searchPattern)
  207. convList = self.convertList(self.host.getCurrList())
  208. return RetHost(RetHost.OK, value = convList)
  209. # return list of links for VIDEO with given Index
  210. # for given Index
  211. def getLinksForVideo(self, Index = 0, selItem = None):
  212. listLen = len(self.host.currList)
  213. if listLen < Index and listLen > 0:
  214. printDBG( "ERROR getLinksForVideo - current list is to short len: %d, Index: %d" % (listLen, Index) )
  215. return RetHost(RetHost.ERROR, value = [])
  216. if self.host.currList[Index]["type"] != 'video':
  217. printDBG( "ERROR getLinksForVideo - current item has wrong type" )
  218. return RetHost(RetHost.ERROR, value = [])
  219. urlsTab = self.host.getVideoUrl(self.host.currList[Index]["page"])
  220. retlist = []
  221. for urlItem in urlsTab:
  222. retlist.append(CUrlItem(urlItem['name'], urlItem['url'], 0))
  223. return RetHost(RetHost.OK, value = retlist)
  224. # end getLinksForVideo
  225. # return resolved url
  226. # for given url
  227. def getResolvedURL(self, url):
  228. return RetHost(RetHost.NOT_IMPLEMENTED, value = [])
  229. # end getResolvedURL
  230. # return full path to player logo
  231. def getLogoPath(self):
  232. return RetHost(RetHost.OK, value = [ GetLogoDir('fighttubelogo.png') ])
  233. def convertList(self, cList):
  234. hostList = []
  235. for cItem in cList:
  236. hostLinks = []
  237. type = CDisplayListItem.TYPE_UNKNOWN
  238. possibleTypesOfSearch = None
  239. if cItem['type'] == 'category':
  240. if cItem['title'] == 'Wyszukaj':
  241. type = CDisplayListItem.TYPE_SEARCH
  242. else:
  243. type = CDisplayListItem.TYPE_CATEGORY
  244. elif cItem['type'] == 'video':
  245. type = CDisplayListItem.TYPE_VIDEO
  246. title = cItem.get('title', '')
  247. description = cItem.get('plot', '')
  248. icon = cItem.get('icon', '')
  249. hostItem = CDisplayListItem(name = title,
  250. description = description,
  251. type = type,
  252. urlItems = hostLinks,
  253. urlSeparateRequest = 1,
  254. iconimage = icon )
  255. hostList.append(hostItem)
  256. return hostList
  257. # end convertList
  258. def getSearchResults(self, searchpattern, searchType = None):
  259. self.isSearch = True
  260. retList = []
  261. self.searchPattern = searchpattern
  262. # Find 'Wyszukaj' item
  263. list = self.host.getCurrList()
  264. try:
  265. for i in range( len(list) ):
  266. if list[i]['title'] == 'Wyszukaj':
  267. return self.getListForItem( i )
  268. except Exception:
  269. printDBG('getSearchResults EXCEPTION')
  270. return RetHost(RetHost.ERROR, value = [])
  271. # end getSearchResults