#!/usr/bin/python # # goopy: CLI for module goopy. Version 0.5 # Written by Michael G. (mynameisfiber@gmail.com) (copyleft) # # This version can handle: # Websites (local or foreign language) # Video Results # Files (ie: pdf, ps, ppt, etc.) # Recommended Search Terms # # ===================================================================== # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # # ===================================================================== import sys import os from goopy import websearch from urllib2 import URLError if __name__ == "__main__": #######BROWSER CONFIG######## browser="firefox" header = """Goopy: Google for python. Version 0.5\nMade by Michael G.""" usage = """ #: item number to display b "browser": change browser to "browser" g "result#": outputs starting at number h: this help l num: show num results per page n: next page o: open results in browser p: previous page q: quit r: reshow page s "query": preforms new search y: research using recommended query""" if (len(sys.argv) == 1): print ("%s\nUsage: %s \"query\" [results per page] [interactive]\n" +\ "\tquery: What to search for\n" +\ "\tresults per page: how many results to display\n "+\ "\tinteractive: 0 for non-interactive\n"+\ "In-program Commands:\n%s") % (header,sys.argv[0], usage) exit() perPage = 5 if (len(sys.argv) >= 3): perPage = int(sys.argv[2]) if (len(sys.argv) == 4 and sys.argv[3].strip() == '0'): gp = websearch(sys.argv[1]) gp.show_results(0,perPage) exit() input = "s %s" % (sys.argv[1].strip()) while input != "q": if input == "n": result+=perPage elif input == "p": if result>perPage: result-=perPage else: result=0 elif input == "" or input == "r": True #understatment of the century elif input == "o": os.system("%s \"http://google.com/search?q=%s&start=%d\"" \ %(browser,gp.query,gp.offset)) print "Opening results in browser" display = -2 elif input == "h": print header print usage display = -2 elif input == "y": if input == "y" and gp.recommend != "": input = "s %s" % gp.recommend continue else: print "No recommended search" display = -2 elif input.startswith("b"): try: browser = input[2:].strip('" \n') print 'Browser set to "%s"' % browser except: print "Invalid browser string" display = -2 elif input.startswith("s"): try: gp = websearch(input[2:].strip('" \n')) print "Found %s results for '%s'" % (gp.numResults, gp.query) if gp.recommend != "": print "Did you mean \"%s\"? Press y to use this search term."\ % gp.recommend result = 0 display = -1 except URLError, inst: print inst print "No connection to google" display = -2 except: print "Invalid search term" display = -2 elif input.startswith("g"): try: result = int(input[2:]) except: print "Invalid result number" display = -2 elif input.startswith("l"): try: perPage = int(input[2:]) except: print "Invalid integer" display = -2 else: try: display=int(input) print 'Getting item', display os.system("%s \"%s\""%(browser,gp.results[display]["url"])) except: print 'Invalid Input' display = -2 if display == -1: gp.show_results(result,perPage) elif display >= 0: gp.show_results(display,1) display = -1 input = raw_input("\n>").strip()