Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import re
- import io, gzip
- import subprocess
- import base64, random
- import httplib
- # rai.py v0.3
- #
- # USAGE python rai.py [-vlcdir=<dir>][-uagent=<user-agent>] [(channel|video_on_demand)]
- #
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Notice:
- # channel = RaiUno | RaiDue | RaiTre | RaiQuattro
- # RaiNews24 | RaiSport+ | RaiStoria | RaiEdu | RaiGulp
- # RaiSatExtra | RaiSatPremium | RaiSatCinema | RaiSatYoyo
- # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- # Change Log
- #
- # + 0.3
- # - Add Video-On-demand support
- # - Update of key in encode2 function
- # - Bugfix in encode2 fucntion: j-index rotation is setted by key length
- # - Refactoring
- #
- # + 0.2
- # - Update of ttAuth generation algorithm
- #
- # + 0.1
- # - Creation of ttAuth
- vlcdir = ""
- isVoD = False
- stream = "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=983"
- agent = "Mozilla/5.0 (X11; U; Linux x86_64; it; rv:1.9.1.7) Gecko/20100106 Ubuntu/9.10 (karmic) Firefox/3.6"
- urls = {
- "RaiUno": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=983",
- "RaiDue": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=984",
- "RaiTre": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=986",
- "RaiQuattro": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=75708",
- "RaiNews24": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=1",
- "RaiSport+": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=4145",
- "RaiStoria": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=24269",
- "RaiEdu": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=24268",
- "RaiGulp": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=4119",
- "RaiSatExtra": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=72382",
- "RaiSatPremium": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=72383",
- "RaiSatCinema": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=72381",
- "RaiSatYoyo": "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=72384",
- }
- def videoURL(pageURL):
- re_page = "^http://(?P<host>[a-zA-Z0-9]*\.([a-zA-Z0-9]*\.)+[a-zA-Z0-9]*)(?P<path>/[\w\-\+\~\%\#\.\/\?\=]*)"
- match_page = re.match(re_page, pageURL).groupdict()
- request = httplib.HTTPConnection(match_page["host"])
- request.putrequest("POST", match_page["path"])
- request.putheader("User-Agent", agent)
- request.putheader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
- request.putheader("Accept-Language", "it-it,it;q=0.8,en-us;q=0.5,en;q=0.3")
- request.putheader("Accept-Encoding", "gzip,deflate")
- request.putheader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
- request.putheader("Keep-Alive", "115")
- request.putheader("Connection", "keep-alive")
- request.putheader("Cookie", "volume=0,5; ns_cookietest=true; ns_session=true")
- request.putheader("Cache-Control", "max-age=0")
- request.endheaders()
- response = request.getresponse().read()
- request.close()
- page = str(gzip.GzipFile(fileobj=io.BytesIO(response)).read(), "utf8")
- return re.search("(?P<video>http://mediapolis.rai.it.*)\"", page).groupdict()["video"]
- def streamURL(host, path, chan, ttAuth):
- asx_connection = httplib.HTTPConnection(host)
- asx_connection.putrequest("POST", path+"?cont="+chan)
- asx_connection.putheader("User-Agent", agent)
- asx_connection.putheader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
- asx_connection.putheader("Accept-Language", "it-it,it;q=0.8,en-us;q=0.5,en;q=0.3")
- asx_connection.putheader("Accept-Encoding", "gzip,deflate")
- asx_connection.putheader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
- asx_connection.putheader("Connection", "keep-alive")
- asx_connection.putheader("Keep-Alive", "115")
- asx_connection.putheader("viaurl", "www.rai.tv")
- asx_connection.putheader("ttAuth", ttAuth)
- asx_connection.putheader("Content-Length", "0")
- asx_connection.endheaders()
- asx_response = asx_connection.getresponse().read()
- asx_connection.close()
- asx = bytes.decode(asx_response)
- return re.search("(?P<mms>mms://.*)\"", asx).groupdict()["mms"]
- def raiDate():
- date_connection = httplib.HTTPConnection("videowall.rai.it")
- date_connection.putrequest("GET", "/cgi-bin/date")
- date_connection.putheader("User-Agent", agent)
- date_connection.putheader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
- date_connection.putheader("Accept-Language", "it-it,it;q=0.8,en-us;q=0.5,en;q=0.3")
- date_connection.putheader("Accept-Encoding", "gzip,deflate")
- date_connection.putheader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
- date_connection.putheader("Connection", "keep-alive")
- date_connection.putheader("Keep-Alive", "115")
- date_connection.endheaders()
- date_response = date_connection.getresponse().read()
- date_connection.close();
- return bytes.decode(date_response[:len(date_response)-1])
- def encode1(token):
- encoded = ""
- for ch in token:
- encoded += chr(ord(ch)^1)
- print(encoded+";1")
- return encoded+";1"
- def encode2(token, key="hMrxuE2T8V0WRW0VmHaKMoFwy1XRc+hK7eBX2tTLVTw="):
- i = len(token)-1
- j = 0
- encoded = ""
- while i>=0:
- enc = chr(ord(token[i]) ^ ord(key[j]))
- encoded = enc + encoded
- i = i-1
- j = j+1
- print(encoded)
- return encoded
- def encode3(token):
- return base64.encodestring(str.encode(token)).decode()
- for arg in sys.argv[1:]:
- if( arg.startswith("-") ):
- match_parm = re.match("^-(\w*)=(.*)", arg)
- if type(match_parm).__name__ == "NoneType":
- print("WARNING: "+arg+" is not a valid parms")
- continue
- if( match_parm.groups()[0] == "vlcdir" ):
- vlcdir = match_parm.groups()[1]
- elif match_parm.groups()[0] == "uagent":
- agent = match_parm.groups()[1]
- else:
- print("WARNING: unknown argument ("+match_parm.groups()[0]+")")
- else:
- try:
- stream = urls[arg]
- except:
- stream = videoURL(arg)
- isVoD = True
- break
- if( isVoD == False ):
- re_url = "^http://(?P<host>[a-zA-Z0-9]*\.([a-zA-Z0-9]*\.)+[a-zA-Z0-9]*)(?P<path>/[\w\-\+\~\%\#\.\/]*)\?cont\=(?P<chanid>\w*)"
- match_url = re.match(re_url, stream).groupdict()
- host = match_url["host"]
- path = match_url["path"]
- chan = match_url["chanid"]
- re_date = "^(?P<day>\d*)-(?P<month>\d*)-(?P<year>\d*)\s(?P<hour>\d*):(?P<minutes>\d*):(?P<seconds>\d*)"
- match_date = re.match(re_date, raiDate()).groupdict()
- day = match_date["day"]
- month = match_date["month"]
- year = match_date["year"]
- hour = match_date["hour"]
- minutes = match_date["minutes"]
- seconds = match_date["seconds"]
- rnd1 = str(random.randint(0, 1234))
- rnd2 = str(random.randint(0, 1234))
- token = year+";"+chan+";"+day+"-"+month+"-"+"528"+"-"+hour+"-"+minutes+"-"+seconds+"-"+"565"
- ttAuth = encode3(encode2(encode1(token)))
- stream = streamURL(host, path, chan, ttAuth)
- print(token)
- subprocess.Popen([vlcdir+"vlc", "--http-user-agent=\""+agent+"\"", stream])
Add Comment
Please, Sign In to add comment