#!/usr/bin/python
"""\
moz_to_rss.py Copyright 2012, Bjarni R. Einarsson, http://bre.klaki.net/
This program is free software: you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
* * *
This is a simple tool to extract the contents of one Firefox bookmarks folder
(non-recursively) and present it as an RSS feed. The folder is recognized
by its description (not title).
Usage:
$ moz_to_rss.py /path/to/places.sqlite description [NUM] >output.rss
The optional NUM (default is 10) parameter controls how many bookmarks are
extracted.
"""
import os
import sqlite3
import sys
from cgi import escape
from email.utils import formatdate
MAX_ITEMS = 10
SQL_GET_FOLDER_BOOKMARKS = """\
select b.id, p.url, b.title, b.dateAdded
from moz_items_annos f,
moz_bookmarks b,
moz_places p
where f.content = ?
and b.parent = f.item_id
and p.id = b.fk
order by b.dateAdded desc
limit ?
"""
SQL_GET_DESCRIPTION = """\
select a.content
from moz_items_annos a,
moz_bookmarks b
where b.id = ?
and b.id = a.item_id
"""
RSS_TEMPLATE = """\
/
Recently Bookmarked
These are recently bookmarked links, extracted directly from Firefox's
places.sqlite file by moz_places_to_rss.py.
%(builddate)s
%(items)s
"""
RSS_ITEM_TEMPLATE = """\
%(url)s
%(title)s%(description)s%(pubdate)s
"""
def bookmarks_to_rss(dbfile, folder_desc, max_items):
db = sqlite3.connect(dbfile).cursor()
db.execute(SQL_GET_FOLDER_BOOKMARKS, (folder_desc, max_items))
items = []
for bid, url, btitle, dateAdded in db.fetchall():
db.execute(SQL_GET_DESCRIPTION, (bid, ))
description = ' '.join([d[0] for d in db.fetchall()])
items.append(RSS_ITEM_TEMPLATE % {
'url': escape(url.encode('utf-8')),
'title': escape(btitle.encode('utf-8')),
'description': escape(description.encode('utf-8')),
'pubdate': formatdate(dateAdded / 1000000)
})
db.close()
return RSS_TEMPLATE % {
'items': ''.join(items),
'builddate': formatdate()
}
if __name__ == '__main__':
if len(sys.argv) < 3:
print __doc__
sys.exit(1)
if len(sys.argv) > 3:
MAX_ITEMS = int(sys.argv[3])
print bookmarks_to_rss(sys.argv[1], sys.argv[2], MAX_ITEMS)