python webkit webview remember cookies?

Question:

I have written a short python script that opens Google music in web view window. however I can’t seem to find anything about getting webkit to use cookies so that I don’t have to login every time I start it up.

Here’s what I have:

#!/usr/bin/env python
import gtk, webkit
import ctypes

libgobject = ctypes.CDLL('/usr/lib/i386-linux-gnu/libgobject-2.0.so.0')
libwebkit = ctypes.CDLL('/usr/lib/libsoup-2.4.so.1')
libsoup = ctypes.CDLL('/usr/lib/libsoup-2.4.so.1')
libwebkit = ctypes.CDLL('/usr/lib/libwebkitgtk-1.0.so.0')

proxy_uri = libsoup.soup_uri_new('http://tcdproxy.tcd.ie:8080') #proxy urli

session = libwebkit.webkit_get_default_session()
libgobject.g_object_set(session, "proxy-uri", proxy_uri, None)

w = gtk.Window()
w.connect("destroy",w.destroy)
w.set_size_request(1000,600)
w.connect('delete-event', lambda w, event: gtk.main_quit())
s = gtk.ScrolledWindow()
v = webkit.WebView()
s.add(v)
w.add(s)
w.show_all()

v.open('http://music.google.com')
gtk.main()

Any help on this would be greatly appreciated,
thanks,

Richard

Asked By: Richard Mosse

||

Answers:

Worked it out, but it required learning more ctypes than I wanted -_-. Try this- I required different library paths, etc than you, so I’ll just paste what’s relevant.

#remove all cookiejars
generic_cookiejar_type = libgobject.g_type_from_name('SoupCookieJar')
libsoup.soup_session_remove_feature_by_type(session, generic_cookiejar_type)

#and replace with a new persistent jar
cookiejar = libsoup.soup_cookie_jar_text_new('/path/to/your/cookies.txt',False)
libsoup.soup_session_add_feature(session, cookiejar)

The code’s pretty self explanatory. There’s also a SoupCookieJarSqlite that you might prefer, though I’m sure the text file would be easier for development.

EDIT: actually, the cookie jar removal doesn’t seem to be doing anything, so the appropriate snippet is

#add a new persistent cookie jar
cookiejar = libsoup.soup_cookie_jar_text_new('/path/to/your/cookies.txt',False)
libsoup.soup_session_add_feature(session, cookiejar)
Answered By: Matt Luongo

I know its old question and have been looking for the answer all over the place. Finally came up on my own after some trial and error. Hope this helps others.

This is basically same answer from Matt, just using GIR introspection and feels more pythonish.

from gi.repository import Soup
cookiejar = Soup.CookieJarText.new("<Your cookie path>", False)
cookiejar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS)
session = WebKit.get_default_session()
session.add_feature(cookiejar)
Answered By: user871199

In the latest version i.e. GTK WebKit2 4.0, this has to be done in the following way:

import gi
gi.require_version('Soup', '2.4')
gi.require_version('WebKit2', '4.0')

from gi.repository import Soup
from gi.repository import WebKit2

browser = WebKit2.WebView()

website_data_manager = browser.get_website_data_manager()
cookie_manager = website_data_manager.get_cookie_manager()
cookie_manager.set_persistent_storage('PATH_TO_YOUR/cookie.txt', WebKit2.CookiePersistentStorage.TEXT)
cookie_manager.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS)
Answered By: Blake
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.