From 9c8429477aefb3928b94ab9750a3bbb70289270d Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 25 Apr 2017 17:17:22 -0400 Subject: [PATCH] -browser/firefox/bin, +shell/fzf/bin/fzf-{bookmarks,history} --- browser/firefox/bin/firefox-bookmarks | 7 --- browser/firefox/bin/firefox-history | 7 --- browser/firefox/bin/lib/FirefoxProfile.py | 73 ----------------------- browser/firefox/bin/lib/__init__.py | 0 shell/fzf/bin/fzf-bookmarks | 41 ++++++++++++- shell/fzf/bin/fzf-history | 40 ++++++++++++- 6 files changed, 77 insertions(+), 91 deletions(-) delete mode 100755 browser/firefox/bin/firefox-bookmarks delete mode 100755 browser/firefox/bin/firefox-history delete mode 100644 browser/firefox/bin/lib/FirefoxProfile.py delete mode 100644 browser/firefox/bin/lib/__init__.py diff --git a/browser/firefox/bin/firefox-bookmarks b/browser/firefox/bin/firefox-bookmarks deleted file mode 100755 index c3e6cd87..00000000 --- a/browser/firefox/bin/firefox-bookmarks +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env python3 - -from lib.FirefoxProfile import FirefoxProfile - - -with FirefoxProfile.default() as ff: - print("\n".join(ff.bookmarks())) diff --git a/browser/firefox/bin/firefox-history b/browser/firefox/bin/firefox-history deleted file mode 100755 index da309a43..00000000 --- a/browser/firefox/bin/firefox-history +++ /dev/null @@ -1,7 +0,0 @@ -#!/usr/bin/env python - -from lib.FirefoxProfile import FirefoxProfile - - -with FirefoxProfile.default() as ff: - print("\n".join(ff.history())) diff --git a/browser/firefox/bin/lib/FirefoxProfile.py b/browser/firefox/bin/lib/FirefoxProfile.py deleted file mode 100644 index a482dd50..00000000 --- a/browser/firefox/bin/lib/FirefoxProfile.py +++ /dev/null @@ -1,73 +0,0 @@ -import sqlite3 as sqlite -import os -from glob import glob -from sys import platform as _platform - -class FirefoxProfile: - def __init__(self, dbfile): - self.db = sqlite.connect(dbfile) - self.cur = self.db.cursor() - - def __enter__(self): - return FirefoxProfile.default() - - def __exit__(self, _, __, ___): - self.close() - - @staticmethod - def default(): - dbfile = None - paths = ["~/.mozilla/firefox", "~/Library/Application Support/Firefox/Profiles"] - while paths: - path = os.path.expanduser(paths.pop()) - files = glob('{}/*.default/places.sqlite'.format(path)) - if files: - dbfile = files[0] - break - if not dbfile: - raise Exception("No places.sqlite file") - return FirefoxProfile(dbfile) - - def bookmarks(self): - self.cur.execute( - 'SELECT DISTINCT mb.title, mp.url ' + - 'FROM moz_bookmarks mb ' + - 'INNER JOIN moz_places mp ' + - 'ON mp.id = mb.fk ' + - 'WHERE mb.type = 1 AND mb.title IS NOT NULL ' - 'ORDER BY mp.frecency DESC' - ) - return self._process(self.cur.fetchall()) - - def history(self): - self.cur.execute( - 'SELECT DISTINCT title, url ' + - 'FROM moz_places ' + - 'WHERE title IS NOT NULL ' - 'ORDER BY last_visit_date DESC' - ) - return self._process(self.cur.fetchall()) - - def _process(self, data): - title_lookup = {} - url_lookup = {} - ret = [] - for row in data: - title = row[0].encode('utf-8').decode() - url = row[1].encode('utf-8').decode() - if url.startswith("http") and title not in title_lookup and url not in url_lookup: - title_lookup[title] = 1 - url_lookup[url] = 1 - ret.append("{}, {}".format(title, url)) - return ret - - def browse(self, url): - if _platform.startswith("linux"): - os.system('xdg-open "{}"'.format(url)) - elif _platform == "darwin": - os.system('open "{}"'.format(url)) - else: - raise Exception("What you doin' fool") - - def close(self): - self.db.close() diff --git a/browser/firefox/bin/lib/__init__.py b/browser/firefox/bin/lib/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/shell/fzf/bin/fzf-bookmarks b/shell/fzf/bin/fzf-bookmarks index f58a4321..cf605ec5 100755 --- a/shell/fzf/bin/fzf-bookmarks +++ b/shell/fzf/bin/fzf-bookmarks @@ -1,4 +1,41 @@ #!/usr/bin/env bash -url="$(firefox-bookmarks | gui-fzf | sed 's/^.*, http/http/')" -[[ $url ]] && open "$url" +# Interactively select (in fzf) bookmarks to open from Firefox or +# Chrome/Chromium (depends on $BROWSER). Should work on Linux and MacOS. +# +# requires: sqlite3, jq (if you use chrome/chromium) + +[[ $OSTYPE == linux* ]] && open='xdg-open' || open='open' + +_do_fzf() { + for url in $(awk -F "$1" '{printf "%s, %s\n", $1, $2}' | gui-fzf --multi | sed 's#.*\(https*://\)#\1#'); do + $open "$url" & + done +} + +case ${BROWSER:-firefox} in + firefox) + for dir in ~/.mozilla/firefox/*.default ~/Library/Application\ Support/Firefox/Profiles/*.default; do + if [[ -d "$dir" ]]; then + sep='{{::}}' + sqlite3 -separator $sep "$dir/places.sqlite" \ + "SELECT DISTINCT mb.title, mp.url FROM moz_bookmarks mb \ + INNER JOIN moz_places mp ON mp.id = mb.fk \ + WHERE mb.type = 1 AND mb.title IS NOT NULL \ + ORDER BY mp.frecency DESC" | _do_fzf "$sep" + break + fi + done + ;; + *) + for dir in ~/.config/google-chrome/Default ~/.config/chromium/Default ~/Library/Application\ Support/Google/Chrome/Default; do + # requires jq + if [[ -f "$dir/Bookmarks" ]]; then + jq -r '.. | [.name, .url]? | @tsv' "$dir/Bookmarks" | \ + sed '/^[ \t]*$/d;/\t$/d;/\tchrome:\/\//d' | \ + _do_fzf "\t" + break + fi + done + ;; +esac diff --git a/shell/fzf/bin/fzf-history b/shell/fzf/bin/fzf-history index baf1753d..bd80b31c 100755 --- a/shell/fzf/bin/fzf-history +++ b/shell/fzf/bin/fzf-history @@ -1,4 +1,40 @@ #!/usr/bin/env bash -url="$(firefox-history | gui-fzf | sed 's/^.*, http/http/')" -[[ $url ]] && open "$url" +# Interactively select (in fzf) an url from Firefox/Chrome/Chromium history to +# open in your browser (depends on $BROWSER). Should work on Linux and MacOS. +# +# requires: sqlite3 + +sep='{{::}}' +[[ $OSTYPE == linux* ]] && open='xdg-open' || open='open' + +_do_fzf() { + for url in $(awk -F "$sep" '{printf "%s, %s\n", $1, $2}' | gui-fzf --multi | sed 's#.*\(https*://\)#\1#'); do + $open "$url" & + done +} + +case ${BROWSER:-firefox} in + firefox) + for dir in ~/.mozilla/firefox/*.default ~/Library/Application\ Support/Firefox/Profiles/*.default; do + if [[ -d "$dir" ]]; then + sqlite3 -separator $sep "$dir/places.sqlite" \ + "SELECT DISTINCT title, url FROM moz_places \ + WHERE title IS NOT NULL \ + ORDER BY last_visit_date DESC" | _do_fzf + break + fi + done + ;; + *) + for dir in ~/.config/google-chrome/Default ~/.config/chromium/Default ~/Library/Application\ Support/Google/Chrome/Default; do + if [[ -f "$dir/History" ]]; then + cp -f "$dir/History" "/tmp/_h" + sqlite3 -separator $sep "/tmp/_h" \ + "SELECT substr(title, 1, $((COLUMNS / 3))), url \ + FROM urls ORDER BY last_visit_time DESC" | _do_fzf + break + fi + done + ;; +esac