Skip to content

{ Tag Archives } firefox

Reading cookies from most Firefox versions…

Yesterday, I wrote about how to reading the cookies from Firefox 3.0 from Python. This code snippet extends the previous example by adding code which finds the cookie file on various different operating systems (Windows, Linux and Mac OS X). Hope this helps people who need to do this.

#! /usr/bin/env python
# Reading the cookie's from Firefox/Mozilla. Supports Firefox 3.0 and Firefox 2.x
#
# Author: Noah Fontes <nfontes AT cynigram DOT com>, 
#         Tim Ansell <mithro AT mithis DOT com>
# License: MIT
 
def sqlite2cookie(filename):
    from cStringIO import StringIO
    from pysqlite2 import dbapi2 as sqlite
 
    con = sqlite.connect(filename)
 
    cur = con.cursor()
    cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies")
 
    ftstr = ["FALSE","TRUE"]
 
    s = StringIO()
    s.write("""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
""")
    for item in cur.fetchall():
        s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (
            item[0], ftstr[item[0].startswith('.')], item[1],
            ftstr[item[2]], item[3], item[4], item[5]))
 
    s.seek(0)
 
    cookie_jar = cookielib.MozillaCookieJar()
    cookie_jar._really_load(s, '', True, True)
    return cookie_jar
 
import cookielib
import os
import sys
import logging
import ConfigParser
 
# Set up cookie jar paths
def _get_firefox_cookie_jar (path):
    profiles_ini = os.path.join(path, 'profiles.ini')
    if not os.path.exists(path) or not os.path.exists(profiles_ini):
        return None
 
    # Open profiles.ini and read the path for the first profile
    profiles_ini_reader = ConfigParser.ConfigParser();
    profiles_ini_reader.readfp(open(profiles_ini))
    profile_name = profiles_ini_reader.get('Profile0', 'Path', True)
 
    profile_path = os.path.join(path, profile_name)
    if not os.path.exists(profile_path):
        return None
    else:
        if os.path.join(profile_path, 'cookies.sqlite'):
            return os.path.join(profile_path, 'cookies.sqlite')
        elif os.path.join(profile_path, 'cookies.txt'):
            return os.path.join(profile_path, 'cookies.txt')
 
def _get_firefox_nt_cookie_jar ():
    # See http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/473846
    try:
        import _winreg
        import win32api
    except ImportError:
        logging.error('Cannot load winreg -- running windows and win32api loaded?')
    key = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, r'Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders')
    try:
        result = _winreg.QueryValueEx(key, 'AppData')
    except WindowsError:
        return None
    else:
        key.Close()
        if ret[1] == _winreg.REG_EXPAND_SZ:
            result = win32api.ExpandEnvironmentStrings(ret[0])
        else:
            result = ret[0]
 
    return _get_firefox_cookie_jar(os.path.join(result, r'Mozilla\Firefox\Profiles'))
 
def _get_firefox_posix_cookie_jar ():
    return _get_firefox_cookie_jar(os.path.expanduser(r'~/.mozilla/firefox'))
 
def _get_firefox_mac_cookie_jar ():
    # First of all...
    result = _get_firefox_cookie_jar(os.path.expanduser(r'~/Library/Mozilla/Firefox/Profiles'))
    if result == None:
        result = _get_firefox_cookie_jar(os.path.expanduser(r'~/Library/Application Support/Firefox/Profiles'))
    return result
 
FIREFOX_COOKIE_JARS = {
    'nt': _get_firefox_nt_cookie_jar,
    'posix': _get_firefox_posix_cookie_jar,
    'mac': _get_firefox_mac_cookie_jar
}
 
cookie_jar = None
try:
    cookie_jar = FIREFOX_COOKIE_JARS[os.name]()
except KeyError:
    cookie_jar = None
 
path = raw_input('Path to cookie jar file [%s]: ' % cookie_jar)
if path.strip():
    # Some input specified, set it
    cookie_jar = os.path.realpath(os.path.expanduser(path.strip()))
 
if cookie_jar.endswith('.sqlite'):
    cookie_jar = sqlite2cookie(cookie_jar)
else:
    cookie_jar = cookielib.MozillaCookieJar(cookie_jar)

Edit: The latest version of this code can be found at http://blog.mithis.com/cgi-bin/gitweb.cgi and includes numerous fixes and updates.

Also tagged , , ,

Reading Firefox 3.x cookies in Python

