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

SourceBase.py 5.3KB

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