[kupfer] Add chromium bookmarks plugin
- From: Ulrik Sverdrup <usverdrup src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [kupfer] Add chromium bookmarks plugin
- Date: Fri, 23 Oct 2009 10:41:05 +0000 (UTC)
commit e27aaefa770d59a501523e85c6022b93be7cf9a9
Author: Francesco Marella <francesco marella gmail com>
Date: Fri Oct 23 12:02:57 2009 +0200
Add chromium bookmarks plugin
Index of Chromium Bookmarks
kupfer/plugin/chromium.py | 48 ++++++++++++++++++++++++++++
kupfer/plugin/chromium_support.py | 62 +++++++++++++++++++++++++++++++++++++
2 files changed, 110 insertions(+), 0 deletions(-)
---
diff --git a/kupfer/plugin/chromium.py b/kupfer/plugin/chromium.py
new file mode 100644
index 0000000..73c4406
--- /dev/null
+++ b/kupfer/plugin/chromium.py
@@ -0,0 +1,48 @@
+import os
+
+from kupfer.objects import Leaf, Action, Source, AppLeafContentMixin
+from kupfer.objects import UrlLeaf
+from kupfer import plugin_support
+
+__kupfer_name__ = _("Chromium Bookmarks")
+__kupfer_sources__ = ("BookmarksSource", )
+__kupfer_contents__ = ("BookmarksSource", )
+__description__ = _("Index of Chromium bookmarks")
+__version__ = ""
+__author__ = "Francesco Marella <francesco marella gmail com>"
+
+__kupfer_settings__ = plugin_support.PluginSettings(
+ plugin_support.SETTING_PREFER_CATALOG,
+)
+
+class BookmarksSource (AppLeafContentMixin, Source):
+ appleaf_content_id = ("chromium-browser")
+ def __init__(self):
+ super(BookmarksSource, self).__init__(_("Chromium Bookmarks"))
+
+ def _get_chromium_items(self, fpath):
+ """Parse Chromium' bookmarks backups"""
+ from chromium_support import get_bookmarks
+ self.output_debug("Parsing", fpath)
+ bookmarks = get_bookmarks(fpath)
+ for book in bookmarks:
+ yield UrlLeaf(book["url"], book["name"])
+
+ def get_items(self):
+ from chromium_support import get_chromium_home_file
+ fpath = get_chromium_home_file("Bookmarks")
+ if fpath:
+ try:
+ return self._get_chromium_items(fpath)
+ except Exception, exc:
+ self.output_error(exc)
+
+ self.output_error("No Chromium bookmarks file found")
+ return []
+
+ def get_description(self):
+ return _("Index of Chromium bookmarks")
+ def get_icon_name(self):
+ return "chromium-browser"
+ def provides(self):
+ yield UrlLeaf
diff --git a/kupfer/plugin/chromium_support.py b/kupfer/plugin/chromium_support.py
new file mode 100644
index 0000000..44f5d93
--- /dev/null
+++ b/kupfer/plugin/chromium_support.py
@@ -0,0 +1,62 @@
+# -*- coding: UTF-8 -*-
+
+from __future__ import with_statement
+try:
+ import cjson
+ json_decoder = cjson.decode
+except ImportError:
+ import json
+ json_decoder = json.loads
+from os.path import join, expanduser, exists, basename
+
+def get_chromium_home_file(needed_file):
+ chromium_dir = expanduser("~/.config/chromium/Default/")
+ if not exists(chromium_dir):
+ # no break
+ return None
+
+ return join(chromium_dir, needed_file)
+
+def get_bookmarks(bookmarks_file):
+ # construct and configure the parser
+ if not bookmarks_file:
+ return []
+
+ with open(bookmarks_file) as f:
+ content = f.read().decode("UTF-8")
+ root = json_decoder(content)
+
+ # make a dictionary of unique bookmarks
+ bmap = {}
+
+ def bmap_add(bmark, bmap):
+ if bmark["id"] not in bmap:
+ bmap[bmark["id"]] = bmark
+
+ CONTAINER = "folder"
+ UNWANTED_SCHEME = ("data", "place", "javascript")
+
+ def is_container(ch):
+ return ch["type"] == CONTAINER
+ def is_bookmark(ch):
+ return ch.get("url")
+ def is_good(ch):
+ return not ch["url"].split(":", 1)[0] in UNWANTED_SCHEME
+
+ folders = []
+
+ # add some folders
+ folders.extend(root['roots']['bookmark_bar']['children'])
+ folders.extend(root['roots']['other']['children'])
+
+ for item in folders:
+ if is_bookmark(item) and is_good(item):
+ bmap_add(item, bmap)
+ if is_container(item):
+ folders.extend(item["children"])
+
+ return bmap.values()
+
+if __name__ == "__main__":
+ fpath = get_chromium_home_file("Bookmarks")
+ print "Parsed # bookmarks:", len(list(get_bookmarks(fpath)))
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]