#!/usr/bin/python
# Vincent Kriek
# Mason Larobina
import sys
from os import environ as env, system
from os.path import join
from textwrap import dedent
from urlparse import urlparse
from urllib import quote
XDG_DATA_DIR = env.get('XDG_DATA_HOME', join(env['HOME'], '.local/share/'))
DATA_DIR = join(XDG_DATA_DIR, 'uzbl/')
escape = lambda uri: uri.replace('@', '\\@')
#escape = lambda uri: uri.replace('@', '%40')
#escape = lambda uri: quote(uri, '.:/')
def addBookmark(url, keyword, title):
print "HELLO GOT HERE"
print join(DATA_DIR, 'keywordBookmarks.txt')
fh = open(join(DATA_DIR, 'keywordBookmarks.txt'), 'a')
fh.write("%s %s %s\n" % (url, keyword, title))
fh.close()
createBookmarkpage()
def getBookmark(keyword):
fh = open(join(DATA_DIR, 'keywordBookmarks.txt'), 'r')
for line in fh:
(url, key) = line.split(' ', 2)[:2]
if key == keyword:
return url
def createBookmarkpage():
fh = open(join(DATA_DIR, 'keywordBookmarks.html'), 'w')
htmlStart = """
Bookmarks
Bookmarks
"""
fh.write(dedent(htmlStart))
bookmarks = open(join(DATA_DIR, 'keywordBookmarks.txt'), 'r')
for line in bookmarks:
(url, keyword, title) = (line.strip().split(' ', 2) + ['',])[:3]
urlp = urlparse(url)
favicon = '%s://%s/favicon.ico' % (urlp.scheme, urlp.netloc)
fh.write(dedent('''
%s
%s
''' % (favicon, url, url, keyword)))
fh.write('\n')
fh.close()
# Read script args.
for (index, value) in enumerate(sys.argv): print index, value
(config, pid, x_id, fifo, socket, uri, title) = sys.argv[1:8]
(action, keyword) = map(str.strip, (sys.argv[8:10] + [None,])[:2])
print "ACTION", action
# Add a bookmark
if action == "add":
print "GOT HERE"
assert keyword, "empty keyword"
addBookmark(uri, keyword, title)
# Open a bookmark via keyword
elif action == "get":
url = getBookmark(keyword)
if url:
system('echo "uri %s" > %r' % (escape(url), fifo))
elif action == "open":
#Open the bookmark if found
url = getBookmark(keyword)
if url:
system('echo "uri %s" > %r' % (escape(url), fifo))
#Search for the words on google if it's not a url
elif ' ' in keyword or '.' not in keyword:
system('echo "uri http://www.google.com/search?q=%s" > %r'\
% (quote(keyword, ''), fifo))
#Open as url
else:
system('echo "uri %s" > %r' % (escape(keyword), fifo))
#Same as the previous elif but in a new window("tab")
elif action == "tab":
#Open the bookmark if found
url = getBookmark(keyword)
if url:
system('echo "event NEW_TAB %s" > %r' % (escape(url), fifo))
#Search for the words on google if it's not a url
elif ' ' in keyword or '.' not in keyword:
system('echo "event NEW_TAB http://www.google.com/search?q=%s" > %r'\
% (quote(keyword, ''), fifo))
#Open as url
else:
system('echo "event NEW_TAB %s" > %r' % (escape(keyword), fifo))
===== Installation ======
This has to go in your uzbl/config
set kwbm = @scripts_dir/keyword_bookmarks.py
@cbind a_ = @kwbm add %s
@cbind g_ = @kwbm get %s
@cbind t_ = @kwbm tab %s
@cbind o_ = @kwbm open %s
===== Quick Start ======
When the scripts gets a add, it adds a bookmark and when it sees a get, it navigates to the bookmark. Both times you have to supply a keyword, either for looking up or for adding.
The t and o first look if the entered string is a keyword, if it is, it opens it. If it isn't it checks if it could be a url, if it is, it opens it else it googles the arguments.
When you add a bookmark a page containing all bookmarks gets created. The bookmarks shortcut opens that page. You can customize it using css, $XDG_DATA_HOME/uzbl/bookmarks.css