Skip to content

Instantly share code, notes, and snippets.

@gcr
Created July 6, 2009 04:04
Show Gist options
  • Select an option

  • Save gcr/141259 to your computer and use it in GitHub Desktop.

Select an option

Save gcr/141259 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Run it like this!
# python ./gs.py 'Sybil' 'sybil.appspot.com'
import sys
from textwrap import TextWrapper
try:
from xgoogle.search import GoogleSearch
except ImportError:
print "You need the xgoogle library! http://www.catonmat.net/download/xgoogle.zip"
sys.exit()
def continuous_results(gs):
r = gs.get_results()
while r:
for result in r:
yield result
r = gs.get_results()
def get_search_match(query, target_url):
s = GoogleSearch(query)
for rank, result in enumerate(continuous_results(s)):
# Progress
if rank % 100 == 0:
sys.stdout.write(".")
sys.stdout.flush()
if target_url in result.url:
return rank, result
return (None, None)
def __main__(query, target_url):
print "Finding a match... query: %s, target URL: %s" % (query, target_url)
rank, result = get_search_match(query, target_url)
if result:
print
tw = TextWrapper(subsequent_indent = " " * 13)
print "Hit!"
print tw.fill("Rank: %s" % rank)
print tw.fill("Name: %s" % result.title)
print tw.fill("Description: %s" % result.desc)
print tw.fill("URL: %s" % result.url)
sys.exit(0)
else:
print
print "No match. Sorry."
sys.exit(1)
if __name__ == '__main__':
__main__(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment