Created
June 23, 2011 17:27
-
-
Save gma/1043057 to your computer and use it in GitHub Desktop.
How old are @Hubmum's followers?
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
#!/usr/bin/env python | |
# Quick hack to count the age of people replying to @hubmum's survey. | |
# https://twitter.com/hubmum/status/83935023895556098 | |
# Limitations: | |
# - It only looks at the last 100 replies to @hubmum | |
# (it needs to be able to sift through multiple pages) | |
# - It doesn't support polish notation (sorry @pubstrat) | |
# I've got to go to the pub, but maybe another Python hacker can pick up, | |
# fork this gist, and improve it somewhat? ;-) -- @grahamashton | |
import sys | |
import re | |
import feedparser | |
def search_url(): | |
params = { | |
"q": "to%3Ahubmum", | |
"since_id": "83935023895556098", | |
"rpp": "100" | |
} | |
query_string = "&".join(["=".join(pair) for pair in params.items()]) | |
return "http://search.twitter.com/search.atom?" + query_string | |
def get_ages(): | |
pattern = re.compile(r'(\d+)') | |
results = {} | |
feed = feedparser.parse(search_url()) | |
for tweet in feed["entries"]: | |
match = pattern.search(tweet["title"]) | |
if match: | |
age = int(match.group(1)) | |
if age < 10 or age > 90: | |
try: | |
sys.stderr.write("Suspect tweet: %s\n" % tweet["title"]) | |
except UnicodeEncodeError: | |
pass | |
else: | |
results[age] = results.get(age, 0) + 1 | |
return results | |
def main(): | |
results = get_ages() | |
for age in sorted(results.keys()): | |
print "%s, %s" % (age, results[age]) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment