pythoncookiessaveloadroblox

How do I save/load specific browser cookies?


I have the following Python code that is supposed to log into a website using the .ROBLOSECURITY cookie. It also includes an except IOERROR: function so that if the .ROBLOSECURITY cookie doesn't log in, it will use a username/password to log in AND it will save the cookies it gets from that.

import urllib2
import urllib
import cookielib

try:
    cookielib.LWPCookieJar().load("cookies.txt") #Trying to load the cookie file

except IOError: #In case the cookies.txt fails to log in. I don't know if IOError is the correct error specification for an expired cookie
    print "Loading stored cookies failed, manually logging in..."
    cj = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    opener.addheaders = [('User-agent', 'Mozilla/5.0')]
    urllib2.install_opener(opener)
    authentication_url = 'https://www.roblox.com/newlogin'
    payload = {
        'username' : 'UsernameHere',
        'password' : 'PasswordHere',
        '' : 'Log In',
        }
    data = urllib.urlencode(payload)
    req = urllib2.Request(authentication_url, data)
    resp = urllib2.urlopen(req)
    cj.save("cookies.txt") #Saving to the cookie file

tc = urllib2.urlopen("http://www.roblox.com/My/Money.aspx").read() #The hidden page
checksession = re.findall('My Transactions',tc) #Just checking for text that is only found on the hidden page
print checksession

I think that cookielib.LWPCookieJar().load("cookies.txt") is not working because it is also loading other cookies other than the .ROBLOSECURITY (Which I know logs in if you only use that). How do I either load ONLY the .ROBLOSECURITY cookie or save ONLY the .ROBLOSECURITY (So that other cookies don't interfere with the .ROBLOSECURITY logging in)?

Also, I'm not sure if my except IOError: will function correctly because I only know that that works if I change my cookielib.LWPCookieJar().load("cookies.txt") to cookielib.MozillaCookieJar().load("cookies.txt")

Lastly, how can I change my .ROBLOSECURITY's expiration date to something like 2050-12-31 24:00:00Z


Solution

  • So here was my final code:

    import urllib #Idk if this is necessary, but I'm not gonna bother checking. Only time I used it was for urllib.urlencode, which idk if urllib2 can do
    import urllib2
    import cookiejar
    try:
        cj = cookielib.MozillaCookieJar("./cookies.txt")
        cj.load()
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        tc = opener.open("http://www.roblox.com/My/Money.aspx").read()
    
    #    print "Loading stored cookies succeeded, automatically logging in..."
    
    #    checksession = re.findall('My Transactions',tc)
    
    #    if len(checksession) > 0:
    #        print "Login success!"
    
    except IOError:
        print "Loading stored cookies failed, manually logging in..."
        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        opener.addheaders = [('User-agent', 'Mozilla/5.0')]
        urllib2.install_opener(opener)
        authentication_url = 'https://www.roblox.com/newlogin'
        payload = {
            'username' : 'USERNAMEHERE',
            'password' : 'PASSWORDHERE',
            '' : 'Log In', #In hindsight Idk if this is necessary, but I don't feel like checking now
            }
        data = urllib.urlencode(payload)
        req = urllib2.Request(authentication_url, data)
        resp = urllib2.urlopen(req)
        cj.save("./cookies.txt") #./ to make sure its in the right directory
    
        tc = opener.open("http://www.roblox.com/My/Money.aspx").read() #Only accessible if logged in
    #    checksession = re.findall('My Transactions',tc) #The rest is to check if log in was success by looking for specific phrase in the page
    
    #    if len(checksession) > 0:
    #        print "Login success!"