#100DaysOfWeb in Python Transcripts
Chapter: Days 77-80: Twitter and Slack bots
Lecture: Post to Twitter with tweepy

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Next we're going to authenticate with the Twitter API and post this Tweet to my timeline. First I'm going to complete Twitter authenticate
0:10 which is a helper function to connect to the API. And we're going to use the Tweepy module which has an OAuth handler.
0:37 The auth object I created gets the set access token which takes the other two keys we set up before.
0:58 And it returns a Tweepy API object. So now I have this nice utility function to authenticate me with the API.
1:13 On to the exciting stuff, posting to my timeline. This becomes very easy with Tweepy. So first of all, I make my function so that
1:27 I first authenticate, that gives me an API object and I'm passing that into the function. So here we got the Tweet
1:35 then I call Tweet status, an API, and the Tweet text. And with Tweepy, the only thing I have to do is call api.update_status Tweet.
1:52 I'm going to log that. I'm using fstring, which I can do starting Python 3.6 which makes it possible to embed variables.
2:16 I probably should refactor this to raise an explicit exception but for now I'm going to catch any exception and just log that as an error.
2:38 The only thing that we need to make sure of is that we Tweet the status one time cause I don't want to spam my timeline with the same day over and over
2:48 might it happen that this script runs various times. So I'm going to make a helper function, already posted and if that happens, then I'm going to say
3:02 warning get post to Twitter. Tweet already posted.
3:20 I'm going to early return. Now how do we know if a Tweet already is posted? I'm going to go through the log file because every Tweet we post is logged
3:36 so I should have that Tweet string in the log. If it's in the log I assume that's because it was already posted.
3:45 So I'm going to open the log file. Log file which we define here. And here I give you a nice Pythonic construct
4:03 can do any, so we can do for line in read lines. So this gives me an iterator of all the lines and then I can say
4:18 Tweet in line for all the lines in the log file if the Tweet is in any of these lines in any of these lines this returns true.
4:29 If Tweet is not found in any line this any returns to a false so this just returns true or false. So this will be the same as writing
4:51 in a function. Instead of four lines, we do it in a single line. So if already posted we log a warning, return
5:03 else we post to my timeline, log the Tweet if any problem, we log an error. Here you see the nice thing about logging
5:12 is you can say error, info, warning according to the event you want to log. So let's try this out.
5:24 All right, no errors, because they would have been logged. So let's check the log. I notice we get a lot of stuff here.
5:33 So here is stuff from, I think Tweepy stuff from requests, urllib. So all these modules have logging enabled and we get that as well.
5:47 And I got an error, which is great for a demonstration. So I put the wrong secret key in so let me fix that in the background and try it again.
5:58 All right I fixed my consumer and secret authentication tokens so let's try this again.
6:17 Successfully posted to Twitter, cool. Let's see how that looks. And there we go, 12 seconds ago. Day three, 100 days ago progress. Great.
6:34 And 100 days of code bop directly auto Tweeted, beautiful. Okay, let's try that again let's see if it actually prevents me from posting again.
6:52 And I get a warning, skip post to twitter day 3 was already posted. So that's great. So we have a working script. In the last video for a coding demo
7:03 we're going to use argparse to pass in the username and the project dynamically.


Talk Python's Mastodon Michael Kennedy's Mastodon