Last active
December 11, 2015 06:19
-
-
Save jeremyjbowers/4558864 to your computer and use it in GitHub Desktop.
Get counts by voting type.
This file contains hidden or 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 requests | |
from collections import defaultdict | |
base_url = 'http://api.tumblr.com/v2/blog/inauguration2013.tumblr.com/posts/photo' | |
key_param = '?api_key=Cxp2JzyA03QxmQixf7Fee0oIYaFtBTTHKzRA0AveHlh094bwDH' | |
limit_param = '&limit=20' | |
limit = 20 | |
new_limit = limit | |
r = requests.get(base_url + key_param) | |
total_count = int(json.loads(r.content)['response']['total_posts']) | |
pages_count = (total_count / limit) | |
pages_remainder = (total_count % limit) | |
if pages_remainder > 0: | |
pages_count += 1 | |
pages = range(0, pages_count) | |
categories = defaultdict(int) | |
for page in pages: | |
start_number = new_limit - limit | |
end_number = new_limit | |
if end_number > total_count: | |
end_number = total_count | |
new_limit = new_limit + limit | |
page_param = '&offset=%s' % start_number | |
page_url = base_url + key_param + limit_param + page_param | |
r = requests.get(page_url) | |
posts = json.loads(r.content) | |
for post in posts['response']['posts']: | |
for tag in post['tags']: | |
if 'vot' in tag: | |
categories[tag] += 1 | |
for tag, count in categories.items(): | |
percent = float(float(count) / float(total_count) * 100) | |
print '%s: %s posts, %.2f percent' % (tag, count, percent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment