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

ustvnow.py 7.0KB

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