Automated Twitter Bot in Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on guys welcome back in today's video we're going to learn how to build a simple twitter bot in python so let's get right into it now as always when we're using twitter in python we need to visit the developer.twitter.com website which is the developer portal you need to make sure you have a twitter account you need to make sure that you have a developer account that you can just activate it's not too complicated and you go to projects and apps and you go to overview and then you just create a new app now i have one already from my neural nine sentiment analysis video um it's not too complicated to set up an app i'm not going to do the whole process you just created and the important thing is that you're going to get four keys you're going to get um what was it you're going to get the access token the access token secret and then two other tokens one secret and one public uh those are important you need to get them you need to copy them i think you get them by clicking on keys and tokens and then you can get the api key and the api secret key and then you get the access token and the access token secret key so you can regenerate them and then you just have to write them down somewhere so in my case i have a file called twitter keys you can name it whatever you want i have all the four keys that we're going to need in this file you can also put them in a file or you can put them directly in the code if you want to i'm not going to put them directly in the code because i'm recording so you would see my keys you could use my api and then post on my twitter profile which is of course not something that i want you to do so if you don't have to hide anything you can just paste it in a code otherwise i recommend using a file and maybe even encrypting it i'm not going to do it for this video now uh once you have that you need to open up a command line or a terminal and you need to say pip install twepi this is the library that we're going to use tweetpie for tweeting and python i'm not going to do it or actually i can just press enter and you're going to see that i have already installed it so it's not going to do anything here uh and once you have installed we pi you need to import it and then we can start with the actual keys now the important thing is we need first uh we need to first get all the keys we need to store them we need to authenticate we need to set up the api and then we can start uh coding the actual bot or doing actual stuff on twitter so the first thing we're going to do is we're going to say all keys this is going to be a list of keys we're going to say all keys is open twitter keys or whatever you call your file in reading mode and we're going to get the content immediately we're going to see dot read and then we're going to split lines so we're going to get all the individual lines as individual elements of the all keys object so then we're going to say api key equals all keys 0 api key secret equals all keys 1 and by the way of course if you're not reading it from a file you can just paste the actual keys here so you can say api key equal string and then you can uh put in the key directly if you want to and then we're going to say access token equals all keys too and access token secret equals all keys three there you go so now we have all the keys uh depending on the way you put them in but that that's how you have it you have four variables with the api key the api secret key the access token and the access token secret and what we now do is we create an authenticator so we say authenticator equals twi.off handler and what we do here is we pass the api key and we pass the api key secret in order to authenticate and then what we do is we need to set the access token so that we can actually use the app so we can say uh we have to say authenticator dot set access token and then we pass access token and access token secret there you go and the last thing we need to do before we can start with the actual actions is we need to say api is twepi.api and we're going to pass the authenticator that already has the keys and we're going to say weight on rate limit equals true so that is the basic setup all right so now we're connected to the twitter api and we can do all sorts of things we can follow people we can like stuff we can iterate through stuff we can post we can change descriptions and so on so what this video is going to be about is just showing the functionality so you can then define functions you can then define an assistant if you want to or a basic thing where you can enter commands but i'm just going to show you how to do the individual things you can then uh you know create all sorts of algorithms to do certain things for you so the first thing we're going to do is we're going to follow people and in order to follow people you just need to say api dot create friendship and then you pass the name of the person you want to follow now in this case i'm going to follow my other accounts so i'm currently logged into neural nine and i also have the personal account flooring dead off which is my personal profile so i'm following right now i can unfollow here so i'm no longer following myself uh and then i can go back to my profile for example and i can go into pycharm and say okay follow florian dead off and then it's going to um to follow me so if i run the script now and we don't have any mistakes it should just terminate without telling me anything and now if i look at floor ended up again twitter is always lagging i don't know why they go if i go to my profile you can see that if i refresh this i am following the profile as you can see now i can unfollow again uh even though i unfollowed already it's following my profile so this is a basic way to follow people and of course you can now go ahead and iterate over a list of names or patterns or whatever or just browse some people and then you can create friendships basically following them uh this is for some people maybe a strategy to follow people maybe they follow back maybe someone notices you or something like that all sorts of things that you can do here so let's go to something more basic which is not just following but uh posting something on twitter so you can post stuff on twitter by just saying api dot update status and those of you who follow me on twitter will see this message on my twitter page right now and uh you will know now when you watch the video that i was recording a video back then and here i'm just going to say hey or not hey let's say i am currently recording [Music] a video on uh coding a twitter bot in python this is it so if i now run this script we should see that tweet on my profile so if i go to neural nine you're going to see i'm currently recording a video on coding a twitter bot in python this is it so those people who are going to see this tweet and like this tweet are going to remember that this was the tweet that you're seeing in this video right now um and what we can also do is we can update the profile i'm not sure if i want to do that though so we can go ahead and say api dot update and then we need to say update profile and we can set the description to something else so description equals something now the question is do i want to do that or not because i think you believe me even though i don't want to demonstrate that to you so i'm not going to do that because i would have to copy all of that and reset it but basically by doing that you change the description of your twitter profile by just saying api.updateprofile and then you pass some text and you can replace this stuff here so that's those are three basic functionalities updating your status updating your profile and following people all right so let us look at some more functionalities that are maybe not as simple as a one-liner so let's delete that and let's do something like getting information about a user how do we do that we define a user object you can call it user or whatever you want and we say api dot get user and then we can pass a name now i can pass neural nine here or i can pass any other user i'm going to pass my other profile again my personal profile and i'm going to get that user now i can do very simple stuff like printing the name of the user so print user.name which is actually the one thing that i passed but i can also print the description here for example so if i run this script you're going to see uh the name and the description as you can see here owner of neural nine um and we can also go ahead and take a look at the followers so this is not a one-liner but we can actually iterate over the list of the followers so we can say for follower in user dot followers we can just go ahead and say um follower let's make an f string out of it follower follows user dot name and maybe you're going to see some of you guys here but we have a problem here what do we have we actually need to print follower dot name because follower itself is a user object so follower dot name follows user dot name there you go and you can see here okay neural nine follows lorenzo this is myself and vincent drew uh all these people down here maybe some of you guys are amongst them uh and of course we can change this to neural nine and you're going to see that we get maybe some similar names but maybe also some different names so you can see some people following neural nine here um so this is how you can iterate over the followers or through through the followers one thing that we can also do is we cannot just iterate over users that are followers of a certain profile we can also iterate over posts for example in the timeline and what we do for that is we have to create a variable again let's call it tweets home which is just just some tweets on my home page and we're going to say api dot home timeline and i'm going to say okay give me 10 posts from that timeline and what i'm going to do now is i'm going to say okay for tweet in tweets home so for all these tweets that i have now gathered i'm going to say okay if the tweet dot author dot name so the username of the author of the tweet if that is not myself so if that is not neural nine i'm going to like that tweet because i don't want to like my own tweets that's a little bit pathetic so if the name is not neural nine actually i'm not sure if i have to say neural nine like that uh if it's case sensitive or not but i'm going to leave it like that um and we're going to say okay if author or let's let's make it like that if tweet author name dot lower just to make sure um is not equal to neural nine then we're going to say also if not tweet favorited which basically means if i have not liked this tweet yet because if you try to like a tweet that you have already liked uh you're going to get an exception and the program is going to crash so you want to make sure you're not liking your own stuff you want to make sure that you're not liking something that you have already liked and if those two conditions are met you're just going to say api dot create favorite and you're going to pass the tweet dot id and we can also print a message here we can say print f string liking tweets uh and we can pass the author here so tweet dot author dot name and you can see who posted this tweet so there you go let's run this and you can see i'm stopping this here you can see okay python software foundation free code camp lex friedman deepmind uh harrison kingsley i think that's uh syntax um and if we go on twitter i'm not sure if we're going to see these posts or these tweets that i have just liked but if they are on the timeline here there you go python software foundation liked i didn't like this manually this post also liked this post also liked lex friedman's post also liked and so on so you can see that it works the script the bot just liked a couple of posts from my timeline now we can do something similar here but a little bit differently we can also iterate over the timeline of a specific user so instead of just looking at the home timeline we can target a specific user and like their posts so if we say something like tweets uh user not the most creative name i know and we're going to say dot user timeline and uh user id is going to be user uh dot id so basically the id of oh actually we did we deleted that right so let's just remove all of this here and let's create a user object again let's say user how did i define it user equals api dot user and we can pass something like lex friedman for example and now we can go ahead and say okay for tweet in tweets user so for all the tweets in the timeline that we can access with the api we're going to say okay if we haven't favorited that tweet so if we haven't liked that tweet we're going to like it i'm going to say api dot create not friendship but favorite tweet dot id there you go so we're not going to print any messages but we can go to lex friedman's um twitter profile and you're going to see that i haven't liked the most recent posts so i have like this one but a lot of them are not liked by me so what we're going to do now is we're going to run this script and we're going to see if this is going to change i'm not sure how long it will go maybe it will like 10 or 20 tweets maybe it will like 100 maybe it's not going to stop unless i terminate it now it terminated itself so let's see reload the page here and we're going to see that probably most of the posts here are like now or favorited uh there you go favorite it favorited like like uh there you go liked like as you can see this works i targeted this specific user i liked all the posts so this is also something that we can use uh that we can do with tweetpie all right so last but not least i want to show you how you can search for stuff on twitter using the tweetpie module and i have actually showed you that in the sentiment analysis tutorial already but we're going to do it again here so we need to create a cursor we need to say tweets equals tweeppi dot cursor and we're going to pass here the api dot search we're going to pass a search term which is q equals and we're going to pass something like brains for example and we're going to pass the language which is english so en now in order to get the actual tweets we need to say dot items and how many items we want let's say 10 for example and then we can go ahead and print those by saying i hope we can print a list comprehension uh we're going to say tweet uh no tweet for tweet in tweets that should work there you go so if we now run this we should see a bunch of tweets where actually we are going to see tweet objects there you go those are the objects we need to get the text from the tweets so tweets.txt because we're not interested in some metadata here unless you are of course but there you go you can see okay at someone literally didn't say anything bad about their collective brains are the size of a p supporting pal okay we don't want to get political here uh so we're going to skip that part but however you can just enter some search term for example you can also say bitcoin um and we're going to run that again i don't get how elon can run four companies have six kids with two different women trying to get humans to mars and still find and then probably something about uh bitcoin now if we if we run the script we're going to get the same results so maybe i can actually i'm not sure if we can index list comprehensions but looks like we can so maybe we can see the full tweet there uh no it's not the same so maybe changes or maybe it was not index zero but index one uh i don't know however what you can do is you can iterate over the tweets we can you can look for stuff you can also add some uh more stuff like synths if you wanna uh narrow down the the time frame so you can say okay i don't want to see any tweets that are older than 10 days which i think you're not able to query any tweets that are older than seven days anyways with the default version of the api however this is how you can iterate over tweets you can look at the metadata you can look at the text of the of those tweets um but that's also quite a nice feature so that's it for today's video hope you enjoyed it hope you learned something if so let me know by hitting the like button and leaving a comment in the comment section down below and of course don't forget to subscribe to this channel and hit the notification bell to not miss a single future video for free other than that thank you very much for watching see you next video and bye [Music] you
Info
Channel: NeuralNine
Views: 5,630
Rating: 4.951807 out of 5
Keywords: twitter bot, twitter api, python twitter bot, twitter, api, bot, automatic, automate twitter, python bot, like bot, python twitter api, tweepy, tutorial, python twitter tutorial
Id: UGv_bJkF1kg
Channel Id: undefined
Length: 19min 3sec (1143 seconds)
Published: Thu May 27 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.