Python module (submodule repositary), which provides content (video streams) from various online stream sources to corresponding Enigma2, Kodi, Plex plugins

ContentSources.py 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. #!/usr/bin/env python
  2. # coding=utf8
  3. #
  4. # This file is part of PlayStream - enigma2 plugin to play video streams from various sources
  5. # Copyright (c) 2016 ivars777 (ivars777@gmail.com)
  6. # Distributed under the GNU GPL v3. For full terms see http://www.gnu.org/licenses/gpl-3.0.en.html
  7. #
  8. import sys, os, re
  9. import glob, traceback
  10. sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
  11. from sources.SourceBase import stream_type
  12. import util
  13. show_hidden = False # Ja True, tad vienalga radā hidden sources (noder izstradē)
  14. class ContentSources(object):
  15. """Wrapper for content sources plugin"""
  16. #----------------------------------------------------------------------
  17. def __init__(self, plugin_path):
  18. self.plugins = {}
  19. self.error_content = [("..atpakaļ", "back", None, "Kļūda, atgriezties atpakaļ")]
  20. sys.path.insert(0, plugin_path)
  21. #for f in os.listdir(plugin_path):
  22. #for f in next(os.walk(plugin_path))[2]:
  23. print "ContentSources: Importing sources from "+plugin_path
  24. files = glob.glob(os.path.join(plugin_path, "*.py"))
  25. for f in files:
  26. fname, ext = os.path.splitext(f)
  27. fname = os.path.split(fname)[1]
  28. if fname == "__init__": continue
  29. if ext == '.py':
  30. print "Importing %s"%fname
  31. mod = __import__(fname)
  32. reload(mod)
  33. if "Source" in dir(mod):
  34. self.plugins[fname] = mod.Source()
  35. print fname+ " imported"
  36. else:
  37. pass
  38. #print fname+ "skipped"
  39. sys.path.pop(0)
  40. if not "config" in self.plugins:
  41. raise Exception("Problem importing content sources")
  42. cfg = self.plugins["config"]
  43. for pl in self.plugins.keys():
  44. found = False
  45. for lst in cfg.get_lists():
  46. for item in cfg.lists[lst]:
  47. if item[1].split("::")[0]==pl:
  48. found = True
  49. break
  50. if found: break
  51. if not found:
  52. title = self.plugins[pl].title if "title" in dir(self.plugins[pl]) else pl
  53. img = self.plugins[pl].img if "img" in dir(self.plugins[pl]) else ""
  54. desc = self.plugins[pl].desc if "desc" in dir(self.plugins[pl]) else title
  55. cfg.add_item("home",(title,"%s::home"%pl,img,desc))
  56. cfg.write_streams()
  57. def get_content(self,data):
  58. source = data.split("::")[0]
  59. if source in self.plugins:
  60. content0 = self.plugins[source].get_content(data)
  61. if content0:
  62. content = []
  63. if isinstance(content0,list):
  64. for i,item in enumerate(content0):
  65. source2 = item[1].split("::")[0]
  66. if not (source2 == "back" or item[1].startswith("http") or item[1].startswith("rtmp")):
  67. if source2 not in self.plugins or (not show_hidden and "hidden" in dir(self.plugins[source2]) and self.plugins[source2].hidden):
  68. continue
  69. item2=[]
  70. for el in item:
  71. if isinstance(el,unicode):
  72. el = el.encode("utf8")
  73. item2.append(el)
  74. content.append(tuple(item2))
  75. else:
  76. item2=[]
  77. for el in content0:
  78. if isinstance(el,unicode):
  79. el = el.encode("utf8")
  80. item2.append(el)
  81. content=tuple(item2)
  82. return content
  83. else:
  84. return self.error_content
  85. else:
  86. return self.error_content
  87. def get_streams(self,data):
  88. if stream_type(data):
  89. if "::" in data:
  90. data = data.split("::")[1]
  91. content = self.get_content(data)
  92. stream = util.item()
  93. stream["name"] = data
  94. stream["url"] = data
  95. stream["type"] = stream_type(data)
  96. #stream["img"] = ""
  97. #stream["desc"] = ""
  98. return[stream]
  99. if not self.is_video(data):
  100. return []
  101. source = data.split("::")[0]
  102. if source in self.plugins:
  103. streams = self.plugins[source].get_streams(data)
  104. for s in streams:
  105. for k in s:
  106. if isinstance(s[k],unicode):
  107. s[k] = s[k].encode("utf8")
  108. if not "resolver" in s:
  109. s["resolver"] = source
  110. if not "surl" in s or not s["surl"]:
  111. s["surl"] = data
  112. if not "nfo" in s:
  113. s["nfo"]={"movie":{"title":s["name"],"thumb":s["img"],"plot":s["desc"]}}
  114. return streams
  115. else:
  116. return []
  117. def get_info(self,data):
  118. nfo = {}
  119. if self.is_video(data):
  120. source = data.split("::")[0]
  121. if source in self.plugins:
  122. if "get_info" in dir(self.plugins[source]):
  123. streams = self.get_streams(data)
  124. nfo = streams[0]["nfo"] if streams and "nfo" in streams[0] else {}
  125. else:
  126. nfo = self.plugins[source].get_info(data)
  127. else:
  128. pass # TODO create nfo for listing
  129. return nfo
  130. def stream_type(self,data):
  131. return stream_type(data)
  132. def is_video(self,data):
  133. if self.stream_type(data):
  134. return True
  135. source = data.split("::")[0]
  136. if source in self.plugins:
  137. return self.plugins[source].is_video(data)
  138. else:
  139. return False
  140. def options_read(self,source):
  141. if source in self.plugins:
  142. options = self.plugins[source].options_read()
  143. if options:
  144. return options
  145. else:
  146. return None
  147. else:
  148. return None
  149. def options_write(self,source,options):
  150. if source in self.plugins:
  151. return self.plugins[source].options_write(options)
  152. else:
  153. return None
  154. def run(data="config::home"):
  155. sources = ContentSources("sources")
  156. #options = sources.options_read("ltc")
  157. #print options
  158. history = []
  159. cur = ("Home",data,None,None)
  160. content = sources.get_content(cur[1])
  161. exit_loop = False
  162. while True:
  163. print
  164. for i,item in enumerate(content):
  165. s = "%i: %s - %s %s"%(i,item[0],item[1],item[2])
  166. print s #.encode(sys.stdout.encoding,"replace")
  167. while True:
  168. a = raw_input("Enter number, (-) for download, q for exit: ")
  169. if a in ("q","Q","x","X"):
  170. exit_loop = True
  171. print "Exiting"
  172. break
  173. try:
  174. n = int(a)
  175. break
  176. except:
  177. print "Not number!"
  178. if exit_loop: break
  179. download = False
  180. if n<0:
  181. n = abs(n)
  182. download = True
  183. cur2 = content[n]
  184. data0 = cur2[1].split("::")[1] if "::" in cur2[1] else cur2[1]
  185. if not data0:
  186. pass
  187. elif cur2[1] == "back":
  188. cur = history.pop()
  189. elif sources.is_video(cur2[1]):
  190. if sources.stream_type(cur2[1]):
  191. stream = util.item()
  192. stream["url"] = cur2[1]
  193. stream["name"] = cur2[0]
  194. streams = [stream]
  195. else:
  196. try:
  197. if not download:
  198. streams = sources.get_streams(cur2[1])
  199. else:
  200. stream = util.item()
  201. stream["url"] = cur2[1]
  202. stream["name"] = cur2[0]
  203. stream["url"] = util.streamproxy_encode2(stream["url"])
  204. print stream["url"]
  205. streams = [stream]
  206. except Exception as e:
  207. print unicode(e)
  208. traceback.print_exc()
  209. streams = []
  210. if streams:
  211. if not download:
  212. util.play_video(streams)
  213. else:
  214. #urlp = util.streamproxy_encode2(streams[0]["url"])
  215. #print urlp
  216. #util.player(urlp)
  217. #Downloader.download_video(streams)
  218. pass
  219. else:
  220. print "**No stream to play - %s "%(
  221. cur2[1])
  222. raw_input("Press any key")
  223. #import os
  224. #os.system('"c:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%s"'%cur2[1])
  225. else:
  226. if "{0}" in cur2[1]:
  227. a = raw_input("Enter value:")
  228. cur2 = (cur2[0],cur2[1].format(a),cur2[2],cur2[3])
  229. history.append(cur)
  230. cur = cur2
  231. try:
  232. content = sources.get_content(cur[1])
  233. except Exception as e:
  234. print unicode(e)
  235. traceback.print_exc()
  236. raw_input("Continue?")
  237. if __name__ == "__main__":
  238. show_hidden = False
  239. if len(sys.argv)>1:
  240. data= sys.argv[1]
  241. else:
  242. data = "config::home"
  243. run(data)