def read_binary_cookies(path): """Read a cookie list from an Apple binarycookies file. Returns a list of dictionaries. """ if NSHTTPCookieStorage is None: raise NotImplementedError("Apple binary cookies are not supported on this system") storage = NSHTTPCookieStorage.sharedHTTPCookieStorage() cookies = [] for raw_cookie in storage.cookies(): props = raw_cookie.properties() created = float(props["Created"]) + osx_epoch_offset expires = float(props["Expires"].timeIntervalSince1970()) cookies.append( { "raw": raw_cookie, "Created": datetime.fromtimestamp(created), "Domain": unicode(props["Domain"]), "Expires": datetime.utcfromtimestamp(expires), "Name": unicode(props["Name"]), "Path": unicode(props["Path"]), "Secure": bool(raw_cookie.isSecure()), "Value": unicode(props["Value"]), } ) return cookies
def read_binary_cookies(path): """Read a cookie list from an Apple binarycookies file. Returns a list of dictionaries. """ if NSHTTPCookieStorage is None: raise NotImplementedError( "Apple binary cookies are not supported on this system") storage = NSHTTPCookieStorage.sharedHTTPCookieStorage() cookies = [] for raw_cookie in storage.cookies(): props = raw_cookie.properties() created = float(props['Created']) + osx_epoch_offset expires = float(props['Expires'].timeIntervalSince1970()) cookies.append({ 'raw': raw_cookie, 'Created': datetime.fromtimestamp(created), 'Domain': unicode(props['Domain']), 'Expires': datetime.utcfromtimestamp(expires), 'Name': unicode(props['Name']), 'Path': unicode(props['Path']), 'Secure': bool(raw_cookie.isSecure()), 'Value': unicode(props['Value']), }) return cookies
def write_binary_cookies(cookies, path): """Write a cookie list to an Apple binarycookies file. """ if NSHTTPCookieStorage is None: raise NotImplementedError("Apple binary cookies are not supported on this system") storage = NSHTTPCookieStorage.sharedHTTPCookieStorage() keep = set(cookie.get("raw") for cookie in cookies) keep.discard(None) kill = set(cookie for cookie in storage.cookies() if cookie not in keep) for cookie in kill: storage.deleteCookie_(cookie) storage = None
def get_cookiefile_for_url(url): """ Returns WebKit cookies for the specified URL as a string. Modified from http://zerokspot.com/weblog/2007/04/12/safari-cookies-to-netscape-cookie-format/ """ storage = NSHTTPCookieStorage.sharedHTTPCookieStorage() u = NSURL.URLWithString_(url) fd = StringIO() try: fd.write(FILE_HEADER) for c in storage.cookiesForURL_(u): line = "%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ( c.domain(), 'TRUE', c.path(), str(bool(c.isSecure())).upper(), int(c.expiresDate().timeIntervalSince1970()), c.name(), c.value(), ) fd.write(line) finally: fd.seek(0) return fd.read()