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

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