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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. # coding: utf-8
  2. import sys, os, os.path, re, sys
  3. import urllib,urllib2
  4. from xml.sax.saxutils import unescape,escape
  5. from urllib import quote, unquote
  6. user_agent = "Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; da-dk) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3"
  7. headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()])
  8. ########################################################################
  9. class LattelecomTV(object):
  10. """www.lattelecom.tv video server handling object"""
  11. #----------------------------------------------------------------------
  12. def __init__(self):
  13. self.name = "ltc"
  14. self.title = "lattelecom.tv"
  15. self.headers = headers2dict("""
  16. Host: m.lattelecom.tv
  17. User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile Safari/600.1.4
  18. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  19. Accept-Language: en-US,en;q=0.5
  20. DNT: 1
  21. Connection: keep-alive""")
  22. #----------------------------------------------------------------------
  23. def login(self,user,password):
  24. """Login in to site, create session cookies"""
  25. url0 = "https://m.lattelecom.tv"
  26. class NoRedirectHandler(urllib2.HTTPRedirectHandler):
  27. def http_error_302(self, req, fp, code, msg, headers):
  28. infourl = urllib.addinfourl(fp, headers, req.get_full_url())
  29. infourl.status = code
  30. infourl.code = code
  31. return infourl
  32. http_error_300 = http_error_302
  33. http_error_301 = http_error_302
  34. http_error_303 = http_error_302
  35. http_error_307 = http_error_302
  36. # Dabūjam sesijas id un url_gif, kas redirektējas uz auth_url
  37. headers = headers2dict("""
  38. Host: m.lattelecom.tv
  39. User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile Safari/600.1.4
  40. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  41. Accept-Language: en-US,en;q=0.5
  42. DNT: 1
  43. Connection: keep-alive
  44. """)
  45. response = urllib2.urlopen(urllib2.Request(url0+"/authorization", headers=headers))
  46. session_id = response.headers["set-cookie"].split(";")[0]
  47. html = response.read()
  48. url_gif = url0 + re.search('(/auth/\d+.gif)', html).group(1)
  49. # Dabūtjam auth_url
  50. headers = headers2dict("""
  51. Host: m.lattelecom.tv
  52. User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile Safari/600.1.4
  53. Accept: image/png,image/*;q=0.8,*/*;q=0.5
  54. Accept-Language: en-US,en;q=0.5
  55. Accept-Encoding: gzip, deflate
  56. DNT: 1
  57. Referer: https://m.lattelecom.tv/authorization
  58. Connection: keep-alive
  59. """)
  60. headers["Cookie"] = session_id
  61. urllib2.install_opener(urllib2.build_opener(NoRedirectHandler()))
  62. response = urllib2.urlopen(urllib2.Request(url_gif, headers=headers))
  63. if response.code == 302:
  64. url_auth = response.headers["location"]
  65. else:
  66. self.error = u"auth.gif nenostrādāja"
  67. #raise u"auth.gif nenostrādāja"
  68. return False
  69. # Pierakstāmies iekš auth.lattelecom.lv
  70. headers = headers2dict("""
  71. Host: auth.lattelecom.lv
  72. User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
  73. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  74. Accept-Language: en-US,en;q=0.5
  75. Accept-Encoding: gzip, deflate
  76. DNT: 1
  77. Referer: http://m.lattelecom.tv/authorization
  78. Connection: keep-alive
  79. """)
  80. response = urllib2.urlopen(urllib2.Request(url_auth, headers=headers))
  81. if not response.code == 302:
  82. self.error = u"pierakstīšanās auth.lattelecom.lv nenostrādāja"
  83. return False
  84. #raise "pierakstīšanās auth.lattelecom.lv nenostrādāja"
  85. # nolasām auth_gif
  86. ## headers = headers2dict("""
  87. ## Host: m.lattelecom.tv
  88. ## User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
  89. ## Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  90. ## Accept-Language: en-US,en;q=0.5
  91. ## Accept-Encoding: gzip, deflate
  92. ## DNT: 1
  93. ## Referer: http://m.lattelecom.tv/authorization
  94. ## """)
  95. ## headers["Cookie"] = session_id
  96. ## response = urllib2.urlopen(urllib2.Request(url_gif, headers=headers))
  97. ## if not response.code == 200:
  98. ## raise u"kļūda lasot auth_gif"
  99. # Mēģinam ielogoties
  100. headers = headers2dict("""
  101. Host: m.lattelecom.tv
  102. User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
  103. Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  104. Accept-Language: en-US,en;q=0.5
  105. Accept-Encoding: deflate
  106. DNT: 1
  107. Referer: http://m.lattelecom.tv/authorization
  108. Connection: keep-alive
  109. Content-Type: application/x-www-form-urlencoded""")
  110. headers["Cookie"] = session_id
  111. data = "login=yes&email=%s&passw=%s"%(user,password)
  112. req = urllib2.Request(url0+"/authorization", data, headers)
  113. response = urllib2.urlopen(req)
  114. #with open("auth.htm","w") as f: f.write(response.read())
  115. if not response.code == 302:
  116. self.error = u"kļūda ielogojoties"
  117. #raise u"kļūda ielogojoties"
  118. return False
  119. mobtv = response.headers["set-cookie"].split(";")[0]
  120. if not mobtv.split("=")[0]=="mobtv_cache":
  121. #raise "nav mobtv cookie!"
  122. self.error = "nav mobtv cookie!"
  123. return False
  124. # atveram /account
  125. ## headers = headers2dict("""
  126. ## Host: m.lattelecom.tv
  127. ## User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
  128. ## Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  129. ## Accept-Language: en-US,en;q=0.5
  130. ## Accept-Encoding: deflate
  131. ## DNT: 1
  132. ## Referer: http://m.lattelecom.tv/authorization
  133. ## Connection: keep-alive """)
  134. ## headers["Cookie"] = "%s; %s"%(session_id,mobtv)
  135. ## response = urllib2.urlopen(urllib2.Request(url0+"/account", headers=headers))
  136. ## if not response.code == 302:
  137. ## raise u"kļūda atverot /account"
  138. # atveram /profils
  139. ## headers = headers2dict("""
  140. ## Host: m.lattelecom.tv
  141. ## User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7
  142. ## Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  143. ## Accept-Language: en-US,en;q=0.5
  144. ## Accept-Encoding: deflate
  145. ## DNT: 1
  146. ## Referer: http://m.lattelecom.tv/authorization
  147. ## Connection: keep-alive
  148. ## """)
  149. ## headers["Cookie"] = "%s; %s"%(session_id,mobtv)
  150. ## response = urllib2.urlopen(urllib2.Request(url0+"/profils", headers=headers))
  151. ## if not response.code == 200:
  152. ## raise u"kļūda atverot /profils"
  153. ## with open("profils.htm","w") as f: f.write(response.read())
  154. self.mobtv_cache = mobtv
  155. self.session_id = session_id
  156. self.error = ""
  157. return True
  158. #----------------------------------------------------------------------
  159. def get_tv_channels(self,):
  160. """Get tv channels list"""
  161. url = "https://m.lattelecom.tv/tiesraide"
  162. response = urllib2.urlopen(urllib2.Request(url, headers=self.headers))
  163. html = response.read()
  164. channels = re.findall(r'(?si)data-url="/tiesraide/(.*?)"><div title="(.*?)" class="channel-placeholder" style="width: 131px; height: 131px; background-image:url\(/media/imse/105x105s/channels/(.*?)\)', html)
  165. for i in range(len(channels)):
  166. channels[i]=[channels[i][0],channels[i][1],"https://m.lattelecom.tv/media/imse/100x100s/channels/"+channels[i][2]]
  167. return channels
  168. #----------------------------------------------------------------------
  169. def get_tv_url(self,video_id):
  170. """Get m3u8 url for given live tv channel"""
  171. url = "https://m.lattelecom.tv/tiesraide/%s"%video_id
  172. headers = self.headers
  173. headers["Cookie"] = "%s; %s; _hjIncludedInSample=0; MobBitr=1; MobRentBitr=hq; MobRentLang=lv;"%(self.session_id,self.mobtv_cache)
  174. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  175. html = response.read()
  176. m3u8 = re.search('x-mpegURL" src="(.*?)"', html).group(1) if "x-mpegURL" in html else ""
  177. return unescape(m3u8)
  178. #----------------------------------------------------------------------
  179. def get_noma_url(self,video_id,quality="hq",language="lv"):
  180. """Get m3u8 url for given rental video"""
  181. url = "https://m.lattelecom.tv/free_origin?show_origin=1&type=4&video_url=%s&bitrate=%s&lng=%s"%(video_id,quality,language)
  182. headers = self.headers
  183. headers["Cookie"] = "%s; %s; MobRentBitr=%s; MobBitr=1; MobRentLang=%s"%(self.session_id,self.mobtv_cache,quality,language)
  184. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  185. m3u8 = response.read()
  186. return unescape(m3u8)
  187. #----------------------------------------------------------------------
  188. def get_arhivs_url(self,video_id):
  189. """Get m3u8 url for given archive video"""
  190. url = "https://m.lattelecom.tv/free_origin?show_origin=1&type=arhivs&event_id=%s&bitrate=mhq"%video_id
  191. headers = self.headers
  192. headers["Cookie"] = "%s; %s; MobBitr=1; "%(self.session_id,self.mobtv_cache)
  193. response = urllib2.urlopen(urllib2.Request(url, headers=headers))
  194. m3u8 = response.read()
  195. return unescape(m3u8)
  196. def create_bouquet(ltc,fname,bname):
  197. import codecs
  198. import enigma2_api
  199. e2 = enigma2_api.DBServices()
  200. e2.create_bouqet(self.name, self.title)
  201. with open(fname,"w") as f:
  202. f.write("#NAME %s\n"%bname)
  203. channels = ltc.get_tv_channels()
  204. for i,(ch,title,picon_url) in enumerate(channels):
  205. url=ltc.get_tv_url(ch)
  206. print title #, url, picon_url
  207. sref = "1:0:1:%i:777:777:0:0:0:0:"%i
  208. #sref = e2.get_service_by_name(title.lower())
  209. #if sref:
  210. # sref = sref[0]["sref"]
  211. f.write("#SERVICE %s%s:%s\n"%(sref,quote(url),title))
  212. f.write("#DESCRIPTION %s\n"%title)
  213. pass
  214. if __name__ == "__main__":
  215. #sys.exit(main(sys.argv))
  216. #import enigma2_api
  217. #e2 = enigma2_api.DBServices()
  218. ltc = LattelecomTV()
  219. if not ltc.login("ivars777","kaskade7"):
  220. print "Error during login"
  221. sys.exit()
  222. else:
  223. print 'Logged in'
  224. service_path = "enigma2" if sys.platform=="win32" else "/etc/enigma2"
  225. create_bouquet(ltc, os.path.join(service_path,"userbouquet.ltc.tv"), "lattelecom.tv")