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

ContentSources.py 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. sys.path.insert(0,os.path.dirname(os.path.abspath(__file__)))
  10. from sources.SourceBase import stream_type
  11. import util
  12. stream0 = {'name': '', 'url': '', 'quality': '???', 'surl': '', 'subs': '', 'headers': {}, "desc":"", "img":"", "lang": "", "type": ""}
  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. fname, ext = os.path.splitext(f)
  22. if fname == "__init__": continue
  23. if ext == '.py':
  24. mod = __import__(fname)
  25. reload(mod)
  26. if "Source" in dir(mod):
  27. self.plugins[fname] = mod.Source()
  28. sys.path.pop(0)
  29. cfg = self.plugins["config"]
  30. for pl in self.plugins.keys():
  31. found = False
  32. for lst in cfg.get_lists():
  33. for item in cfg.lists[lst]:
  34. if item[1].split("::")[0]==pl:
  35. found = True
  36. break
  37. if found: break
  38. if not found:
  39. title = self.plugins[pl].title if "title" in dir(self.plugins[pl]) else pl
  40. img = self.plugins[pl].img if "img" in dir(self.plugins[pl]) else ""
  41. desc = self.plugins[pl].desc if "desc" in dir(self.plugins[pl]) else title
  42. cfg.add_item("home",(title,"%s::home"%pl,img,desc))
  43. cfg.write_streams()
  44. def get_content(self,data):
  45. source = data.split("::")[0]
  46. if source in self.plugins:
  47. content = self.plugins[source].get_content(data)
  48. if content:
  49. if isinstance(content,list):
  50. for i,item in enumerate(content):
  51. item2=[]
  52. for el in item:
  53. if isinstance(el,unicode):
  54. el = el.encode("utf8")
  55. item2.append(el)
  56. content[i]=tuple(item2)
  57. else:
  58. item2=[]
  59. for el in content:
  60. if isinstance(el,unicode):
  61. el = el.encode("utf8")
  62. item2.append(el)
  63. content=tuple(item2)
  64. return content
  65. else:
  66. return self.error_content
  67. else:
  68. return self.error_content
  69. def get_streams(self,data):
  70. if stream_type(data):
  71. if "::" in data:
  72. data = data.split("::")[1]
  73. content = self.get_content(data)
  74. stream = stream0.copy()
  75. stream["name"] = data
  76. stream["url"] = data
  77. stream["type"] = stream_type(data)
  78. #stream["img"] = ""
  79. #stream["desc"] = ""
  80. return[stream]
  81. if not self.is_video(data):
  82. return []
  83. source = data.split("::")[0]
  84. if source in self.plugins:
  85. streams = self.plugins[source].get_streams(data)
  86. for s in streams:
  87. for k in s:
  88. if isinstance(s[k],unicode):
  89. s[k] = s[k].encode("utf8")
  90. return streams
  91. else:
  92. return []
  93. def stream_type(self,data):
  94. return stream_type(data)
  95. def is_video(self,data):
  96. if self.stream_type(data):
  97. return True
  98. source = data.split("::")[0]
  99. if source in self.plugins:
  100. return self.plugins[source].is_video(data)
  101. else:
  102. return False
  103. def options_read(self,source):
  104. if source in self.plugins:
  105. options = self.plugins[source].options_read()
  106. if options:
  107. return options
  108. else:
  109. return None
  110. else:
  111. return None
  112. def options_write(self,source,options):
  113. if source in self.plugins:
  114. return self.plugins[source].options_write(options)
  115. else:
  116. return None
  117. if __name__ == "__main__":
  118. sources = ContentSources("sources")
  119. if len(sys.argv)>1:
  120. data= sys.argv[1]
  121. else:
  122. data = "config::home"
  123. #options = sources.options_read("ltc")
  124. #print options
  125. history = []
  126. cur = ("Home",data,None,None)
  127. content = sources.get_content(cur[1])
  128. exit_loop = False
  129. while True:
  130. print
  131. for i,item in enumerate(content):
  132. s = "%i: %s - %s %s %s "%(i+1,item[0],item[1],item[2],item[3])
  133. print s #.encode(sys.stdout.encoding,"replace")
  134. while True:
  135. a = raw_input("Enter numeber, q for exit: ")
  136. if a in ("q","Q","x","X"):
  137. exit_loop = True
  138. print "Exiting"
  139. break
  140. try:
  141. n = int(a)
  142. break
  143. except:
  144. print "Not number!"
  145. if exit_loop: break
  146. cur2 = content[n-1]
  147. data0 = cur2[1].split("::")[1] if "::" in cur2[1] else cur2[1]
  148. if not data0:
  149. pass
  150. elif cur2[1] == "back":
  151. cur = history.pop()
  152. elif sources.is_video(cur2[1]):
  153. if sources.stream_type(cur2[1]):
  154. stream = stream0.copy()
  155. stream["url"] = cur2[1]
  156. stream["name"] = cur2[0]
  157. streams = [stream]
  158. else:
  159. streams = sources.get_streams(cur2[1])
  160. if streams:
  161. util.play_video(streams)
  162. else:
  163. print "**No stream to play - %s "%(cur2[1])
  164. raw_input("Press any key")
  165. #import os
  166. #os.system('"c:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%s"'%cur2[1])
  167. else:
  168. if "{0}" in cur2[1]:
  169. a = raw_input("Enter value:")
  170. cur2 = (cur2[0],cur2[1].format(a),cur2[2],cur2[3])
  171. history.append(cur)
  172. cur = cur2
  173. content = sources.get_content(cur[1])