Enigma2 plugin to to play various online streams (mostly Latvian).

ContentSources.py 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. if __name__ == "__main__":
  155. show_hidden = False
  156. sources = ContentSources("sources")
  157. if len(sys.argv)>1:
  158. data= sys.argv[1]
  159. else:
  160. data = "config::home"
  161. #options = sources.options_read("ltc")
  162. #print options
  163. history = []
  164. cur = ("Home",data,None,None)
  165. content = sources.get_content(cur[1])
  166. exit_loop = False
  167. while True:
  168. print
  169. for i,item in enumerate(content):
  170. s = "%i: %s - %s %s"%(i,item[0],item[1],item[2])
  171. print s #.encode(sys.stdout.encoding,"replace")
  172. while True:
  173. a = raw_input("Enter number, (-) for download, q for exit: ")
  174. if a in ("q","Q","x","X"):
  175. exit_loop = True
  176. print "Exiting"
  177. break
  178. try:
  179. n = int(a)
  180. break
  181. except:
  182. print "Not number!"
  183. if exit_loop: break
  184. download = False
  185. if n<0:
  186. n = abs(n)
  187. download = True
  188. cur2 = content[n]
  189. data0 = cur2[1].split("::")[1] if "::" in cur2[1] else cur2[1]
  190. if not data0:
  191. pass
  192. elif cur2[1] == "back":
  193. cur = history.pop()
  194. elif sources.is_video(cur2[1]):
  195. if sources.stream_type(cur2[1]):
  196. stream = util.item()
  197. stream["url"] = cur2[1]
  198. stream["name"] = cur2[0]
  199. streams = [stream]
  200. else:
  201. try:
  202. if not download:
  203. streams = sources.get_streams(cur2[1])
  204. else:
  205. stream = util.item()
  206. stream["url"] = cur2[1]
  207. stream["name"] = cur2[0]
  208. stream["url"] = util.streamproxy_encode2(stream["url"])
  209. print stream["url"]
  210. streams = [stream]
  211. except Exception as e:
  212. print unicode(e)
  213. traceback.print_exc()
  214. streams = []
  215. if streams:
  216. if not download:
  217. util.play_video(streams)
  218. else:
  219. #urlp = util.streamproxy_encode2(streams[0]["url"])
  220. #print urlp
  221. #util.player(urlp)
  222. #Downloader.download_video(streams)
  223. pass
  224. else:
  225. print "**No stream to play - %s "%(
  226. cur2[1])
  227. raw_input("Press any key")
  228. #import os
  229. #os.system('"c:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%s"'%cur2[1])
  230. else:
  231. if "{0}" in cur2[1]:
  232. a = raw_input("Enter value:")
  233. cur2 = (cur2[0],cur2[1].format(a),cur2[2],cur2[3])
  234. history.append(cur)
  235. cur = cur2
  236. try:
  237. content = sources.get_content(cur[1])
  238. except Exception as e:
  239. print unicode(e)
  240. traceback.print_exc()
  241. raw_input("Continue?")