#!/usr/bin/python
import sys
from optparse import OptionParser
try:
  from howmanyhits.howmanyhits import googleTask,bingTask,yahooTask
except ImportError:
  print "The corresponding class file was not installed. Check your python-pysubdomainer installation"
  sys.exit(1)

DEFAULT_SITE_RESTRICTION = False
query_some = {}
query_all = False
targets = ['Google','Yahoo!','Bing']
parser = OptionParser()
parser.description ="Query Google and Yahoo! for the number of results for a search term"
parser.add_option("-g","--google",
               action="store_true", dest="google", default=False,
               help="Query Google for the number of search results")
parser.add_option("-y","--yahoo",
               action="store_true", dest="yahoo", default=False,
               help="Query Yahoo! for the number of search results")
parser.add_option("-b","--bing",
               action="store_true", dest="bing", default=False,
               help="Query Bing for the number of search results")
parser.add_option("-s","--site",
               action="store_true", dest="siteonly", default=DEFAULT_SITE_RESTRICTION,
               help="Restrict search using site: prefix")
(options, args) = parser.parse_args()
if not len(args):
  print "You have to add exactly one search term (domain) to query"
  sys.exit(1)
y = yahooTask(args[0],site=options.siteonly)
g = googleTask(args[0],site=options.siteonly)
b = bingTask(args[0],site=options.siteonly)
if not options.google and not options.yahoo and not options.bing:
  query_all=True
else:
  if options.google: query_some['Google'] = g.totalResults()
  if options.yahoo:  query_some['Yahoo!'] = y.totalResults()
  if options.bing:   query_some['Bing'] = b.totalResults()
if query_all:
  print "Google found:\t%li"%g.totalResults()
  print "Yahoo found:\t%li"%y.totalResults()
  print "Bing found:\t%li"%b.totalResults()
  sys.exit(0)
else:
  for target in targets:
    if query_some.has_key(target):
      print "%s found:\t%li"%(target,query_some[target])
  sys.exit(0)

