# coding: utf-8 import sys, os, os.path, re, sys import urllib,urllib2 from xml.sax.saxutils import unescape,escape from urllib import quote, unquote 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" headers2dict = lambda h: dict([l.strip().split(": ") for l in h.strip().splitlines()]) ######################################################################## class LattelecomTV(object): """www.lattelecom.tv video server handling object""" #---------------------------------------------------------------------- def __init__(self): self.name = "ltc" self.title = "lattelecom.tv" self.headers = headers2dict(""" Host: m.lattelecom.tv 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 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 DNT: 1 Connection: keep-alive""") #---------------------------------------------------------------------- def login(self,user,password): """Login in to site, create session cookies""" url0 = "https://m.lattelecom.tv" class NoRedirectHandler(urllib2.HTTPRedirectHandler): def http_error_302(self, req, fp, code, msg, headers): infourl = urllib.addinfourl(fp, headers, req.get_full_url()) infourl.status = code infourl.code = code return infourl http_error_300 = http_error_302 http_error_301 = http_error_302 http_error_303 = http_error_302 http_error_307 = http_error_302 # Dabūjam sesijas id un url_gif, kas redirektējas uz auth_url headers = headers2dict(""" Host: m.lattelecom.tv 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 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 DNT: 1 Connection: keep-alive """) response = urllib2.urlopen(urllib2.Request(url0+"/authorization", headers=headers)) session_id = response.headers["set-cookie"].split(";")[0] html = response.read() url_gif = url0 + re.search('(/auth/\d+.gif)', html).group(1) # Dabūtjam auth_url headers = headers2dict(""" Host: m.lattelecom.tv 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 Accept: image/png,image/*;q=0.8,*/*;q=0.5 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate DNT: 1 Referer: https://m.lattelecom.tv/authorization Connection: keep-alive """) headers["Cookie"] = session_id urllib2.install_opener(urllib2.build_opener(NoRedirectHandler())) response = urllib2.urlopen(urllib2.Request(url_gif, headers=headers)) if response.code == 302: url_auth = response.headers["location"] else: self.error = u"auth.gif nenostrādāja" #raise u"auth.gif nenostrādāja" return False # Pierakstāmies iekš auth.lattelecom.lv headers = headers2dict(""" Host: auth.lattelecom.lv 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 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate DNT: 1 Referer: http://m.lattelecom.tv/authorization Connection: keep-alive """) response = urllib2.urlopen(urllib2.Request(url_auth, headers=headers)) if not response.code == 302: self.error = u"pierakstīšanās auth.lattelecom.lv nenostrādāja" return False #raise "pierakstīšanās auth.lattelecom.lv nenostrādāja" # nolasām auth_gif ## headers = headers2dict(""" ## Host: m.lattelecom.tv ## 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 ## Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ## Accept-Language: en-US,en;q=0.5 ## Accept-Encoding: gzip, deflate ## DNT: 1 ## Referer: http://m.lattelecom.tv/authorization ## """) ## headers["Cookie"] = session_id ## response = urllib2.urlopen(urllib2.Request(url_gif, headers=headers)) ## if not response.code == 200: ## raise u"kļūda lasot auth_gif" # Mēģinam ielogoties headers = headers2dict(""" Host: m.lattelecom.tv 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 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: deflate DNT: 1 Referer: http://m.lattelecom.tv/authorization Connection: keep-alive Content-Type: application/x-www-form-urlencoded""") headers["Cookie"] = session_id data = "login=yes&email=%s&passw=%s"%(user,password) req = urllib2.Request(url0+"/authorization", data, headers) response = urllib2.urlopen(req) #with open("auth.htm","w") as f: f.write(response.read()) if not response.code == 302: self.error = u"kļūda ielogojoties" #raise u"kļūda ielogojoties" return False mobtv = response.headers["set-cookie"].split(";")[0] if not mobtv.split("=")[0]=="mobtv_cache": #raise "nav mobtv cookie!" self.error = "nav mobtv cookie!" return False # atveram /account ## headers = headers2dict(""" ## Host: m.lattelecom.tv ## 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 ## Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ## Accept-Language: en-US,en;q=0.5 ## Accept-Encoding: deflate ## DNT: 1 ## Referer: http://m.lattelecom.tv/authorization ## Connection: keep-alive """) ## headers["Cookie"] = "%s; %s"%(session_id,mobtv) ## response = urllib2.urlopen(urllib2.Request(url0+"/account", headers=headers)) ## if not response.code == 302: ## raise u"kļūda atverot /account" # atveram /profils ## headers = headers2dict(""" ## Host: m.lattelecom.tv ## 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 ## Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 ## Accept-Language: en-US,en;q=0.5 ## Accept-Encoding: deflate ## DNT: 1 ## Referer: http://m.lattelecom.tv/authorization ## Connection: keep-alive ## """) ## headers["Cookie"] = "%s; %s"%(session_id,mobtv) ## response = urllib2.urlopen(urllib2.Request(url0+"/profils", headers=headers)) ## if not response.code == 200: ## raise u"kļūda atverot /profils" ## with open("profils.htm","w") as f: f.write(response.read()) self.mobtv_cache = mobtv self.session_id = session_id self.error = "" return True #---------------------------------------------------------------------- def get_tv_channels(self,): """Get tv channels list""" url = "https://m.lattelecom.tv/tiesraide" response = urllib2.urlopen(urllib2.Request(url, headers=self.headers)) html = response.read() channels = re.findall(r'(?si)data-url="/tiesraide/(.*?)">