PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/IPTVPlayer/hosts/hostpinkbike.py

https://gitlab.com/zatoshi/iptvplayer-for-e2
Python | 291 lines | 246 code | 28 blank | 17 comment | 25 complexity | 6c68e5f83d619dac604a5180277c1342 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. ###################################################
  3. # LOCAL import
  4. ###################################################
  5. from Plugins.Extensions.IPTVPlayer.components.iptvplayerinit import TranslateTXT as _
  6. from Plugins.Extensions.IPTVPlayer.components.ihost import CHostBase, CBaseHostClass, CDisplayListItem, ArticleContent, RetHost, CUrlItem
  7. from Plugins.Extensions.IPTVPlayer.tools.iptvtools import printDBG, printExc, GetLogoDir, GetCookieDir, byteify
  8. from Plugins.Extensions.IPTVPlayer.libs.urlparserhelper import getDirectM3U8Playlist
  9. from Plugins.Extensions.IPTVPlayer.libs.urlparser import urlparser
  10. ###################################################
  11. ###################################################
  12. # FOREIGN import
  13. ###################################################
  14. from Components.config import config, ConfigInteger, ConfigSelection, ConfigYesNo, ConfigText, getConfigListEntry
  15. from datetime import timedelta
  16. from binascii import hexlify
  17. import re
  18. import urllib
  19. import time
  20. import random
  21. try: import simplejson as json
  22. except: import json
  23. ###################################################
  24. ###################################################
  25. # E2 GUI COMMPONENTS
  26. ###################################################
  27. from Plugins.Extensions.IPTVPlayer.components.asynccall import MainSessionWrapper
  28. from Screens.MessageBox import MessageBox
  29. ###################################################
  30. ###################################################
  31. # Config options for HOST
  32. ###################################################
  33. config.plugins.iptvplayer.hitbox_iconssize = ConfigSelection(default = "medium", choices = [ ("large", _("large")), ("medium", _("medium")), ("small", _("small")) ])
  34. def GetConfigList():
  35. optionList = []
  36. return optionList
  37. ###################################################
  38. def gettytul():
  39. return 'pinkbike.com'
  40. class Pinkbike(CBaseHostClass):
  41. MAIN_URL = 'http://www.pinkbike.com/'
  42. VID_MAIN_URL = MAIN_URL + 'video/'
  43. VID_SRCH_URL = VID_MAIN_URL + 'search/?q='
  44. MAIN_CAT_TAB = [{'category':'best_video_categories', 'title':_('Best Pinkbike Videos') },
  45. {'category':'video_categories', 'title':_('Categories') },
  46. {'category':'search', 'title':_('Search'), 'search_item':True},
  47. {'category':'search_history', 'title':_('Search history')} ]
  48. def __init__(self):
  49. printDBG("Pinkbike.__init__")
  50. CBaseHostClass.__init__(self, {'history':'Pinkbike.tv'})
  51. self.best = []
  52. self.categories = []
  53. self.catItems = {}
  54. def _fillCategories(self):
  55. printDBG("Pinkbike._fillCategories")
  56. if len(self.best): return
  57. sts, data = self.cm.getPage( Pinkbike.VID_MAIN_URL )
  58. if not sts: return
  59. bestData = self.cm.ph.getDataBeetwenMarkers(data, 'Best Pinkbike Videos', '</div>', False)[1]
  60. bestData = re.compile('href="([^"]+?)"[^>]*?>([^<]+?)</a>').findall(bestData)
  61. for item in bestData: self.best.append({'url':item[0], 'title':item[1]})
  62. if len(self.categories): return
  63. data = self.cm.ph.getDataBeetwenMarkers(data, '<td valign="top" width="25%">', '</div>', False)[1]
  64. data = data.split('</table>')
  65. if len(data): del data[-1]
  66. for item in data:
  67. title = self.cm.ph.getDataBeetwenMarkers(item, '<h3>', '</h3>', False)[1]
  68. desc = self.cm.ph.getDataBeetwenMarkers(item, '<h5>', '</h5>', False)[1]
  69. catItems = []
  70. tmp = re.compile('<td>(.+?)</td>').findall(item)
  71. for cat in tmp:
  72. url = self.cm.ph.getSearchGroups(cat, 'href="([^"]+?)"')[0]
  73. tit = self.cleanHtmlStr(cat)
  74. if url.startswith('http'): catItems.append({'title':tit, 'url':url})
  75. if len(tmp):
  76. self.categories.append({'title':title, 'desc':desc})
  77. self.catItems[title] = catItems
  78. def listBestCategories(self, cItem, category):
  79. printDBG("Pinkbike.listBestCategories")
  80. self._fillCategories()
  81. for item in self.best:
  82. params = dict(cItem)
  83. params.update(item)
  84. params['category'] = category
  85. self.addDir(params)
  86. def listCategories(self, cItem, category):
  87. printDBG("Pinkbike.listCategories")
  88. self._fillCategories()
  89. for item in self.categories:
  90. params = dict(cItem)
  91. params.update(item)
  92. params['category'] = category
  93. self.addDir(params)
  94. def listCatItems(self, cItem, category):
  95. printDBG("Pinkbike.listSubCategories")
  96. for item in self.catItems[cItem['title']]:
  97. params = dict(cItem)
  98. params.update(item)
  99. params['category'] = category
  100. self.addDir(params)
  101. def listVideos(self, cItem):
  102. printDBG("Pinkbike.listVideos")
  103. page = cItem.get('page', 1)
  104. if '?' in cItem['url']: url = cItem['url'] + '&'
  105. else: url = cItem['url'] + '?'
  106. url = url + 'page=' + str(page)
  107. sts,data = self.cm.getPage(url)
  108. if not sts: return
  109. if ('page=%d"' % (page+1)) in data: nextPage = True
  110. else: nextPage = False
  111. if '<table class="paging-container">' in data: marker = '<table class="paging-container">'
  112. else: marker = '<div class="foot f11">'
  113. data = self.cm.ph.getDataBeetwenMarkers(data, '<div id="inList" class="fullview">', marker, False)[1]
  114. data = data.split('</ul>')
  115. if len(data): del data[-1]
  116. for item in data:
  117. item = item.split('</li>')
  118. icon = self.cm.ph.getSearchGroups(item[0], 'src="([^"]+?)"')[0]
  119. desc = self.cleanHtmlStr(item[1])
  120. url = self.cm.ph.getSearchGroups(item[1], 'href="([^"]+?)"')[0]
  121. title = self.cleanHtmlStr( self.cm.ph.getSearchGroups(item[1], 'title="([^"]+?)"')[0] + ' ' + self.cm.ph.getSearchGroups(item[1], '<a [^>]+?>(.+?)</a>')[0] )
  122. self.addVideo({'title':title, 'url':url, 'icon':icon, 'desc':desc})
  123. if nextPage:
  124. params = dict(cItem)
  125. params.update({'title':_('Next page'), 'page':page+1})
  126. self.addDir(params)
  127. def listSearchResult(self, cItem, searchPattern, searchType):
  128. printDBG("Pinkbike.listSearchResult cItem[%s], searchPattern[%s] searchType[%s]" % (cItem, searchPattern, searchType))
  129. searchPattern = urllib.quote_plus(searchPattern)
  130. item = dict(cItem)
  131. item['category'] = 'list_videos'
  132. item['url'] = Pinkbike.VID_SRCH_URL + searchPattern
  133. self.listVideos(item)
  134. def getLinksForVideo(self, cItem):
  135. printDBG("Pinkbike.getLinksForVideo [%s]" % cItem)
  136. urlTab = []
  137. sts,data = self.cm.getPage(cItem['url'])
  138. if not sts: return urlTab
  139. data = self.cm.ph.getDataBeetwenMarkers(data, '<video', '</video>', False)[1].replace('\\"', '"')
  140. data = re.compile('data-quality="([^"]+?)"[^>]+?src="([^"]+?)"').findall(data)
  141. for item in data: urlTab.append({'name':item[0], 'url':item[1]})
  142. return urlTab
  143. def getFavouriteData(self, cItem):
  144. return cItem['url']
  145. def getLinksForFavourite(self, fav_data):
  146. return self.getLinksForVideo({'url':fav_data})
  147. def handleService(self, index, refresh=0, searchPattern='', searchType=''):
  148. printDBG('Pinkbike.handleService start')
  149. CBaseHostClass.handleService(self, index, refresh, searchPattern, searchType)
  150. name = self.currItem.get("name", None)
  151. category = self.currItem.get("category", '')
  152. printDBG( "Pinkbike.handleService: ---------> name[%s], category[%s] " % (name, category) )
  153. searchPattern = self.currItem.get("search_pattern", searchPattern)
  154. self.currList = []
  155. if None == name:
  156. self.listsTab(Pinkbike.MAIN_CAT_TAB, {'name':'category'})
  157. elif 'best_video_categories' == category:
  158. self.listBestCategories(self.currItem, 'list_videos')
  159. elif 'video_categories' == category:
  160. self.listCategories(self.currItem, 'list_sub_video_categories')
  161. elif 'list_sub_video_categories' == category:
  162. self.listCatItems(self.currItem, 'list_videos')
  163. elif 'list_videos' == category:
  164. self.listVideos(self.currItem)
  165. #WYSZUKAJ
  166. elif category in ["search"]:
  167. cItem = dict(self.currItem)
  168. cItem.update({'search_item':False, 'name':'category'})
  169. self.listSearchResult(cItem, searchPattern, searchType)
  170. #HISTORIA WYSZUKIWANIA
  171. elif category == "search_history":
  172. self.listsHistory({'name':'history', 'category': 'search'}, 'desc', _("Type: "))
  173. else:
  174. printExc()
  175. CBaseHostClass.endHandleService(self, index, refresh)
  176. class IPTVHost(CHostBase):
  177. def __init__(self):
  178. CHostBase.__init__(self, Pinkbike(), True, [CDisplayListItem.TYPE_VIDEO, CDisplayListItem.TYPE_AUDIO])
  179. def getLogoPath(self):
  180. return RetHost(RetHost.OK, value = [GetLogoDir('pinkbikelogo.png')])
  181. def getLinksForVideo(self, Index = 0, selItem = None):
  182. retCode = RetHost.ERROR
  183. retlist = []
  184. if not self.isValidIndex(Index): return RetHost(retCode, value=retlist)
  185. urlList = self.host.getLinksForVideo(self.host.currList[Index])
  186. for item in urlList:
  187. need_resolve = 0
  188. name = item["name"]
  189. url = item["url"]
  190. retlist.append(CUrlItem(name, url, need_resolve))
  191. return RetHost(RetHost.OK, value = retlist)
  192. # end getLinksForVideo
  193. def converItem(self, cItem):
  194. hostList = []
  195. searchTypesOptions = [] # ustawione alfabetycznie
  196. hostLinks = []
  197. type = CDisplayListItem.TYPE_UNKNOWN
  198. possibleTypesOfSearch = None
  199. if 'category' == cItem['type']:
  200. if cItem.get('search_item', False):
  201. type = CDisplayListItem.TYPE_SEARCH
  202. possibleTypesOfSearch = searchTypesOptions
  203. else:
  204. type = CDisplayListItem.TYPE_CATEGORY
  205. elif cItem['type'] == 'video':
  206. type = CDisplayListItem.TYPE_VIDEO
  207. elif 'more' == cItem['type']:
  208. type = CDisplayListItem.TYPE_MORE
  209. elif 'audio' == cItem['type']:
  210. type = CDisplayListItem.TYPE_AUDIO
  211. if type in [CDisplayListItem.TYPE_AUDIO, CDisplayListItem.TYPE_VIDEO]:
  212. url = cItem.get('url', '')
  213. if '' != url:
  214. hostLinks.append(CUrlItem("Link", url, 1))
  215. title = cItem.get('title', '')
  216. description = cItem.get('desc', '')
  217. icon = cItem.get('icon', '')
  218. return CDisplayListItem(name = title,
  219. description = description,
  220. type = type,
  221. urlItems = hostLinks,
  222. urlSeparateRequest = 1,
  223. iconimage = icon,
  224. possibleTypesOfSearch = possibleTypesOfSearch)
  225. # end converItem
  226. def getSearchItemInx(self):
  227. try:
  228. list = self.host.getCurrList()
  229. for i in range( len(list) ):
  230. if list[i]['category'] == 'search':
  231. return i
  232. except:
  233. printDBG('getSearchItemInx EXCEPTION')
  234. return -1
  235. def setSearchPattern(self):
  236. try:
  237. list = self.host.getCurrList()
  238. if 'history' == list[self.currIndex]['name']:
  239. pattern = list[self.currIndex]['title']
  240. search_type = list[self.currIndex]['search_type']
  241. self.host.history.addHistoryItem( pattern, search_type)
  242. self.searchPattern = pattern
  243. self.searchType = search_type
  244. except:
  245. printDBG('setSearchPattern EXCEPTION')
  246. self.searchPattern = ''
  247. self.searchType = ''
  248. return