Guest User

buzz_stats.py

a guest
Mar 10th, 2010
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. #!/opt/local/bin/python2.6
  2.  
  3. import feedparser
  4. import httplib2
  5. import re
  6. import sys
  7.  
  8. BASE_FEED_URL = 'http://buzz.googleapis.com/feeds/%s/public/posted'
  9.  
  10. HTTP = httplib2.Http("/tmp/httplib2-cache")
  11.  
  12. def parse_feed(feed_url):
  13.   response, content = HTTP.request(feed_url, "GET")
  14.   assert response.status == 200
  15.   return feedparser.parse(content)
  16.  
  17. def main(argv):
  18.   if len(argv) != 2:
  19.     print 'Usage: buzz_feed.py username'
  20.     return 1
  21.   feed = parse_feed(BASE_FEED_URL % argv[1])
  22.   total_posts = len(feed.entries)
  23.   total_words = 0
  24.   total_chars = 0
  25.   total_replies = 0
  26.   total_reply_words = 0
  27.   total_reply_chars = 0
  28.   total_newlines = 0
  29.   reply_authors = set()
  30.   for entry in feed.entries:
  31.     total_words += len(re.split('\W+', entry.content[0].value))
  32.     total_chars += len(entry.content[0].value)
  33.     total_replies += int(entry.total)
  34.     total_newlines += entry.content[0].value.count('\n')
  35.     for link in entry.links:
  36.       if link.rel == 'replies':
  37.         reply_feed = parse_feed(link.href)
  38.         for reply in reply_feed.entries:
  39.           total_reply_words += len(re.split('\W+', reply.content[0].value))
  40.           total_reply_chars += len(reply.content[0].value)
  41.           reply_authors.add(reply.author)
  42.   print 'Number of posts: %d' % total_posts
  43.   print 'Total words in %d posts: %d' % (total_posts, total_words)
  44.   print 'Total characters: %d' % total_chars
  45.   print 'Total newlines: %d' % total_newlines
  46.   print 'Average words per post: %d' % (total_words / total_posts)
  47.   print 'Average chars per post: %d' % (total_chars / total_posts)
  48.   print 'Total replies: %d' % total_replies
  49.   print 'Average number of replies: %d' % (total_replies / total_posts)
  50.   print 'Average number of words per reply: %d' % (total_reply_words / total_replies)
  51.   print 'Average number of characters per reply: %d' % (total_reply_chars / total_replies)
  52.   print 'Number of unique authors replying: %d' % len(reply_authors)
  53.  
  54. if __name__ == '__main__':
  55.   main(sys.argv)
Advertisement
Add Comment
Please, Sign In to add comment