import urllib, urllib2 import json import sys import webbrowser import random ## While probably very lazily/badly programmed, hopefully it functions decently. def yes_no(question): "Returns 1 for a positive response and 0 for a negative response" s1 = "" pos = ('y', 'yes', 'yup', 'affirmative') neg = ('n', 'no', 'nope', 'negative') responses = pos + neg while not s1 in responses: s1 = raw_input(question).lower() if not s1 in responses: print 'Yes or no response, please.', if s1 in pos: return True elif s1 in neg: return False def get_query(): "Gets a query from the user" #default search is 'computational thought' query = {'q' : 'computational thought'} #checks for argument, else default query if 1 < len(sys.argv): #previous version only searched first term query['q'] = sys.argv[1:] else: new_search = yes_no('Would you like to enter your own search? ') if new_search: query['q'] = raw_input("Enter your search term: ") search_term = ''.join(query['q']) print 'Search results for:', search_term return query def perform_search(query, start): "Performs a search given a query and a starting result number" referrer = "http://cs.nyu.edu/q/3900610" if userip: query.update(userip=userip) if api_key: query.update(key=api_key) #performs the search with the query url = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&start=%(res)s&%(query)s' % \ {"res": start, "query": urllib.urlencode(query)} #requests and processes results request = urllib2.Request(url, headers=dict(Referrer=referrer)) response = urllib2.urlopen(request) data = json.load(response) results = data['responseData'] return results def print_results(results, start): "Prints results given a set of results, numbers them according to 'start'" print '' for r in results: start += 1 print 'Result', start #using titleNoFormatting eliminates tags, encode in case of unicode print 'Title:', r['titleNoFormatting'].encode('utf-8', 'replace') #using unescapedUrl... looks better? print 'URL:', r['unescapedUrl'] #print 'Content:', r['content'].encode('utf-8', 'replace') print "----------" if start == 0: print 'There are no results.' return 0 return start def collect_url(results): "Collects a set of URLs given a set of results" urls = [] for r in results: urls.append(r['url']) return urls def open_url(urls, res_num): url_open = urls[res_num] fp = webbrowser.open(url_open) open_again = yes_no('Would you like to open another page? ') if not open_again: print 'Happy browsing.', return False return True def open_results(urls, count): "Opens a search result if prompted" res_raw, res_num = "", -1 while True: if count == 0: print 'I must be a bad program.' break res_raw = raw_input("Type result number to view ('q' to exit, 'l' for random): ").lower() if res_raw.isdigit(): res_num = int(res_raw) - 1 if 0 <= res_num < count: cont = open_url(urls, res_num) if not cont: break else: print 'Out of range.', elif res_raw in ('lucky', 'l', 'random', 'r'): rand = random.randrange(0, count) cont = open_url(urls, rand) if not cont: break elif res_raw in ('q', 'quit'): break else: print 'Invalid input.', def handle_results(query): "Performs search, prints results, and opens results as needed" start, count, urls = 0, 0, [] while True: try: data = perform_search(query, start) results = data['results'] except TypeError: if start > 0: print 'End of available results.', else: print 'No results found.', break #64 seems to be the maximum amount of results allowed, remove if/else if otherwise try: total = int(data['cursor']['resultCount'].replace(',', '')) except KeyError: print 'There are no results.', break if count < 64: count = total else: count = 64 start = print_results(results, start) urls += collect_url(results) if start > 0 and start < count: next_page = yes_no('Would you like more results? ') if not next_page: break else: print 'End of available results.', break if start < count: open_results(urls, start) else: open_results(urls, count) def handle_search(): while True: query = get_query() handle_results(query) search_again = yes_no('Would you like to perform a new search? ') if not search_again: break #================================================ api_key, userip = None, None print '\nWelcome to googlesearcher++.' handle_search() print 'Thank you for using googlesearcher++.'