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

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. import urllib2, urllib
  9. import datetime, re, sys,os
  10. import requests
  11. try:
  12. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  13. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  14. except:
  15. pass
  16. from collections import OrderedDict
  17. import ConfigParser
  18. try:
  19. import util
  20. except:
  21. parent = os.path.dirname(os.path.abspath(__file__))
  22. parent = os.sep.join(parent.split(os.sep)[:-1])
  23. sys.path.insert(0,parent)
  24. import util
  25. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  26. class SourceBase(object):
  27. """Stream source base class"""
  28. def __init__(self,country="lv"):
  29. self.name = "name"
  30. self.title = "Title"
  31. self.img = ""
  32. self.desc = ""
  33. self.options = OrderedDict()
  34. self.config_file = ""
  35. self.url = "http://www.bbb.com/"
  36. self.headers = headers2dict("""
  37. User-Agent: Mozilla/5.0 (Linux; U; Android 4.4.4; Nexus 5 Build/KTU84P) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
  38. """)
  39. def login(self,user="",password=""):
  40. return False
  41. def logout(self):
  42. return True
  43. def get_content(self,data):
  44. ### To be overriden in child class
  45. return [("..atpakaļ","back",None,"Kļūda, atgriezties atpakaļ")]
  46. def is_video(self,data):
  47. ### To be overriden in child class
  48. return False
  49. def get_streams(self,data):
  50. ### Normally to be overrided in child class
  51. if not self.is_video(data):
  52. return []
  53. content = self.get_content(data)
  54. stream = util.item()
  55. stream["name"] = content[0].encode("utf8") if isinstance(content[0],unicode) else content[0]
  56. stream["url"] = content[1].encode("utf8") if isinstance(content[1],unicode) else content[1]
  57. stream["img"] = content[2].encode("utf8") if isinstance(content[2],unicode) else content[2]
  58. stream["desc"] = content[3].encode("utf8") if isinstance(content[3],unicode) else content[3]
  59. stream["type"] = stream_type(content[1]).encode("utf8")
  60. return[stream]
  61. def get_epg(self,data):
  62. ### Normally to be overrided in child class
  63. return [self.get_info(data)]
  64. def options_read(self):
  65. if not ("options" in dir(self) and self.options): # process options only if self.options defined, self.config_file should be defined too
  66. return None
  67. config = ConfigParser.ConfigParser()
  68. if os.path.exists(self.config_file):
  69. config.read(self.config_file)
  70. self.options = OrderedDict(config.items(self.name))
  71. else:
  72. self.options_write(self.options)
  73. return self.options
  74. def options_write(self,options):
  75. config = ConfigParser.ConfigParser()
  76. config.add_section(self.name)
  77. for k in options.keys():
  78. config.set(self.name, k,options[k])
  79. with open(self.config_file,"w") as f:
  80. config.write(f)
  81. self.options = OrderedDict(config.items(self.name))
  82. def call(self, data,params=None,headers=None,lang=""):
  83. if not headers: headers = self.headers
  84. url = self.url+data
  85. result = self._http_request(url,params,headers=headers)
  86. return result
  87. def call_json(self, data,params=None,headers=None,lang=""):
  88. result = self.call(url,params,headers=headers)
  89. if result:
  90. result = json.loads(content)
  91. return result
  92. else:
  93. raise "No data returned"
  94. def _http_request(self, url,params = None, headers=None):
  95. if not headers:
  96. headers = self.headers if "headers" in dir(self) else headers2dict("User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0")
  97. try:
  98. if params:
  99. r = requests.post(url, data=params, headers=headers,verify=False)
  100. else:
  101. r = requests.get(url, headers=headers,verify=False)
  102. return r.content
  103. except Exception as ex:
  104. if "read" in ex:
  105. content = ex.read()
  106. else:
  107. content = None
  108. return content
  109. @staticmethod
  110. def stream_type(data):
  111. return stream_type(data)
  112. @staticmethod
  113. def parse_data(data):
  114. if "::" in data:
  115. source = data.split("::")[0]
  116. data = data.split("::")[1]
  117. else:
  118. source = ""
  119. path = data.split("?")[0]
  120. plist = path.split("/")
  121. clist = plist[0]
  122. params = data[data.find("?"):] if "?" in data else ""
  123. qs = dict(map(lambda x:x.split("="),re.findall("\w+=\w+",params)))
  124. return source,data,path,plist,clist,params,qs
  125. def stream_type(data):
  126. data = data.lower()
  127. m = re.search(r"^(\w+)://", data)
  128. prefix = m.group(1) if m else ""
  129. if prefix in ("http","https"):
  130. if ".m3u8" in data:
  131. return "hls"
  132. elif ".mpd" in data:
  133. return "dash"
  134. else:
  135. return "http"
  136. else:
  137. return prefix
  138. if __name__ == "__main__":
  139. pass