Skip to content

Instantly share code, notes, and snippets.

@storborg
Created February 27, 2012 18:08
Show Gist options
  • Save storborg/1925914 to your computer and use it in GitHub Desktop.
Save storborg/1925914 to your computer and use it in GitHub Desktop.
getting social media counts for urls
import simplejson as json
from urllib import urlencode
import requests
import re
def facebook_likes_for_url(url):
r = requests.get('http://graph.facebook.com/%s' % url)
obj = json.loads(r.content)
return obj['likes']
def jsonp_api(api_url, target_url):
d = dict(callback='fakeCallback', url=target_url)
r = requests.get(api_url + ('?%s' % urlencode(d)))
m = re.match('fakeCallback\((.+)\)$', r.content)
obj = json.loads(m.group(1))
return obj['count']
def pinterest_pins_for_url(url):
return jsonp_api('http://api.pinterest.com/v1/urls/count.json', url)
def tweets_for_url(url):
return jsonp_api('http://urls.api.twitter.com/1/urls/count.json', url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment