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

config.py 6.5KB

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