Created
October 5, 2010 14:38
-
-
Save quandyfactory/611660 to your computer and use it in GitHub Desktop.
Checks a list of names to determine whether they are in the top four Google search results.
This file contains 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
import json | |
import urllib | |
import urllib2 | |
candidates = ['David Sweet', 'James W. Byron', 'Brad Clark', 'Terry Anderson', 'Stephen E. Brotherston', 'Dave Braden', 'Annie Tennier', 'Michelle Stockwell', 'Marie Bountrogianni', 'Nancy MacBain', 'David Christopherson', 'Wayne Marston', 'Chris Charlton', 'Peter Ormond', 'Karen Burson', 'David Hart Dyke', 'Dean Allison', 'Stephen Henry Bieda', 'David Heatley', 'Bryan Jongbloed', 'Jim Enos', 'Michael James Baldasaro', 'Jamile Ghaddar', 'Anthony Giles', 'Lisa Nussey', 'Wendell Fields', 'Bob Green Innes', 'Bob Mann', 'Greg Pattinson', 'Henryk Adamiec', 'Sid Frere', 'Gord Hill'] | |
url_template = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={{q}}&key={{key}}&userip={{userip}}' | |
key = 'YOUR-GOOGLE-API-KEY' | |
referer = 'http://your.domain.com' | |
userip = '127.0.0.1' | |
# uncomment if working from behind a proxy server | |
# proxy_handler = urllib2.ProxyHandler({'http': 'http://proxy.server.ca'}) | |
# opener = urllib2.build_opener(proxy_handler) | |
# urllib2.install_opener(opener) | |
def get_results(addon='candidate', display_all=False): | |
"""Gets the results of the Google queries""" | |
total = 0 | |
total_in_results = 0 | |
for candidate in candidates: | |
total += 1 | |
q = '%s %s' % (get_straight_name(candidate), addon) | |
url = make_url(q) | |
request = urllib2.Request(url, None, {'Referer': referer}) | |
response = urllib2.urlopen(request) | |
content = json.loads(response.read()) | |
results = content['responseData']['results'] | |
print '%s - ' % (q), | |
in_top_four = False | |
if display_all == True: print | |
for result in results: | |
if display_all==True: | |
print result['titleNoFormatting'], result['url'] | |
if 'elections.raisethehammer.org' in result['url']: | |
in_top_four = True | |
if in_top_four == True: | |
print 'Yes' | |
total_in_results += 1 | |
else: | |
print 'No' | |
print 'Summary: RTH Elections is in the top four results for %s out of %s candidates.' % (total_in_results, total) | |
def make_url(q, url_template=url_template, key=key, userip=userip): | |
"""Makes an URL""" | |
return url_template.replace('{{q}}', urllib.quote_plus(q)).replace('{{key}}', key).replace('{{userip}}', userip) | |
def get_straight_name(name): | |
"""Takes a `Lastname, Firstname` string and returns a `Firstname Lastname` string""" | |
namelist = name.split(',') | |
namelen = len(namelist) | |
outname = [] | |
for i in range(namelen): outname.append(namelist[namelen-i-1].strip()) | |
return ' '.join(outname) | |
if __name__ == '__main__': | |
get_results() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment