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

ContentSources.py 6.6KB

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