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

ContentSources.py 7.9KB

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