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

ContentSources.py 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. item2.append(el)
  77. content.append(tuple(item2))
  78. else:
  79. item2=[]
  80. for el in content0:
  81. if isinstance(el,unicode):
  82. el = el.encode("utf8")
  83. item2.append(el)
  84. content=tuple(item2)
  85. return content
  86. else:
  87. return self.error_content
  88. else:
  89. return self.error_content
  90. def get_streams(self,data):
  91. if stream_type(data):
  92. if "::" in data:
  93. data = data.split("::")[1]
  94. content = self.get_content(data)
  95. stream = util.item()
  96. stream["name"] = data
  97. stream["url"] = data
  98. stream["type"] = stream_type(data)
  99. #stream["img"] = ""
  100. #stream["desc"] = ""
  101. return[stream]
  102. if not self.is_video(data):
  103. return []
  104. source = data.split("::")[0]
  105. if source in self.plugins:
  106. streams = self.plugins[source].get_streams(data)
  107. for s in streams:
  108. for k in s:
  109. if isinstance(s[k],unicode):
  110. s[k] = s[k].encode("utf8")
  111. if not "resolver" in s:
  112. s["resolver"] = source
  113. if not "surl" in s or not s["surl"]:
  114. s["surl"] = data
  115. if not "nfo" in s:
  116. s["nfo"]={"movie":{"title":s["name"],"thumb":s["img"],"plot":s["desc"]}}
  117. return streams
  118. else:
  119. return []
  120. def get_info(self,data):
  121. nfo = {}
  122. if self.is_video(data):
  123. source = data.split("::")[0]
  124. if source in self.plugins:
  125. if "get_info" in dir(self.plugins[source]):
  126. nfo = self.plugins[source].get_info(data)
  127. else:
  128. streams = self.get_streams(data)
  129. if streams and "nfo" in streams[0] and streams[0]["nfo"]:
  130. nfo = streams[0]["nfo"]
  131. else:
  132. nfo = {"movie": {"title": current[0], "thumb": self.current[2], "plot": self.current[3]}}
  133. else:
  134. pass # TODO create nfo for listing
  135. return nfo
  136. def stream_type(self,data):
  137. return stream_type(data)
  138. def is_video(self,data):
  139. if self.stream_type(data):
  140. return True
  141. source = data.split("::")[0]
  142. if source in self.plugins:
  143. return self.plugins[source].is_video(data)
  144. else:
  145. return False
  146. def options_read(self,source):
  147. if source in self.plugins:
  148. options = self.plugins[source].options_read()
  149. if options:
  150. return options
  151. else:
  152. return None
  153. else:
  154. return None
  155. def options_write(self,source,options):
  156. if source in self.plugins:
  157. return self.plugins[source].options_write(options)
  158. else:
  159. return None
  160. if __name__ == "__main__":
  161. from run import run
  162. show_hidden = False
  163. data= sys.argv[1] if len(sys.argv) > 1 else "config::home"
  164. cfg_file = sys.argv[2] if len(sys.argv) > 2 else "streams.cfg"
  165. sources = ContentSources("sources", cfg_file=cfg_file)
  166. run(sources, data)