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

ustvnow.py 6.8KB

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