-browser/firefox/bin, +shell/fzf/bin/fzf-{bookmarks,history}

This commit is contained in:
Henrik Lissner 2017-04-25 17:17:22 -04:00
parent 3100ccb884
commit 9c8429477a
6 changed files with 77 additions and 91 deletions

View file

@ -1,7 +0,0 @@
#!/usr/bin/env python3
from lib.FirefoxProfile import FirefoxProfile
with FirefoxProfile.default() as ff:
print("\n".join(ff.bookmarks()))

View file

@ -1,7 +0,0 @@
#!/usr/bin/env python
from lib.FirefoxProfile import FirefoxProfile
with FirefoxProfile.default() as ff:
print("\n".join(ff.history()))

View file

@ -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()

View file

@ -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

View file

@ -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