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

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