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

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