#100DaysOfCode in Python Transcripts
Chapter: Days 58-60: Twitter data analysis with Python
Lecture: Get all tweets with tweepy.Cursor
0:00 Alright, let's do some coding.
0:02 Finally. First, let's import the modules
0:05 we're going to use and do some set up.
0:08 Then, I'm going to define a namedtuple called Tweet.
0:16 And next, I'm going to set some global variables,
0:20 which, in Python, are uppercase,
0:23 and words divided by underscore.
0:26 So, we're going to look at Twitter data from our account
0:29 and here are the environment variables explained
0:31 in last video, loaded into the notebook.
0:34 And you can use os environment or os.environ
0:39 and you can make a script in your favorite editor.
0:42 Or follow along in the notebook
0:44 but notice to load a virtual environment in iPython,
0:49 there is some set up you might need to do.
0:52 So, here's a link and a command you can run
0:55 to get the virtual environment loaded into your notebook.
0:58 That's all set. Lets dive straight into getting
1:02 PyBite's Twitter history which is over two thousand tweets.
1:06 And we're going to look at the most popular tweets
1:09 by the number of likes and retweets
1:10 the most common hashtags and mentions
1:14 and finally, create a nice Wordcloud and we will see that
1:17 Tweepy is awesome in making this very easy.
1:20 Alright, let's make an API object first.
1:31 Alright, and then let's define a function
1:34 to get all our tweets.
1:47 Wow, that's a lot going on here.
1:49 So, we use a Tweepy cursor, which is an efficient
1:53 way to loop all over the tweets.
1:56 And, I'm going to access the user time line,
1:59 which is basically all our tweets,
2:02 screen name is PyBites and for now,
2:05 I'm not going to show replies
2:06 because I need them later.
2:08 I'm going to include the retweets,
2:10 so, basically I get everything.
2:13 And that's good because we can always discard stuff later.
2:16 But we can not put stuff back that was initially not there.
2:20 So, then to loop over it, I use the items
2:23 on that cursor, we had a namedtuple at the beginning,
2:26 with id text created likes and retweets fields,
2:29 and I'm just populating those and yielding
2:32 each tweet one by one.
2:34 So, this is basically a generator,
2:36 which we covered in Day 16.
2:40 Then let's load it on to a list,
2:42 which might take a while,
2:43 but makes it easier to inspect.
2:48 And let's see how big that is.
2:53 Great, so, we have 2400 tweets.
2:56 Let's see what we can do with those tweets.