I found the following code snippet on my hard drive today. It allows you to access Firefox 3.x cookies in Python. Firefox 3.x moved away from the older text file format to a sqlite database.

This code is useful if you want to access something behind an authentication gateway and you also access the page through your web browser. You can also use this code to convert a sqlite database into a cookie file CURL can read.

I didn’t write this code, it was written by Noah Fontes when we where doing some scraping of the Google Summer of Code website (before I joined Google).

#! /usr/bin/env python
# Protocol implementation for handling gsocmentors.com transactions
# Author: Noah Fontes nfontes AT cynigram DOT com
# License: MIT
 
def sqlite2cookie(filename):
    from cStringIO import StringIO
    from pysqlite2 import dbapi2 as sqlite
 
    con = sqlite.connect(filename)
 
    cur = con.cursor()
    cur.execute("select host, path, isSecure, expiry, name, value from moz_cookies")
 
    ftstr = ["FALSE","TRUE"]
 
    s = StringIO()
    s.write("""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.
""")
    for item in cur.fetchall():
        s.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (
            item[0], ftstr[item[0].startswith('.')], item[1],
            ftstr[item[2]], item[3], item[4], item[5]))
 
    s.seek(0)
 
    cookie_jar = cookielib.MozillaCookieJar()
    cookie_jar._really_load(s, '', True, True)
    return cookie_jar
Also tagged , , ,

Quotidian!

I’m sure most people are wondering what he hell “quotidian” is, I myself did not know this word existed. The word actually means mundane or everyday, a work colleague suggested it when I asked is anyone knew what the opposite to epiphany was. So you probably wondering why I’m going on about some stupid word, well the reason is that I have finally converted from the gnome Epiphany web browser to Firefox.

For thoses who don’t know, Epiphany is generally described as “the closest thing to Gnome’s official web browser”. It has lots of nifty features and use to have much better intergration with the Gnome desktop (things like actually using the Gnome print dialog). I use to advocate that Gnome should push Epiphany instead of Firefox.

So why have I given up? I’m tired of my browser being broken.

The developers of Epiphany decided to make some huge changes in the latest version, they started adding support for the WebKit (the render behind Safari) instead of just being dependent on Gecko (the same render Firefox uses). This is actually a very good goal, being able to have a choice of renders in my browser would be great. However, in the process of doing this change they broke everything! Things like the vitally important Adblock extension no longer work and the password manager is totally broken in a number of ways – for a long time they didn’t even show up in the dialog.

I’m okay with a few bugs here and there (even these quite serious ones) to get something better in the long run. I have diligently reported bugs as I found them (even firing up a different browser when the gnome bug browser was crashing Epiphany). I even started porting Google Gears to Epiphany because I had faith that Epiphany was going to remain a killer browser.

Now I find out that it has all been for nothing, the developers have decided they are going to totally ditch Gecko and move only to WebKit (with all the compatibility problems it will bring). There are so many reasons why this is a bad idea, none of which I’m going to repeat here.

So I’m now writing this in Firefox instead of Epiphany and I’m pretty happy. Having access to all the extensions that Firefox has is really nice for once. There a number of features which I miss from Epiphany and extensions have filled most of that void, so what did I install?

  • Tab History, this means that new tabs have the same history as the parent tab. This is something Epiphany does by default and I find absolutely vital.
  • Compact Menu 2, a little extension which gives you the ability to have all your menus in a single button saving you precious vertical screen space.
  • Ad Block Plus, got to keep away those evil ads. It’s amazing how annoying the web is with ads, I had gotten so use to not seeing them that I didn’t know how bad it actually was.
  • NoScript, get rid of all that annoying flash and evil javascript. In epiphany I generally ended up just apt-get removing flash, now I can still watch stupid Youtube videos without being violated by monkies.

I also installed two extensions that have no equivalent under Epiphany, they are

  • Greasemonkey, the extension for making websites the way you like them.
  • Firebug, a really cool tool for figuring out how a website is made up and various problems with them.

I’m still looking for an extension which makes the Firefox 3.0 URL bar sane. I really like how the Epiphany one behaves and will probably end up writing my own extension if I don’t find anything. The important features that I require are,

  • Single line per URL with the title on the right.
  • Support for “smart bookmarks” as the last option. These let you search for the current term at Google or Wikipedia without prefixing it with a stupid keyword or something.

Here is what my Firefox looks like currently. As you can see I have significantly customized the toolbar to remove all that excesses.

Screenshot of my Firefox

Also tagged , , ,