Jake.codes

BangSearch, for Pythonista

Tuesday, December 10, 2013

This is a fork of search_script.py from kstrauser which is a fork of BangSearch.py from Phil Gruneich

# Bookmarklet:
#
# javascript:%20s%20=%20prompt('Enter%20search:',%20'');if%20(!s);if%20(s)%20location.href%20=%20'pythonista://BangSearch?action=run&argv='+s;
import sys
import re
import urllib
import webbrowser
import console
 
services = [
    # Web services
    ('s', 'safari-http://stackoverflow.com/search?q=%s'),
 
    # Apps
    ('1p', 'onepassword://search/%s'),
    ('op', 'onepassword://search/%s'),
    ('w', 'wikipanion:///%s'),
    ('wiki', 'wikipanion:///%s'),
    ('tw', 'tweetbot:///search?query=%s'),
    ('tb', 'tweetbot:///search?query=%s'),
    ('imdb', 'imdb:///find?q=%s'),
    ('rd', 'rdio://search/%s'),
    ('app', 'http://phobos.apple.com/WebObjects/MZSearch.woa/wa/search?media=software&term=%s'),
    ('comix', 'safari-http://www.comixology.com/search?search=%s')
]

try:
    query = sys.argv[1]
except:
    try:
        query = console.input_alert('BangSearch', 'Example: !comix X-Men', '', 'Search')
    except:
        query = ''

for shortcut, searchurl in services:
    service_identifier = '!%s' % shortcut
    if not service_identifier in query:
        continue
        
    query = (re.sub(service_identifier,'',query,count=1)).strip()

    break
else:
    searchurl = 'safari-https://duckduckgo.com/?q=%s'

webbrowser.open(searchurl % urllib.quote(query))