So we've created this generous bot that follows back.
But what about a narcissist bot?
Maybe it bought that loves your own tweets or loves a tweet based on some certain words.
How can we build that?
Well, let's remove this generous bot for now, or at least comment it out and leave it at the bottom.
What we want to do is let's say we want to search the keywords.
For now, let's just search through number of tweets and we'll set it at two.
We'll get to what these mean in a second.
What we're going to do here is I'm going to say for.
Tweet.
In the.
Tweep dot cursor again this time around, instead of going through followers, I want to go through
tweets.
So in here I can do something called API search.
If we go to the documentation here and we go to the API reference, we see we have API search that returns
tweets that match as specified query.
Again.
If I go back, I can just say API dot search.
To search the tweets and we just search based on the term, which is right here search.
Let's rename this just so we know its different search string.
Let's make this a little bit smaller.
So this is going to be search string.
So we're going to search the tweets with the search string of Python.
So we're going to try and find all the tweets that contain the word python.
And I'm also going to do items in here so we can loop through them.
But within the items I can give it how many items that we want to loop through.
In this case, we just want to find the two tweets.
So a number of items, it's going to be two.
And in here.
We're going to have another tri bloc.
And this tri bloc is just going to say tweet.
Dot favorite to favorite the tweet.
That includes Python and we'll just print to make sure that it worked.
I liked that tweet.
Finally here, we can also add the limit handler to this cursor, but because we're only going through
two items, I think we should be fine.
Although it's always a good idea to wrap the limit handler with a tweep cursor like we did before.
But I just want to keep this clean so you can see what's going on.
Now in here, we've printed this.
We'll do an accept block here.
That will say.
Tweety Dot.
Tweet by error.
So this is if the tweep errors, we'll just say as E and in here we'll just print the reason for the
error.
And then finally, if we get any stop iteration errors in that case, we can just break out of the loop.
So let's save this and see if this works.
Going to open up my console and run.
All right, we get an air.
We have to pee.
Has no attribute to be air.
And that's because it should be tweep error.
Again, let's run this.
And oh, boy, we have another error here.
This time around, there's no tweet favorite.
But if I go to the API and I do, let's say favorite.
Hmm.
I don't get any results.
And this is because I'm Canadian and fun fact if you're Canadian, this is how you spell favourite.
But in the States you spell favorite like this with an oh, there you go.
We have a favorite method here.
So again, fun fact I am Canadian but also Romanian and that was my spelling error because well tpy
was obviously written by person speaking American English.
All right, so let's give it a go.
And it looks like we have twice.
I like that tweet.
Let's go back to check of this word.
If I go back to, let's say, profile and look at likes.
And look at that.
I've liked a tweet that happened one hour ago that had the word python.
And another tweet that I have no idea what's saying, but looks like there's Python in here.
Yeah, look, there's Python right over there.
Hopefully none of these say anything bad.
But look at that.
You can see here that I have liked these tweets.
I'm going to remove that just because I don't know if I should be like these.
There you go.
How cool is that?
So now our narcissistic tweet can potentially just tweet anything that says our own name or like our
own name if there's ever a tweet that has my own name.
Well, let's do that.
Let's.
Like the five tweets that have my own name and my narcissistic bot will hopefully work and look at that.
This is kind of embarrassing, but it looks like I've already liked two of those tweets.
You have already favorited this status.
But let's go back phi refresh here now.
All right, look at that.
I've favorited all the tweets that include my names and looks like these ones I've already liked before.
Very, very cool.
There is my narcissistic bot.
And you can do different things here.
For example, I can retweet as well if I wanted to instead of favorite.
So now you know how on Twitter you always have those bots that somehow always retweet something?
Well, this is what they do.
They look for a keyword like JavaScript or Python or whatever, and they automatically retweet it.
Very, very cool.
As you can see, this tweet library is very useful and combined with the Twitter API, you can do some
really interesting things.
But again, you want to be responsible here because there's a lot of ways that you can abuse and just
spam people with Twitter.
But hopefully now you see some use cases.
For example, if you want to follow back all your followers instead of clicking through every single
profile, you now have a programmer's way of solving that problem with good ol Python.
I'll see you in the next one.
Bye bye.