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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. try:
  9. import json
  10. except:
  11. import simplejson as json
  12. import urllib2, urllib
  13. import datetime, re, sys,os
  14. import traceback
  15. from collections import OrderedDict
  16. import ssl
  17. ssl._create_default_https_context = ssl._create_unverified_context
  18. from SourceBase import SourceBase
  19. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  20. headers0 = headers2dict("""
  21. Host: m-api.ustvnow.com
  22. User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 9_2 like Mac OS X) AppleWebKit/601.1 (KHTML, like Gecko) CriOS/47.0.2526.70 Mobile/13C71 Safari/601.1.46
  23. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  24. DNT: 1
  25. Connection: keep-alive
  26. """)
  27. import HTMLParser
  28. h = HTMLParser.HTMLParser()
  29. class Source(SourceBase):
  30. def __init__(self,country="lv",cfg_path=None):
  31. self.name = "ustvnow"
  32. self.title = "USTVNow"
  33. self.img = "http://watch.ustvnow.com/assets/ustvnow/img/ustvnow_og_image.png"
  34. self.desc = "USTVNow kanālu tiešraide"
  35. self.headers = headers0
  36. self.country=country
  37. self.token = ""
  38. cur_directory = os.path.dirname(os.path.abspath(__file__))
  39. if not cfg_path: cfg_path = cur_directory
  40. self.config_file = os.path.join(cfg_path,self.name+".cfg")
  41. self.options = OrderedDict([("user","lietotajs"),("password","parole")])
  42. self.options_read()
  43. def login(self,user="",password=""):
  44. self.options_read()
  45. if not user: user=self.options["user"]
  46. if not password: password = self.options["password"]
  47. headers = headers2dict("""
  48. Host: m-api.ustvnow.com
  49. Accept-Language: en-US,en;q=0.5
  50. User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:46.0) Gecko/20100101 Firefox/46.0
  51. Accept: text/html,application/xhtml+xml,application/xml
  52. Connection: keep-alive
  53. """)
  54. url = "http://m-api.ustvnow.com/iphone/1/live/login?username=%s&password=%s&device=gtv&redir=0"%(user,password)
  55. #url = "http://m-api.ustvnow.com/gtv/1/live/login?username=%s&password=%s&device=gtv&redir=0"%(user,password)
  56. r = self._http_request(url,headers=headers)
  57. if 'success' in r:
  58. self.token = re.search('"token":"([^"]+)',r).group(1)
  59. return True
  60. else:
  61. return False
  62. def get_content(self, data):
  63. print "[ustvnow] get_content:", data
  64. if "::" in data:
  65. data = data.split("::")[1]
  66. path = data.split("?")[0]
  67. clist = path.split("/")[0]
  68. params = data[data.find("?"):] if "?" in data else ""
  69. qs = dict(map(lambda x:x.split("="),re.findall("\w+=\w+",params)))
  70. lang = qs["lang"] if "lang" in qs else self.country
  71. content=[]
  72. content.append(("..return", "back","","Return back"))
  73. if clist=="home":
  74. content.extend([
  75. ("TV live streams", "ustvnow::tvlive","","TV live streams"),
  76. ("Movies", "ustvnow::movies","","Movies (not implemented yet"),
  77. ("Recordings", "ustvnow::recordings","","Recordings (not implemented yet"),
  78. ])
  79. return content
  80. if clist=="movies":
  81. return content
  82. if clist=="recordings":
  83. return content
  84. ### Tiesraides kanalu saraksts ###
  85. elif data=="tvlive":
  86. if not self.token:
  87. if not self.login():
  88. raise Exception("Can not login\nPlease check USTVNow username/password in\n/usr/lib/enigma2/python/Plugins/Extensions/sources/ustvnow.cfg file")
  89. data = "live/channelguide?token=%s"%self.token
  90. self.r = self.call(data)
  91. if not self.r:
  92. return content
  93. for item in self.r["results"]:
  94. if item["order"] == 1:
  95. title = item["stream_code"]
  96. title = h.unescape(title.decode("utf8")).encode("utf8")
  97. img = "http://m-api.ustvnow.com/"+item["prg_img"] #item["img"]
  98. data2 = "live/view?scode=%s&token=%s"%(item["scode"],self.token)
  99. desc = "%s\n%s (+%s')\n%s"%(item["title"],item["event_time"],int(item["actualremainingtime"])/60,item["description"])
  100. content.append((title,self.name+"::"+data2,img,desc))
  101. return content
  102. ### Tiesraides kanāls ###
  103. elif path == "live/view":
  104. url = "http://m-api.ustvnow.com/stream/1/%s"%data
  105. r = self._http_request(url)
  106. if not r:
  107. return ("No stream found %s"%data,"","","No stream found")
  108. r = json.loads(r)
  109. if not "r" in dir(self):
  110. if not self.token:
  111. self.login()
  112. self.r = self.call("live/channelguide?token=%s"%self.token)
  113. if self.r:
  114. ch = qs["scode"]
  115. for item in self.r["results"]:
  116. if item["order"] == 1 and item["scode"] == ch:
  117. title = item["stream_code"]
  118. title = "%s - %s (%s)"%(item["stream_code"],item["title"],item["event_time"])
  119. img = "http://m-api.ustvnow.com/"+item["prg_img"]
  120. data2 = "live/view?scode=%s&token=%s"%(item["scode"],self.token)
  121. desc = "%s\n%s (+%s')\n%s"%(item["title"],item["event_time"],int(item["actualremainingtime"])/60,item["description"])
  122. else:
  123. title = data
  124. data2 = r["stream"]
  125. desc = title
  126. img = "" # img TODO
  127. return (title,data2,img,desc)
  128. def is_video(self,data):
  129. if "::" in data:
  130. data = data.split("::")[1]
  131. if "live/view" in data:
  132. return True
  133. else:
  134. return False
  135. def call(self, data,headers=headers0,lang=""):
  136. if not lang: lang = self.country
  137. url = "http://m-api.ustvnow.com/gtv/1/"+data
  138. content = self._http_request(url)
  139. result = None
  140. if content:
  141. try:
  142. result = json.loads(content)
  143. except Exception, ex:
  144. return None
  145. return result
  146. if __name__ == "__main__":
  147. country= "lv"
  148. c = Source(country)
  149. if len(sys.argv)>1:
  150. data= sys.argv[1]
  151. else:
  152. data = "home"
  153. content = c.get_content(data)
  154. for item in content:
  155. print item
  156. #cat = api.get_categories(country)
  157. #chan = api.get_channels("lv")
  158. #prog = api.get_programs(channel=6400)
  159. #prog = api.get_programs(category=55)
  160. #seas = api.get_seasons(program=6453)
  161. #str = api.get_streams(660243)
  162. #res = api.get_videos(802)
  163. #formats = api.getAllFormats()
  164. #det = api.detailed("1516")
  165. #vid = api.getVideos("13170")
  166. pass