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

ContentSources.py 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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"] = stream
  103. if not "surl" in s or not s["surl"]:
  104. s["surl"] = data
  105. if not "nfo" in s:
  106. s["nfo"]={"tvshow":{"title":s["title"],"thumb":s["img"],"plot":s["desc"]}}
  107. return streams
  108. else:
  109. return []
  110. def stream_type(self,data):
  111. return stream_type(data)
  112. def is_video(self,data):
  113. if self.stream_type(data):
  114. return True
  115. source = data.split("::")[0]
  116. if source in self.plugins:
  117. return self.plugins[source].is_video(data)
  118. else:
  119. return False
  120. def options_read(self,source):
  121. if source in self.plugins:
  122. options = self.plugins[source].options_read()
  123. if options:
  124. return options
  125. else:
  126. return None
  127. else:
  128. return None
  129. def options_write(self,source,options):
  130. if source in self.plugins:
  131. return self.plugins[source].options_write(options)
  132. else:
  133. return None
  134. if __name__ == "__main__":
  135. sources = ContentSources("sources")
  136. if len(sys.argv)>1:
  137. data= sys.argv[1]
  138. else:
  139. data = "config::home"
  140. #options = sources.options_read("ltc")
  141. #print options
  142. history = []
  143. cur = ("Home",data,None,None)
  144. content = sources.get_content(cur[1])
  145. exit_loop = False
  146. while True:
  147. print
  148. for i,item in enumerate(content):
  149. s = "%i: %s - %s %s %s "%(i,item[0],item[1],item[2],item[3])
  150. print s #.encode(sys.stdout.encoding,"replace")
  151. while True:
  152. a = raw_input("Enter numeber, q for exit: ")
  153. if a in ("q","Q","x","X"):
  154. exit_loop = True
  155. print "Exiting"
  156. break
  157. try:
  158. n = int(a)
  159. break
  160. except:
  161. print "Not number!"
  162. if exit_loop: break
  163. cur2 = content[n]
  164. data0 = cur2[1].split("::")[1] if "::" in cur2[1] else cur2[1]
  165. if not data0:
  166. pass
  167. elif cur2[1] == "back":
  168. cur = history.pop()
  169. elif sources.is_video(cur2[1]):
  170. if sources.stream_type(cur2[1]):
  171. stream = util.item()
  172. stream["url"] = cur2[1]
  173. stream["name"] = cur2[0]
  174. streams = [stream]
  175. else:
  176. try:
  177. streams = sources.get_streams(cur2[1])
  178. except Exception as e:
  179. print unicode(e)
  180. traceback.print_exc()
  181. streams = []
  182. if streams:
  183. util.play_video(streams)
  184. else:
  185. print "**No stream to play - %s "%(
  186. cur2[1])
  187. raw_input("Press any key")
  188. #import os
  189. #os.system('"c:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%s"'%cur2[1])
  190. else:
  191. if "{0}" in cur2[1]:
  192. a = raw_input("Enter value:")
  193. cur2 = (cur2[0],cur2[1].format(a),cur2[2],cur2[3])
  194. history.append(cur)
  195. cur = cur2
  196. try:
  197. content = sources.get_content(cur[1])
  198. except Exception as e:
  199. print unicode(e)
  200. raw_input("Continue?")