Skip to content

Instantly share code, notes, and snippets.

@brennv
Last active July 14, 2017 18:38
Show Gist options
  • Save brennv/aeb4c1784698bf31861c1d59671d9a5f to your computer and use it in GitHub Desktop.
Save brennv/aeb4c1784698bf31861c1d59671d9a5f to your computer and use it in GitHub Desktop.
jump from the command line to the browser and search against specified sites
# Start a search against multiple sites from the command line
# Usage: python duckshift.py <search terms>
import sys
import subprocess
import webbrowser
try:
from urllib.parse import quote
except ImportError:
from urllib import quote # python 2
sites = """
stackoverflow.com
unix.stackexchange.com
serverfault.com
superuser.com
bugzilla.redhat.com
access.redhat.com/solutions
access.redhat.com/articles
openshift.com
github.com/openshift
trello.com
kubernetes.io
"""
def make_url(terms=[], sites=[]):
""" Compose search terms and sites with url safe encoding. """
terms = [quote(x, safe='') for x in terms]
sites = ','.join(sites.strip().split('\n'))
sites = quote('site:' + sites, safe='')
url = 'https://duckduckgo.com/?q=' + '+'.join(terms + [sites])
return url
def browse(url):
""" Visit url with system browser. """
if sys.platform == 'darwin':
subprocess.Popen(['open', url])
else:
webbrowser.open_new_tab(url)
pass
if __name__ == '__main__':
terms = sys.argv[1:]
url = make_url(terms, sites=sites)
print(url)
check = input('Open url in browser? Y|n ')
if not check.strip().lower().startswith('n'):
browse(url)
pass
@brennv
Copy link
Author

brennv commented Jul 14, 2017

Example usage:

$ alias duckshift="python ~/gists/duckshift.py"
$ duckshift error foo bar
https://duckduckgo.com/?q=error+foo+bar+site%3Astackoverflow.com%2Cunix.stackexchange.com%2Cserverfault.com%2Csuperuser.com%2Cbugzilla.redhat.com%2Caccess.redhat.com%2Fsolutions%2Caccess.redhat.com%2Farticles%2Copenshift.com%2Cgithub.com%2Fopenshift%2Ctrello.com%2Ckubernetes.io
Open url in browser? Y|n

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment