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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 options_read(self):
  62. if not ("options" in dir(self) and self.options): # process options only if self.options defined, self.config_file should be defined too
  63. return None
  64. config = ConfigParser.ConfigParser()
  65. if os.path.exists(self.config_file):
  66. config.read(self.config_file)
  67. self.options = OrderedDict(config.items(self.name))
  68. else:
  69. self.options_write(self.options)
  70. return self.options
  71. def options_write(self,options):
  72. config = ConfigParser.ConfigParser()
  73. config.add_section(self.name)
  74. for k in options.keys():
  75. config.set(self.name, k,options[k])
  76. with open(self.config_file,"w") as f:
  77. config.write(f)
  78. self.options = OrderedDict(config.items(self.name))
  79. def call(self, data,params=None,headers=None,lang=""):
  80. if not headers: headers = self.headers
  81. url = self.url+data
  82. result = self._http_request(url,params,headers=headers)
  83. return result
  84. def call_json(self, data,params=None,headers=None,lang=""):
  85. result = self.call(url,params,headers=headers)
  86. if result:
  87. result = json.loads(content)
  88. return result
  89. else:
  90. raise "No data returned"
  91. def _http_request(self, url,params = None, headers=None):
  92. if not headers:
  93. 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")
  94. try:
  95. if params:
  96. r = requests.post(url, data=params, headers=headers,verify=False)
  97. else:
  98. r = requests.get(url, headers=headers,verify=False)
  99. return r.content
  100. except Exception as ex:
  101. if "read" in ex:
  102. content = ex.read()
  103. else:
  104. content = None
  105. return content
  106. @staticmethod
  107. def stream_type(data):
  108. return stream_type(data)
  109. @staticmethod
  110. def parse_data(data):
  111. if "::" in data:
  112. source = data.split("::")[0]
  113. data = data.split("::")[1]
  114. else:
  115. source = ""
  116. path = data.split("?")[0]
  117. plist = path.split("/")
  118. clist = plist[0]
  119. params = data[data.find("?"):] if "?" in data else ""
  120. qs = dict(map(lambda x:x.split("="),re.findall("\w+=\w+",params)))
  121. return source,data,path,plist,clist,params,qs
  122. def stream_type(data):
  123. if "::" in data:
  124. data = data.split("::")[1]
  125. data = data.lower()
  126. m = re.search(r"^(\w+)://", data)
  127. prefix = m.group(1) if m else ""
  128. if prefix in ("http","https") and ".m3u8" in data:
  129. return "hls"
  130. elif prefix == "http":
  131. return "http"
  132. else:
  133. return prefix
  134. if __name__ == "__main__":
  135. pass