PageRenderTime 147ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/IPTVPlayer/hosts/hostmototube_blocked_spark_videos.py

https://gitlab.com/zatoshi/iptvplayer-for-e2
Python | 355 lines | 311 code | 27 blank | 17 comment | 23 complexity | ac34001e1ad89028e8c577182e8a2e1d MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. ###################################################
  3. # LOCAL import
  4. ###################################################
  5. from Plugins.Extensions.IPTVPlayer.components.ihost import CHostBase, CDisplayListItem, RetHost, CUrlItem
  6. from Plugins.Extensions.IPTVPlayer.tools.iptvtools import printDBG, CSearchHistoryHelper, remove_html_markup, GetLogoDir
  7. import Plugins.Extensions.IPTVPlayer.libs.pCommon as pCommon
  8. import Plugins.Extensions.IPTVPlayer.libs.urlparser as urlparser
  9. from Plugins.Extensions.IPTVPlayer.libs.youtube_dl.utils import clean_html
  10. ###################################################
  11. ###################################################
  12. # FOREIGN import
  13. ###################################################
  14. import re
  15. ###################################################
  16. ###################################################
  17. # Config options for HOST
  18. ###################################################
  19. def GetConfigList():
  20. return []
  21. ###################################################
  22. def gettytul():
  23. return 'MotoTube'
  24. class AleKinoTV:
  25. MAINURL = 'http://www.mototube.pl'
  26. SEARCH_URL = MAINURL + '/szukaj/'
  27. SERVICE_MENU_TABLE = {
  28. 1: "Kategorie",
  29. 2: "Wyszukaj",
  30. 3: "Historia wyszukiwania"
  31. }
  32. def __init__(self):
  33. self.up = urlparser.urlparser()
  34. self.cm = pCommon.common()
  35. self.history = CSearchHistoryHelper('MotoTube')
  36. # temporary data
  37. self.currList = []
  38. self.currItem = {}
  39. def getCurrList(self):
  40. return self.currList
  41. def setCurrList(self, list):
  42. self.currList = list
  43. def getCurrItem(self):
  44. return self.currItem
  45. def setCurrItem(self, item):
  46. self.currItem = item
  47. def addDir(self, params):
  48. params['type'] = 'category'
  49. self.currList.append(params)
  50. return
  51. def playVideo(self, params):
  52. params['type'] = 'video'
  53. self.currList.append(params)
  54. return
  55. def fWrite(self, file, data):
  56. #helper to see html returned by ajax
  57. file_path = '/mnt/hdd/' + file
  58. text_file = open(file_path, "w")
  59. text_file.write(data)
  60. text_file.close()
  61. def getDataBeetwenMarkers(self, data, marker1, marker2, withMarkers = True):
  62. idx1 = data.find(marker1)
  63. if -1 == idx1: return False, None
  64. idx2 = data.find(marker2, idx1 + len(marker1))
  65. if -1 == idx2: return False, None
  66. if withMarkers:
  67. idx2 = idx2 + len(marker2)
  68. else:
  69. idx1 = idx1 + len(marker1)
  70. return True, data[idx1:idx2]
  71. def setTable(self):
  72. return self.SERVICE_MENU_TABLE
  73. def listsMainMenu(self, table):
  74. for num, val in table.items():
  75. params = {'name': 'main-menu', 'title': val, 'category': val}
  76. self.addDir(params)
  77. def listCategories(self, url, cat):
  78. printDBG("listCategories for url[%s] cat[%s]" % (url, cat))
  79. sts, data = self.cm.getPage(url)
  80. if not sts: return
  81. sts, data = self.getDataBeetwenMarkers(data, '<div class="submenu">', '</div>', withMarkers = False)
  82. if not sts: return
  83. match = re.compile('<a class="submenu" href="([^"]+?)">([^<]+?)</a>').findall(data)
  84. if match:
  85. for i in range(len(match)):
  86. params = {'title': match[i][1], 'url': match[i][0], 'category': cat, 'plot':match[i][0]}
  87. self.addDir(params)
  88. def listVideos(self, baseUrl, cat, page):
  89. printDBG("listVideos for url[%s] page[%s]" % (baseUrl, page))
  90. if 1 < int(page) != '1': url = baseUrl + page
  91. else: url = baseUrl
  92. sts, data = self.cm.getPage(url)
  93. if not sts: return
  94. nextPage = False
  95. if -1 < data.find("class='pagination_next'"):
  96. nextPage = True
  97. sts, data = self.getDataBeetwenMarkers(data, '<td class="video">', '</table>', withMarkers = False)
  98. if not sts: return
  99. data = data.split('<td class="video">')
  100. for item in data:
  101. # url & title
  102. match = re.search('class="video_title"><a href="([^"]+?)">([^<]+?)</a>', item)
  103. if match:
  104. url = match.group(1)
  105. title = match.group(2)
  106. else: continue
  107. # img
  108. match = re.search('src="([^"]+?)"', item)
  109. if match: img = match.group(1)
  110. else: img = ''
  111. # plot
  112. match = re.search('<p style="margin:5px;" class="video_details">(.+?)</p>', item, re.DOTALL)
  113. if match: plot = remove_html_markup(match.group(1))
  114. else: plot = ''
  115. params = { 'title': title, 'url': url, 'icon': img, 'plot': plot}
  116. self.playVideo(params)
  117. if nextPage:
  118. params = {'title': "Następna strona", 'url': baseUrl, 'category': cat, 'page':str(int(page)+1)}
  119. self.addDir(params)
  120. def getSearchResult(self, baseUrl, cat, page):
  121. printDBG("getSearchResult for url[%s] page[%s]" % (baseUrl, page))
  122. if 1 < int(page) != '1': url = baseUrl + page
  123. else: url = baseUrl
  124. sts, data = self.cm.getPage(url)
  125. if not sts: return
  126. nextPage = False
  127. if -1 < data.find("class='pagination_next'"):
  128. nextPage = True
  129. sts, data = self.getDataBeetwenMarkers(data, '<td valign="top">', '<div class="menu_dol">', withMarkers = False)
  130. if not sts: return
  131. data = data.split('<td valign="top">')
  132. for item in data:
  133. # url & title
  134. match = re.search('<div class="video_title">[^<]*?<a href="([^"]+?)">([^<]+?)</a>', item)
  135. if match:
  136. url = match.group(1)
  137. title = match.group(2)
  138. else: continue
  139. # img
  140. match = re.search('src="([^"]+?)"', item)
  141. if match: img = match.group(1)
  142. else: img = ''
  143. # plot
  144. match = re.search('<div class="video_details">(.+?)</td>', item, re.DOTALL)
  145. if match: plot = remove_html_markup(match.group(1)) #.replace("</div>", " ")
  146. else: plot = ''
  147. params = { 'title': title, 'url': url, 'icon': img, 'plot': plot}
  148. self.playVideo(params)
  149. if nextPage:
  150. params = {'title': "Następna strona", 'url': baseUrl, 'category': cat, 'page':str(int(page)+1)}
  151. self.addDir(params)
  152. def listsHistory(self):
  153. list = self.history.getHistoryList()
  154. for item in list:
  155. params = { 'name': 'history', 'category': 'Wyszukaj', 'title': item, 'plot': 'Szukaj: "%s"' % item}
  156. self.addDir(params)
  157. def getHostingTable(self, url):
  158. printDBG("getHostingTable for url[%s]" % url)
  159. sts, data = self.cm.getPage(url)
  160. if not sts:
  161. return []
  162. #check for internal link
  163. match = re.search('addVariable\("file","\.\.([^"]+?)"', data)
  164. if match:
  165. directUrl = self.MAINURL + match.group(1)
  166. return [{'name':'Internal_Link', 'url': directUrl}]
  167. #check for external link
  168. match = re.search('<embed src="([^"]+?)"', data)
  169. if match:
  170. return self.getLink(match.group(1))
  171. return []
  172. def getLink(self, url):
  173. printDBG('getLink for url[%s]' % (url))
  174. directUrl = self.up.getVideoLink(url)
  175. if directUrl:
  176. return [{'name': self.up.getHostName(url), 'url': directUrl}]
  177. else:
  178. return []
  179. def handleService(self, index, refresh = 0, searchPattern = '', searchType = ''):
  180. printDBG('handleService start')
  181. if 0 == refresh:
  182. if len(self.currList) <= index:
  183. printDBG( "handleService wrong index: %s, len(self.currList): %d" % (index, len(self.currList)) )
  184. return
  185. if -1 == index:
  186. # use default value
  187. self.currItem = { "name": None }
  188. printDBG( "handleService for first self.category" )
  189. else:
  190. self.currItem = self.currList[index]
  191. name = self.currItem.get("name", '')
  192. title = self.currItem.get("title", '')
  193. category = self.currItem.get("category", '')
  194. page = self.currItem.get("page", '1')
  195. icon = self.currItem.get("icon", '')
  196. url = self.currItem.get("url", '')
  197. printDBG( "handleService: |||||||||||||||||||||||||||||||||||| name[%s], category[%s] " % (name, category) )
  198. self.currList = []
  199. #MAIN MENU
  200. if name == None:
  201. self.listsMainMenu(self.SERVICE_MENU_TABLE)
  202. #KATEGORIE
  203. elif category == "Kategorie":
  204. self.listCategories(self.MAINURL + "/najnowsze/", 'video_category')
  205. elif category == "video_category":
  206. self.listVideos(url, category, page)
  207. #WYSZUKAJ
  208. elif category == "Wyszukaj":
  209. pattern = searchPattern.replace(" ", "-")
  210. self.getSearchResult(self.SEARCH_URL + pattern + "/", "search_next", page)
  211. elif category == "search_next":
  212. self.getSearchResult(url, "search_next", page)
  213. #HISTORIA WYSZUKIWANIA
  214. elif category == "Historia wyszukiwania":
  215. self.listsHistory()
  216. class IPTVHost(CHostBase):
  217. def __init__(self):
  218. CHostBase.__init__(self, AleKinoTV(), True)
  219. def getLogoPath(self):
  220. return RetHost(RetHost.OK, value = [GetLogoDir('mototubelogo.png')])
  221. def getLinksForVideo(self, Index = 0, selItem = None):
  222. listLen = len(self.host.currList)
  223. if listLen < Index and listLen > 0:
  224. printDBG( "ERROR getLinksForVideo - current list is to short len: %d, Index: %d" % (listLen, Index) )
  225. return RetHost(RetHost.ERROR, value = [])
  226. if self.host.currList[Index]["type"] != 'video':
  227. printDBG( "ERROR getLinksForVideo - current item has wrong type" )
  228. return RetHost(RetHost.ERROR, value = [])
  229. retlist = []
  230. urlList = self.host.getHostingTable(self.host.currList[Index]["url"])
  231. for item in urlList:
  232. retlist.append(CUrlItem(item["name"], item["url"], 0))
  233. return RetHost(RetHost.OK, value = retlist)
  234. # end getLinksForVideo
  235. def getResolvedURL(self, url):
  236. # resolve url to get direct url to video file
  237. url = self.host.up.getVideoLink( url )
  238. urlTab = []
  239. if isinstance(url, basestring) and url.startswith('http'):
  240. urlTab.append(url)
  241. return RetHost(RetHost.OK, value = urlTab)
  242. def convertList(self, cList):
  243. hostList = []
  244. for cItem in cList:
  245. hostLinks = []
  246. type = CDisplayListItem.TYPE_UNKNOWN
  247. possibleTypesOfSearch = None
  248. if cItem['type'] == 'category':
  249. if cItem['title'] == 'Wyszukaj':
  250. type = CDisplayListItem.TYPE_SEARCH
  251. else:
  252. type = CDisplayListItem.TYPE_CATEGORY
  253. elif cItem['type'] == 'video':
  254. type = CDisplayListItem.TYPE_VIDEO
  255. url = cItem.get('url', '')
  256. if '' != url:
  257. hostLinks.append(CUrlItem("Link", url, 1))
  258. title = cItem.get('title', '')
  259. description = cItem.get('plot', '')
  260. description = clean_html(description.decode("utf-8")).encode("utf-8")
  261. icon = cItem.get('icon', '')
  262. hostItem = CDisplayListItem(name = title,
  263. description = description,
  264. type = type,
  265. urlItems = hostLinks,
  266. urlSeparateRequest = 1,
  267. iconimage = icon,
  268. possibleTypesOfSearch = possibleTypesOfSearch)
  269. hostList.append(hostItem)
  270. return hostList
  271. # end convertList
  272. def getSearchItemInx(self):
  273. # Find 'Wyszukaj' item
  274. try:
  275. list = self.host.getCurrList()
  276. for i in range( len(list) ):
  277. if list[i]['category'] == 'Wyszukaj':
  278. return i
  279. except:
  280. printDBG('getSearchItemInx EXCEPTION')
  281. return -1
  282. def setSearchPattern(self):
  283. try:
  284. list = self.host.getCurrList()
  285. if 'history' == list[self.currIndex]['name']:
  286. pattern = list[self.currIndex]['title']
  287. self.host.history.addHistoryItem( pattern)
  288. self.searchPattern = pattern
  289. except:
  290. printDBG('setSearchPattern EXCEPTION')
  291. self.searchPattern = ''
  292. return