Kodi plugin to to play various online streams (mostly Latvian)

ContentSources.py 7.1KB

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