Guest User

Untitled

a guest
Mar 2nd, 2010
12,361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.97 KB | None | 0 0
  1. import sys
  2. import re
  3. import io, gzip
  4. import subprocess
  5. import base64, random
  6. import httplib
  7.  
  8. # rai.py v0.3
  9. #
  10. # USAGE python rai.py [-vlcdir=<dir>][-uagent=<user-agent>] [(channel|video_on_demand)]
  11. #
  12. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  13. # Notice:
  14. # channel = RaiUno | RaiDue | RaiTre | RaiQuattro
  15. #           RaiNews24 | RaiSport+ | RaiStoria | RaiEdu | RaiGulp
  16. #           RaiSatExtra | RaiSatPremium | RaiSatCinema | RaiSatYoyo
  17. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  18. # Change Log
  19. #
  20. # + 0.3
  21. #       - Add Video-On-demand support
  22. #       - Update of key in encode2 function
  23. #       - Bugfix in encode2 fucntion: j-index rotation is setted by key length
  24. #       - Refactoring
  25. #
  26. # + 0.2
  27. #       - Update of ttAuth generation algorithm
  28. #
  29. # + 0.1
  30. #       - Creation of ttAuth    
  31.  
  32. vlcdir  = ""
  33. isVoD   = False
  34. stream  = "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=983"
  35. agent   = "Mozilla/5.0 (X11; U; Linux x86_64; it; rv:1.9.1.7) Gecko/20100106 Ubuntu/9.10 (karmic) Firefox/3.6"
  36.  
  37. urls = {
  38.     "RaiUno":       "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=983",
  39.     "RaiDue":       "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=984",
  40.     "RaiTre":       "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=986",
  41.     "RaiQuattro":       "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=75708",
  42.     "RaiNews24":        "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=1",
  43.     "RaiSport+":        "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=4145",
  44.     "RaiStoria":        "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=24269",
  45.     "RaiEdu":       "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=24268",
  46.     "RaiGulp":      "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=4119",
  47.     "RaiSatExtra":      "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=72382",
  48.     "RaiSatPremium":    "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=72383",
  49.     "RaiSatCinema":     "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=72381",
  50.     "RaiSatYoyo":       "http://mediapolis.rai.it/relinker/relinkerServlet.htm?cont=72384",
  51. }
  52.  
  53. def videoURL(pageURL):
  54.     re_page = "^http://(?P<host>[a-zA-Z0-9]*\.([a-zA-Z0-9]*\.)+[a-zA-Z0-9]*)(?P<path>/[\w\-\+\~\%\#\.\/\?\=]*)"
  55.     match_page = re.match(re_page, pageURL).groupdict()
  56.     request = httplib.HTTPConnection(match_page["host"])
  57.     request.putrequest("POST", match_page["path"])
  58.     request.putheader("User-Agent", agent)
  59.     request.putheader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  60.     request.putheader("Accept-Language", "it-it,it;q=0.8,en-us;q=0.5,en;q=0.3")
  61.     request.putheader("Accept-Encoding", "gzip,deflate")
  62.     request.putheader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
  63.     request.putheader("Keep-Alive", "115")
  64.     request.putheader("Connection", "keep-alive")
  65.     request.putheader("Cookie", "volume=0,5; ns_cookietest=true; ns_session=true")
  66.     request.putheader("Cache-Control", "max-age=0")
  67.     request.endheaders()
  68.     response = request.getresponse().read()
  69.     request.close()
  70.     page = str(gzip.GzipFile(fileobj=io.BytesIO(response)).read(), "utf8")
  71.     return re.search("(?P<video>http://mediapolis.rai.it.*)\"", page).groupdict()["video"]
  72.  
  73. def streamURL(host, path, chan, ttAuth):
  74.     asx_connection = httplib.HTTPConnection(host)
  75.     asx_connection.putrequest("POST", path+"?cont="+chan)
  76.     asx_connection.putheader("User-Agent", agent)
  77.     asx_connection.putheader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  78.     asx_connection.putheader("Accept-Language", "it-it,it;q=0.8,en-us;q=0.5,en;q=0.3")
  79.     asx_connection.putheader("Accept-Encoding", "gzip,deflate")
  80.     asx_connection.putheader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
  81.     asx_connection.putheader("Connection", "keep-alive")
  82.     asx_connection.putheader("Keep-Alive", "115")
  83.     asx_connection.putheader("viaurl", "www.rai.tv")
  84.     asx_connection.putheader("ttAuth", ttAuth)
  85.     asx_connection.putheader("Content-Length", "0")
  86.     asx_connection.endheaders()
  87.     asx_response = asx_connection.getresponse().read()
  88.     asx_connection.close()
  89.     asx = bytes.decode(asx_response)
  90.     return re.search("(?P<mms>mms://.*)\"", asx).groupdict()["mms"]
  91.  
  92. def raiDate():
  93.     date_connection = httplib.HTTPConnection("videowall.rai.it")
  94.     date_connection.putrequest("GET", "/cgi-bin/date")
  95.     date_connection.putheader("User-Agent", agent)
  96.     date_connection.putheader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
  97.     date_connection.putheader("Accept-Language", "it-it,it;q=0.8,en-us;q=0.5,en;q=0.3")
  98.     date_connection.putheader("Accept-Encoding", "gzip,deflate")
  99.     date_connection.putheader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")
  100.     date_connection.putheader("Connection", "keep-alive")
  101.     date_connection.putheader("Keep-Alive", "115")
  102.     date_connection.endheaders()
  103.     date_response = date_connection.getresponse().read()
  104.     date_connection.close();
  105.     return bytes.decode(date_response[:len(date_response)-1])
  106.  
  107. def encode1(token):
  108.     encoded = ""
  109.  
  110.     for ch in token:
  111.         encoded += chr(ord(ch)^1)
  112.     print(encoded+";1")
  113.     return encoded+";1"
  114.  
  115. def encode2(token, key="hMrxuE2T8V0WRW0VmHaKMoFwy1XRc+hK7eBX2tTLVTw="):
  116.     i = len(token)-1
  117.     j = 0
  118.     encoded = ""
  119.     while i>=0:
  120.         enc = chr(ord(token[i]) ^ ord(key[j]))
  121.         encoded = enc + encoded
  122.         i = i-1
  123.         j = j+1
  124.     print(encoded)
  125.     return encoded
  126.  
  127. def encode3(token):
  128.     return base64.encodestring(str.encode(token)).decode()
  129.  
  130. for arg in sys.argv[1:]:
  131.     if( arg.startswith("-") ):
  132.         match_parm = re.match("^-(\w*)=(.*)", arg)
  133.         if type(match_parm).__name__ == "NoneType":
  134.             print("WARNING: "+arg+" is not a valid parms")
  135.             continue
  136.  
  137.         if( match_parm.groups()[0] == "vlcdir" ):
  138.             vlcdir = match_parm.groups()[1]
  139.         elif match_parm.groups()[0] == "uagent":
  140.             agent = match_parm.groups()[1]
  141.         else:
  142.             print("WARNING: unknown argument ("+match_parm.groups()[0]+")")
  143.     else:
  144.         try:
  145.             stream = urls[arg]
  146.         except:
  147.             stream = videoURL(arg)
  148.             isVoD = True
  149.         break
  150.  
  151. if( isVoD == False ):
  152.     re_url  = "^http://(?P<host>[a-zA-Z0-9]*\.([a-zA-Z0-9]*\.)+[a-zA-Z0-9]*)(?P<path>/[\w\-\+\~\%\#\.\/]*)\?cont\=(?P<chanid>\w*)"
  153.     match_url = re.match(re_url, stream).groupdict()
  154.     host = match_url["host"]
  155.     path = match_url["path"]
  156.     chan = match_url["chanid"]
  157.  
  158.     re_date = "^(?P<day>\d*)-(?P<month>\d*)-(?P<year>\d*)\s(?P<hour>\d*):(?P<minutes>\d*):(?P<seconds>\d*)"
  159.     match_date = re.match(re_date, raiDate()).groupdict()
  160.     day = match_date["day"]
  161.     month   = match_date["month"]
  162.     year    = match_date["year"]
  163.     hour    = match_date["hour"]
  164.     minutes = match_date["minutes"]
  165.     seconds = match_date["seconds"]
  166.  
  167.     rnd1 = str(random.randint(0, 1234))
  168.     rnd2 = str(random.randint(0, 1234))
  169.  
  170.     token = year+";"+chan+";"+day+"-"+month+"-"+"528"+"-"+hour+"-"+minutes+"-"+seconds+"-"+"565"
  171.     ttAuth = encode3(encode2(encode1(token)))
  172.     stream = streamURL(host, path, chan, ttAuth)
  173.     print(token)
  174. subprocess.Popen([vlcdir+"vlc", "--http-user-agent=\""+agent+"\"", stream])
Add Comment
Please, Sign In to add comment