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

SourceBase.py 5.4KB

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