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

config.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 os.path,re
  9. import collections
  10. from SourceBase import SourceBase
  11. os.path.dirname(os.path.abspath(__file__))
  12. class Source(SourceBase):
  13. def __init__(self, cfg_file="streams.cfg", cfg_file2=None):
  14. self.name = "config"
  15. self.lists = collections.OrderedDict()
  16. self.titles = {}
  17. cur_directory = os.path.dirname(__file__)
  18. self.cfg_path = cur_directory
  19. self.set_streams_file(cfg_file)
  20. print "streams_file=", self.streams_file
  21. try:
  22. self.read_streams()
  23. except Exception as e:
  24. if cfg_file2:
  25. # Try fallback
  26. self.set_streams_file(cfg_file2)
  27. self.read_streams()
  28. else:
  29. raise Exception(e.message)
  30. def set_streams_file(self, cfg_file):
  31. if not ("/" in cfg_file or "\\" in cfg_file): # ja tikai faila nosaukums, tad meklē to source folderi
  32. self.streams_file = os.path.join(self.cfg_path,cfg_file)
  33. else:
  34. self.streams_file = cfg_file
  35. def get_content(self, data):
  36. print "[config] get_content",data
  37. self.read_streams()
  38. if "::" in data:
  39. data = data.split("::")[1]
  40. if not data in self.lists:
  41. return []
  42. return self.lists[data]
  43. def is_video(self,data):
  44. return False
  45. def read_streams(self):
  46. if re.search("(https*|ftps*)://.*", self.streams_file):
  47. import urllib2
  48. try:
  49. sf = urllib2.urlopen(self.streams_file, "r")
  50. except Exception as e:
  51. raise Exception("Can not open streams file %s" % self.streams_file)
  52. #print "Warning: can not open streams file %s, callback to default" % self.streams_file
  53. #self.streams_file = os.path.join(self.cfg_path, "streams.cfg")
  54. #sf = open(self.streams_file, "r")
  55. else:
  56. try:
  57. sf = open(self.streams_file, "r")
  58. except Exception as e:
  59. raise Exception("Can not open streams file %s" % self.streams_file)
  60. for line in sf:
  61. r = re.search("^\[(\w+)\]", line)
  62. if r:
  63. name = r.group(1)
  64. self.lists[name] = []
  65. else:
  66. if line[0] in ("#"): continue
  67. items = line.strip().split("|")
  68. if not items[0]: continue
  69. if len(items)==1:
  70. self.titles[name] = items[0]
  71. else:
  72. if len(items) == 4:
  73. items[3] = items[3].replace("\\n","\n")
  74. self.lists[name].append(items)
  75. sf.close()
  76. def write_streams(self):
  77. if re.search("ftps*://.*", self.streams_file):
  78. mode = "ftp"
  79. elif re.search("https*://.*", self.streams_file):
  80. mode = "http"
  81. print "Warning stream_file http upload no yet implemented"
  82. return
  83. else:
  84. mode = "file"
  85. if mode == "ftp":
  86. #import tempfile
  87. #f = tempfile.NamedTemporaryFile("w", delete=False)
  88. #tmp_file = f.name
  89. import StringIO
  90. f = StringIO.StringIO()
  91. else: # Parasts fails
  92. try:
  93. f = open(self.streams_file, "w")
  94. except Exception as e:
  95. raise Exception("Can not open streams file %s" % self.streams_file)
  96. for l in self.lists.keys():
  97. f.write("[%s]\n"%l)
  98. t = self.get_title(l)
  99. if t<>l:
  100. f.write("%s\n"%t)
  101. for item in self.lists[l]:
  102. f.write("%s|%s|%s|%s\n"%(item[0].replace("\n",""),item[1],item[2],item[3].replace("\n","\\n")))
  103. f.write("\n")
  104. if mode == "ftp":
  105. import ftplib, urlparse
  106. p = urlparse.urlparse(self.streams_file)
  107. try:
  108. ftp = ftplib.FTP(p.hostname)
  109. ftp.login(p.username, p.password)
  110. f.seek(0)
  111. ftp.storbinary("STOR "+p.path, f)
  112. ftp.close()
  113. except Exception as e:
  114. print e.message
  115. else:
  116. f.close()
  117. def get_lists(self):
  118. return self.lists.keys()
  119. def get_list_items(self,name):
  120. return self.lists[name]
  121. def get_title(self,name):
  122. if name in self.titles:
  123. return self.titles[name]
  124. else:
  125. return name
  126. def add_list(self,name):
  127. if not name in self.lists.keys():
  128. self.lists[name] = []
  129. def del_list(self,name):
  130. if name in self.lists.keys():
  131. del self.lists[name]
  132. def add_item(self,name,item,pos=None):
  133. if name in self.lists.keys():
  134. if pos==None:
  135. self.lists[name].append(item)
  136. else:
  137. self.lists[name].insert(pos,item)
  138. def del_item(self, name, pos):
  139. self.lists[name].pop(pos)
  140. def replace_item(self, name, item, pos):
  141. self.lists[name][pos]=item
  142. def move_item(self, name, pos, pos2):
  143. # l.insert(newindex, l.pop(oldindex))
  144. if pos2 == None:
  145. self.lists[name].append(self.lists[name].pop(pos))
  146. elif pos2 < pos:
  147. self.lists[name].insert(pos2, self.lists[name].pop(pos))
  148. elif pos2 > pos:
  149. self.lists[name].insert(pos2-1, self.lists[name].pop(pos))
  150. if __name__ == "__main__":
  151. source = Source(cfg_file="streams.cfg")
  152. source.read_streams()
  153. source.write_streams()
  154. import sys
  155. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  156. import run
  157. source = Source()
  158. data= sys.argv[1] if len(sys.argv)>1 else source.name+"::home"
  159. run.run(source, data)
  160. sys.exit()
  161. #c.del_item("home",0)
  162. #c.del_list("favorites")
  163. #c.add_item("home",("My Streams","config::favorites","","Mani saglabātie TV kanāli un video"),0)
  164. #c.replace_item("home",("My Streams","config::my_streams","default","Mani saglabātie TV kanāli un video"),0)
  165. #c.add_list("favorites")
  166. #c.add_item("favorites",("..return","back","back.png","Atgriezties atpakaļ"))
  167. #c.add_item("favorites",("LTV1","http://streamltv.cloudy.services/ltv/LTV02.smil/playlist.m3u8","picons/latvia1.png", "Latvijas televīzijas 1.kanāls"))
  168. #c.write_streams()
  169. #for item in content: print item