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

ustvnow.py 6.6KB

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