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

ContentSources.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, cfg_file="streams.cfg"):
  18. self.plugins = {}
  19. self.error_content = [("..atpakaļ", "back", "back.png", "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. if mod.__name__ == "config":
  35. self.plugins[fname] = mod.Source(cfg_file=cfg_file)
  36. else:
  37. self.plugins[fname] = mod.Source()
  38. print fname+ " imported"
  39. else:
  40. pass
  41. #print fname+ "skipped"
  42. sys.path.pop(0)
  43. if not "config" in self.plugins:
  44. raise Exception("Problem importing content sources")
  45. cfg = self.plugins["config"]
  46. for pl in self.plugins.keys():
  47. found = False
  48. for lst in cfg.get_lists():
  49. for item in cfg.lists[lst]:
  50. if item[1].split("::")[0]==pl:
  51. found = True
  52. break
  53. if found: break
  54. if not found:
  55. title = self.plugins[pl].title if "title" in dir(self.plugins[pl]) else pl
  56. img = self.plugins[pl].img if "img" in dir(self.plugins[pl]) else ""
  57. desc = self.plugins[pl].desc if "desc" in dir(self.plugins[pl]) else title
  58. cfg.add_item("home",(title,"%s::home"%pl,img,desc))
  59. cfg.write_streams()
  60. def get_content(self,data):
  61. source = data.split("::")[0]
  62. if source in self.plugins:
  63. content0 = self.plugins[source].get_content(data)
  64. if content0:
  65. content = []
  66. if isinstance(content0,list):
  67. for i,item in enumerate(content0):
  68. source2 = item[1].split("::")[0]
  69. if not (source2 == "back" or item[1].startswith("http") or item[1].startswith("rtmp")):
  70. if source2 not in self.plugins or (not show_hidden and "hidden" in dir(self.plugins[source2]) and self.plugins[source2].hidden):
  71. continue
  72. item2=[]
  73. for el in item:
  74. if isinstance(el,unicode):
  75. el = el.encode("utf8")
  76. el = util.unescape(el)
  77. item2.append(el)
  78. content.append(tuple(item2))
  79. else:
  80. item2=[]
  81. for el in content0:
  82. if isinstance(el,unicode):
  83. el = el.encode("utf8")
  84. item2.append(el)
  85. content=tuple(item2)
  86. return content
  87. else:
  88. return self.error_content
  89. else:
  90. return self.error_content
  91. def get_streams(self,data):
  92. if stream_type(data):
  93. if "::" in data:
  94. data = data.split("::")[1]
  95. content = self.get_content(data)
  96. stream = util.item()
  97. stream["name"] = data
  98. stream["url"] = data
  99. stream["type"] = stream_type(data)
  100. #stream["img"] = ""
  101. #stream["desc"] = ""
  102. return[stream]
  103. if not self.is_video(data):
  104. return []
  105. source = data.split("::")[0]
  106. if source in self.plugins:
  107. streams = self.plugins[source].get_streams(data)
  108. for s in streams:
  109. for k in s:
  110. if isinstance(s[k],unicode):
  111. s[k] = s[k].encode("utf8")
  112. if not "resolver" in s:
  113. s["resolver"] = source
  114. if not "surl" in s or not s["surl"]:
  115. s["surl"] = data
  116. if not "nfo" in s:
  117. s["nfo"]={"movie":{"title":s["name"],"thumb":s["img"],"plot":s["desc"]}}
  118. return streams
  119. else:
  120. return []
  121. def get_info(self,data):
  122. nfo = {}
  123. if self.is_video(data):
  124. source = data.split("::")[0]
  125. if source in self.plugins:
  126. if "get_info" in dir(self.plugins[source]):
  127. nfo = self.plugins[source].get_info(data)
  128. else:
  129. streams = self.get_streams(data)
  130. if streams and "nfo" in streams[0] and streams[0]["nfo"]:
  131. nfo = streams[0]["nfo"]
  132. else:
  133. nfo = {"movie": {"title": current[0], "thumb": self.current[2], "plot": self.current[3]}}
  134. else:
  135. pass # TODO create nfo for listing
  136. return nfo
  137. def stream_type(self,data):
  138. return stream_type(data)
  139. def is_video(self,data):
  140. if self.stream_type(data):
  141. return True
  142. source = data.split("::")[0]
  143. if source in self.plugins:
  144. return self.plugins[source].is_video(data)
  145. else:
  146. return False
  147. def options_read(self,source):
  148. if source in self.plugins:
  149. options = self.plugins[source].options_read()
  150. if options:
  151. return options
  152. else:
  153. return None
  154. else:
  155. return None
  156. def options_write(self,source,options):
  157. if source in self.plugins:
  158. return self.plugins[source].options_write(options)
  159. else:
  160. return None
  161. if __name__ == "__main__":
  162. from run import run
  163. show_hidden = False
  164. data= sys.argv[1] if len(sys.argv) > 1 else "config::home"
  165. cfg_file = sys.argv[2] if len(sys.argv) > 2 else "streams.cfg"
  166. sources = ContentSources("sources", cfg_file=cfg_file)
  167. run(sources, data)