Kodi plugin to to play various online streams (mostly Latvian)

SourceBase.py 5.0KB

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