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

ustvnow.py 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. r = self._http_request(url,headers)
  52. if 'success' in r:
  53. self.token = re.search('"token":"([^"]+)',r).group(1)
  54. return True
  55. else:
  56. return False
  57. def get_content(self, data):
  58. print "[tvdom] get_content:", data
  59. if "::" in data:
  60. data = data.split("::")[1]
  61. path = data.split("?")[0]
  62. clist = path.split("/")[0]
  63. params = data[data.find("?"):] if "?" in data else ""
  64. qs = dict(map(lambda x:x.split("="),re.findall("\w+=\w+",params)))
  65. lang = qs["lang"] if "lang" in qs else self.country
  66. content=[]
  67. content.append(("..return", "back","","Return back"))
  68. if clist=="home":
  69. content.extend([
  70. ("TV live streams", "ustvnow::tvlive","","TV live streams"),
  71. ("Movies", "ustvnow::movies","","Movies (not implemented yet"),
  72. ("Recordings", "ustvnow::recordings","","Recordings (not implemented yet"),
  73. ])
  74. return content
  75. if clist=="movies":
  76. return content
  77. if clist=="recordings":
  78. return content
  79. ### Tiesraides kanalu saraksts ###
  80. elif data=="tvlive":
  81. if not self.token:
  82. if not self.login():
  83. raise Exception("Can not login\nPlease check USTVNow username/password in\n/usr/lib/enigma2/python/Plugins/Extensions/sources/ustvnow.cfg file")
  84. data = "live/channelguide?token=%s"%self.token
  85. self.r = self.call(data)
  86. if not self.r:
  87. return content
  88. for item in self.r["results"]:
  89. if item["order"] == 1:
  90. title = item["stream_code"]
  91. title = h.unescape(title.decode("utf8")).encode("utf8")
  92. img = "http://m-api.ustvnow.com/"+item["prg_img"] #item["img"]
  93. data2 = "live/view?scode=%s&token=%s"%(item["scode"],self.token)
  94. desc = "%s\n%s (+%s')\n%s"%(item["title"],item["event_time"],int(item["actualremainingtime"])/60,item["description"])
  95. content.append((title,self.name+"::"+data2,img,desc))
  96. return content
  97. ### Tiesraides kanāls ###
  98. elif path == "live/view":
  99. url = "http://m-api.ustvnow.com/stream/1/%s"%data
  100. r = self._http_request(url)
  101. if not r:
  102. return ("No stream found %s"%data,"","","No stream found")
  103. r = json.loads(r)
  104. if self.r:
  105. ch = qs["scode"]
  106. for item in self.r["results"]:
  107. if item["order"] == 1 and item["scode"] == ch:
  108. title = item["stream_code"]
  109. title = "%s - %s (%s)"%(item["stream_code"],item["title"],item["event_time"])
  110. img = "http://m-api.ustvnow.com/"+item["prg_img"]
  111. data2 = "live/view?scode=%s&token=%s"%(item["scode"],self.token)
  112. desc = "%s\n%s (+%s')\n%s"%(item["title"],item["event_time"],int(item["actualremainingtime"])/60,item["description"])
  113. else:
  114. title = data
  115. data2 = r["stream"]
  116. desc = title
  117. img = ""
  118. return (title,data2,img,desc)
  119. def is_video(self,data):
  120. if "::" in data:
  121. data = data.split("::")[1]
  122. if "live/view" in data:
  123. return True
  124. else:
  125. return False
  126. def call(self, data,headers=headers0,lang=""):
  127. if not lang: lang = self.country
  128. url = "http://m-api.ustvnow.com/gtv/1/"+data
  129. content = self._http_request(url)
  130. result = None
  131. if content:
  132. try:
  133. result = json.loads(content)
  134. except Exception, ex:
  135. return None
  136. return result
  137. if __name__ == "__main__":
  138. country= "lv"
  139. c = Source(country)
  140. if len(sys.argv)>1:
  141. data= sys.argv[1]
  142. else:
  143. data = "home"
  144. content = c.get_content(data)
  145. for item in content:
  146. print item
  147. #cat = api.get_categories(country)
  148. #chan = api.get_channels("lv")
  149. #prog = api.get_programs(channel=6400)
  150. #prog = api.get_programs(category=55)
  151. #seas = api.get_seasons(program=6453)
  152. #str = api.get_streams(660243)
  153. #res = api.get_videos(802)
  154. #formats = api.getAllFormats()
  155. #det = api.detailed("1516")
  156. #vid = api.getVideos("13170")
  157. pass