Skip to content

Instantly share code, notes, and snippets.

@bahoo
Last active February 16, 2016 21:37
Show Gist options
  • Save bahoo/d4796106359b7ac37a50 to your computer and use it in GitHub Desktop.
Save bahoo/d4796106359b7ac37a50 to your computer and use it in GitHub Desktop.
Which presidential candidate has the most US based likes on Facebook?
# for context, many marketing teams will buy cheap likes on Facebook by running
# ads in countires where US presidential elections don't take place.
# this can produce misleading counts of "Likes" when you visit their page.
# looking at only US-based likes will give you a more honest assessment of
# relative popularity of the candidates on Facebook in the US.
# but of course, YMMV. :)
import requests
# I can't get these warnings to hide, but w/e.
# requests.packages.urllib3.disable_warnings()
candidates = ['berniesanders', 'hillaryclinton', 'donaldtrump', 'jebbush', 'realbencarson', 'marcorubio', 'tedcruzpage']
ACCESS_TOKEN = '...' # create a Facebook app, or borrow a short-lived token from https://developers.facebook.com/tools/explorer/
for candidate in candidates:
resp = requests.get("https://graph.facebook.com/v2.4/%s/insights/page_fans_country?access_token=%s&format=json&method=get&pretty=0&suppress_http_code=1" % (candidate, ACCESS_TOKEN), verify=False)
print "%s: %s" % (candidate, resp.json()['data'][0]['values'][-1]['value']['US'])
# sorted output, as of 7:30am PT, November 25th, 2015
# realbencarson: 4,251,439
# donaldtrump: 3,048,503
# berniesanders: 1,829,684
# tedcruzpage: 1,545,941
# hillaryclinton: 1,102,897
# marcorubio: 1,079,496
# jebbush: 239,448
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment