-
-
Save extratone/763505f23d2be7a39a76a8ed001100a5 to your computer and use it in GitHub Desktop.
GoogleSearch
This file contains hidden or 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
# Google Search for Pythonista (iOS) | |
# Searches Google and copies the first result to the clipboard as | |
# a Markdown link in the form [title](url). | |
# | |
# Inspired by Brett Terpstra's SearchLink: | |
# http://brettterpstra.com/searchlink-automated-markdown-linking-improved/ | |
import clipboard | |
def google(terms): | |
import requests | |
import cgi | |
url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&filter=1&rsz=small&q=' + cgi.escape(terms) | |
r = requests.get(url, headers={'Referer': 'http://bretterpstra.com'}) | |
if r.json: | |
response_data = r.json.get('responseData', None) | |
if response_data: | |
result = response_data['results'][0] | |
output_url = result['unescapedUrl'] | |
output_title = result['titleNoFormatting'] | |
return output_title, output_url | |
if __name__ == '__main__': | |
terms = raw_input('Enter search terms:') | |
title, url = google(terms) | |
print 'First Google Result:' | |
print 'Title:', title | |
print 'URL:', url | |
clipboard.set('[' + title + ']' + '(' + url + ')') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment