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

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