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

ContentSources.py 8.7KB

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