remove readme from the non-solutions
[twitter-api-cdsw-solutions] / solution-topics-1.py
1 # Modify twitter3.py to produce a list of 1000 tweets about a topic of
2 # your choice.
3
4 # Note: I've changed it to search for "community data" instead of "data science."
5
6 import encoding_fix
7 import tweepy
8 from twitter_authentication import CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
9 import time
10
11 auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
12 auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
13
14 api = tweepy.API(auth)
15
16 # 100 is the maximum number taht can be returned according to:
17 # https://dev.twitter.com/rest/reference/get/search/tweets
18
19 counter = 0
20 for page in tweepy.Cursor(api.search, "community data", count=100).pages():
21     counter = counter + len(page)
22     for tweet in page:
23         print(tweet.user.screen_name + "\t" + str(tweet.created_at) + "\t" + tweet.text)
24     # end this loop if we've gotten 1000
25     if counter == 1000:
26         break
27
28     # This page suggests we can do one request every 5 seconds:
29     # https://dev.twitter.com/rest/reference/get/search/tweets
30     time.sleep(5)
31

Benjamin Mako Hill || Want to submit a patch?