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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. try:
  9. import json
  10. except:
  11. import simplejson as json
  12. import requests
  13. import datetime, re, sys,os
  14. from collections import OrderedDict
  15. import ssl
  16. if "_create_unverified_context" in dir(ssl):
  17. ssl._create_default_https_context = ssl._create_unverified_context
  18. from SourceBase import SourceBase
  19. import resolver
  20. try:
  21. import util
  22. except:
  23. sys.path.insert(0,'..')
  24. import util
  25. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  26. import HTMLParser
  27. h = HTMLParser.HTMLParser()
  28. class Source(SourceBase):
  29. def __init__(self, country="lv",cfg_path=None):
  30. self.name = "enigma2"
  31. self.title = "Engima2"
  32. self.img = "enigma2.png"
  33. self.desc = "Get streams from engima2 sat receiver"
  34. self.headers = headers2dict("""
  35. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  36. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  37. Accept-Language: en-US,en;q=0.5
  38. """)
  39. #self.url = "http://login.tvplayhome.lt:8081/sb/public/"
  40. cur_directory = os.path.dirname(os.path.abspath(__file__))
  41. if not cfg_path: cfg_path = cur_directory
  42. self.config_file = os.path.join(cfg_path,self.name+".cfg")
  43. self.options = OrderedDict([("host", "localhost"), ("user",""),("password","")])
  44. self.options_read()
  45. if self.options["user"]:
  46. self.url = "http://%s/api/" % (self.options["host"])
  47. else:
  48. self.url = "http://%s:%s@%s/api/" % (self.options["user"], self.options["password"],self.options["host"])
  49. ######### Entry point ########
  50. def get_content(self, data):
  51. print "[enigma2] get_content:", data
  52. source, data, path, plist, clist, params, qs = self.parse_data(data)
  53. content = []
  54. content.append(("..return", "back","back.png","Return back"))
  55. if clist=="home":
  56. r = self.call("bouquets?stype=tv")
  57. content.append(("Current stream", self.name+"::"+"streamcurrent.m3u", self.img, "Current receiver stream"))
  58. for item in r["bouquets"]:
  59. title = item[1]
  60. data2 = "getservices?sRef=%s" % item[0]
  61. #data2 = "epgbouquet?bRef=%s" % item[1]
  62. content.append((title, self.name+"::"+data2, self.img, title))
  63. return content
  64. elif clist == "getservices":
  65. r = self.call(data)
  66. data3 = data.replace("getservices?sRef", "epgbouquet?bRef")
  67. r2 = self.call(data3)
  68. for item in r["services"]:
  69. title = item["servicename"]
  70. data2 = "stream.m3u?ref=%s&name=%s" % (item["servicereference"], title)
  71. img = self.img
  72. desc = title
  73. for item2 in r2["events"]:
  74. if item2["sref"] == item["servicereference"]:
  75. title, img, desc = self.get_title_desc(item2)
  76. break
  77. content.append((title,self.name+"::"+data2, img,desc))
  78. return content
  79. ### kaut kas neparedzets ###
  80. else:
  81. return content
  82. def get_title_desc(self, event):
  83. t1 = event["begin_timestamp"]
  84. t2 = event["begin_timestamp"] + event["duration_sec"]
  85. t1 = datetime.datetime.fromtimestamp(t1).strftime("%H:%M")
  86. t2 = datetime.datetime.fromtimestamp(t2).strftime("%H:%M")
  87. title = "%s - %s []%s-%s]" % (event["sname"], event["title"], t1, t2)
  88. desc = event["shortdesc"]
  89. if event["longdesc"]:
  90. desc += "\n" + event["longdesc"]
  91. img = event["sref"][:-1].replace(":", "_") + ".png"
  92. img = self.url.replace("/api/", "/picon/") + img
  93. return title, img, desc
  94. def is_video(self,data):
  95. source,data,path,plist,clist,params,qs = self.parse_data(data)
  96. if clist in ("stream.m3u", "streamcurrent.m3u"):
  97. return True
  98. else:
  99. return False
  100. def call(self, data,params=None,headers=None,lang=""):
  101. if not headers: headers = self.headers
  102. url = self.url+data
  103. r = requests.get(url,headers = headers)
  104. result = json.loads(r.content)
  105. #result = self._http_request(url,params,headers=headers)
  106. return result
  107. def get_streams(self,data):
  108. print "[enigma2] get_streams:", data
  109. if not self.is_video(data):
  110. return []
  111. source,data,path,plist,clist,params,qs = self.parse_data(data)
  112. r = self._http_request(self.url+data)
  113. m = re.search("(http(s)*:.+)", r)
  114. if not m:
  115. raise Exception("No streams found!")
  116. data2 = m.group(1)
  117. if "stream.m3u" in data:
  118. data3 = data.replace("stream.m3u?ref=","epgservicenow?sRef=")
  119. r2 = self.call(data3)
  120. event = r2["events"][0]
  121. else:
  122. data3 = "getcurrent"
  123. r2 = self.call(data3)
  124. event = r2["now"]
  125. title, img, desc = self.get_title_desc(event)
  126. stream = util.item()
  127. stream["name"] = title
  128. stream["url"] = data2
  129. stream["img"] = img
  130. stream["desc"] = desc
  131. stream["resolver"] = "enigma2"
  132. return [stream]
  133. if __name__ == "__main__":
  134. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  135. import run
  136. source = Source()
  137. data= sys.argv[1] if len(sys.argv)>1 else source.name+"::home"
  138. if len(sys.argv) > 2:
  139. run.run_cli(source, data)
  140. else:
  141. run.run(source, data)
  142. sys.exit()