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

SourceBase.py 5.4KB

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. from requests.packages.urllib3.exceptions import InsecureRequestWarning
  12. requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
  13. from collections import OrderedDict
  14. import ConfigParser
  15. try:
  16. import util
  17. except:
  18. parent = os.path.dirname(os.path.abspath(__file__))
  19. parent = os.sep.join(parent.split(os.sep)[:-1])
  20. sys.path.insert(0,parent)
  21. import util
  22. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  23. class SourceBase(object):
  24. """Stream source base class"""
  25. def __init__(self,country="lv"):
  26. self.name = "name"
  27. self.title = "Title"
  28. self.img = ""
  29. self.desc = ""
  30. self.options = OrderedDict()
  31. self.config_file = ""
  32. self.url = "http://www.bbb.com/"
  33. self.headers = headers2dict("""
  34. 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
  35. """)
  36. def login(self,user="",password=""):
  37. return False
  38. def logout(self):
  39. return True
  40. def get_content(self,data):
  41. ### To be overriden in child class
  42. return [("..atpakaļ","back",None,"Kļūda, atgriezties atpakaļ")]
  43. def is_video(self,data):
  44. ### To be overriden in child class
  45. return False
  46. def get_streams(self,data):
  47. ### Normally to be overrided in child class
  48. if not self.is_video(data):
  49. return []
  50. content = self.get_content(data)
  51. stream = util.item()
  52. stream["name"] = content[0].encode("utf8") if isinstance(content[0],unicode) else content[0]
  53. stream["url"] = content[1].encode("utf8") if isinstance(content[1],unicode) else content[1]
  54. stream["img"] = content[2].encode("utf8") if isinstance(content[2],unicode) else content[2]
  55. stream["desc"] = content[3].encode("utf8") if isinstance(content[3],unicode) else content[3]
  56. stream["type"] = stream_type(content[1]).encode("utf8")
  57. return[stream]
  58. def get_epg(self,data):
  59. ### Normally to be overrided in child class
  60. return [self.get_info(data)]
  61. def get_info(self,data):
  62. streams = self.get_streams(data)
  63. return streams[0] if streams else {}
  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. if "::" in data:
  127. data = data.split("::")[1]
  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") and ".m3u8" in data:
  132. return "hls"
  133. elif prefix == "http":
  134. return "http"
  135. else:
  136. return prefix
  137. if __name__ == "__main__":
  138. pass