Last active
July 14, 2017 18:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: