Simple Python Twitter api example

책에 있는 예제입니다.

import twitter
import os
import sys
import json

twitter_search = twitter.Twitter(domain="search.twitter.com",secure=None)
trends = twitter_search.trends()
print [ trend['name'] for trend in trends['trends'] ]

search_results=[]
for page in range(1,2):
    search_results.append( twitter_search.search(q="SNL", rpp=100, page=page) )
#   print json.dumps(search_results, sort_keys=True, indent=1)

tweets = [ r['text'] 
    for result in search_results 
        for r in result['results'] ]

#print tweets

words = []
for t in tweets:
    words += [ w for w in t.split() ]

print len(words)
print len(set(words))

print 1.0*len(set(words))/len(words)
print 1.0*sum([ len(t.split()) for t in tweets ])/len(tweets)

import nltk

freq_dist = nltk.FreqDist(words)

import re
rt_patterns = re.compile(r"(RT|via)((?:bW*@w+)+)", re.IGNORECASE)
for t in tweets:
    print rt_patterns.findall(t)
Advertisements