Created
April 11, 2015 00:57
-
-
Save abdelhai/dfe232840c86e4c7fe54 to your computer and use it in GitHub Desktop.
ages from facebook comments, probably my first python "program" ever
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
# first: pip install requests | |
# second: python fb_ages.py | |
import requests | |
import json | |
import re | |
from collections import Counter, OrderedDict | |
ages = [] | |
# fetch JSON Data | |
comments = requests.get( | |
'http://graph.facebook.com/510085202471863/comments?limit=300').json() | |
# find valid ages and put them in a list | |
for comment in comments['data']: | |
# find ages with two numbers, ignoring single number ages | |
match = re.findall( r'\d{2}', comment['message']) | |
# only get ages where the is only one number in comment | |
if len(match) == 1: | |
ages.append(match[0]) | |
# count & order ages | |
age_count_dict = OrderedDict(sorted(Counter(ages).items(), key=lambda t: t[1], | |
reverse=True)) | |
# print out age, count and percentage | |
for age, count in age_count_dict.items(): | |
print age, count, '%.1f' % round((count / (len(ages)*1.0)) * 100, 2) + '%' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment