Python Twitter Bot That Auto Likes/Retweets Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
good morning my gorgeous lovers my gorgeous friends on the Internet in this episode we're kind of go back in time time traveling on dev edge channel so what's gonna happen is six months ago a that a tutorial that kind of used in Python to automate liking Twitter posts okay but we did it with a browser and it was messy so today what we're gonna do is use the Twitter API and Tweety which is a Python library and we are gonna analyze search terms like Python JavaScript or whatever you want on Twitter and based on those search terms on those tweets we are going to line them or we can retweet them if we want so let's take a look at this is gonna be fun and it's gonna be extremely simple so I highly recommend you watching this hi let's build out this bot feast your eyes amah Pringles that's the name of this cat it was recently renamed to Boris I'm not sure why but okay so the first thing we need to do is to create this bot rather than doing some weird GUI Bui and what we're gonna do is actually sign up to a Twitter API developer thing so this is mine here here are my API keys but I'm gonna regenerate these so I'm gonna show you how you can get these and get started so what you need to do is you need to go to Google and search for Twitter developer like this let me pull this up developer twitter.com so what I'm gonna do is copy this actually and go to incognito and know we're now gonna visit weird web sites we're just gonna go to this one again okay and why you're gonna do is hit apply and then you're gonna hit apply for a developer account alright it's gonna ask you to log in to a Twitter let me do an old Twitter account that I have and I'm gonna take you through this process okay so what I'm gonna do is go to hobbyists here that's fine I'm gonna get making a bot and we're gonna hit next alright and it's gonna ask you which country you live in so let's do what are we doing today we go in the Japan everybody there we go alright and let's see do we need something else I don't even know anymore next I cannot hit next what would you like us to call you edy all right we can go next now here in your words you need to actually complete out a little description on why do you want to use this API so what you can do what I did was I just said hello I want to access the Twitter data and use the API for educational purposes bla bla bla bla bla okay just make sure you get two hundred keywords in here and you are good so let me just copy paste this like two more times so that's fine okay so next up down here are you planning to analyze the Twitter data evil eyes so I'm gonna hit no I don't care about that now what I do care about is I'm gonna use tweet retweets like follow so this is fine so again you can use the kind kind of the same thing here then I'm using it for educational purposes or to learn or to blah blah blah okay so there you go okay next up aggregate data no I don't care about taking the data and using it on my website and no this either alright so once you're done you're gonna hit next alright and then you're gonna hit looks good alright and then you're gonna agree to all of this you're gonna hit next and then you're gonna have to confirm your account which is awesome and finally you're gonna arrive here after you confirm so now what you can do is actually go to get started I believe there we go and you can hit create app so I already made one but what you can do is create an new app all you have to do is give it a name give it again the description here educational purposes and you need a website here and now you can use your metal file website or just something random and that's gonna be fine because we're actually not going to use a website or anything we're all gonna all we're gonna do is just use scripting in Python okay so just add something in here to tell us a bit about the app it's very bad and not good at all delete this why you do this okay create and you are done so after you create that it's gonna appear right here and your get started create an app here we go this is the one you're gonna expand this with details right here this is my dimitri marco website that the guy on Fiverr made me it's still up surprisingly and what we want to do is go to keys and tokens and this is what we're gonna actually be using okay so next up what we want to do is just open up a new project so I'm gonna hit file open folder I'm gonna call this Twitter API tutorial and like this I'm gonna hit select okay I'm gonna create a file this is gonna be called Twitter dot hi like that okay and now obviously you want to have Python installed so if you don't have it make sure you install it and what we want to do is actually install one package and that package is called what is it called tweet be there we go it's tweet me that's what we're gonna be using so what we want to do is say pip install twe P like this okay if you have any issues or errors you can see pick three install tweet beam and that should be fine when those while you do this be quiet okay so what we learn to do here is import that package so we're gonna say import twee P like this okay and another one we're gonna be using it's called time and I'm gonna kind of tell you why we do this okay so the first step is you actually add those keys and to basically be able to access your Twitter data so what we're gonna be doing is creating a variable called odd like this off how do you have the American say it off all right and then Hourigan it is call this twee ptang this package and then we're gonna add a dot and we're gonna say go off Handler like that alright it's automatically gonna complete for us and here we're gonna have basically two parameters I'm gonna add two strings Oh too much JavaScript and and this is what we're gonna add let me show you what we're gonna add we're gonna add these things the consumer API keys I have it open here as well an inception so what we're gonna do is copy these I'm gonna copy the first one the API key and we are gonna add it back here all right so the first one here okay and the second one is gonna be the password so let me copy that as well API secret key and paste it and here okay so after we do this as you can see we also have yeah okay so now that we have this we are also going to add the second one which is access token and access token secret okay and for this one we need to call something else we need to say off dot set access token okay same thing just two parameters tool only parameters lost in the wind and we're just gonna copy paste these over so copy paste again I highly recommend you to use your own because I'm gonna regenerate that after this video so you can have access my Twitter data obviously so let's copy the other one too now here's a little tricky tricky if you're gonna get an error here it's because you might leave a space here as you can see here make sure you don't leave spaces if you leave space it's gonna ever out it's not gonna work so make sure you go to these parameters just go through all of these up okay so no space no space here because I accidentally did a space and it wasn't working and I didn't know why and life turn ezreal okay very fun we're having fun here everybody okay so now that we have all these things we are ready to party so what we need to do is create another variable called API okay and here we can say twe P dot API like this and here we're just gonna pass in that off now in previous video that we did like six months ago the issue that we had was that we're making too many requests and we don't want to be banned from Twitter right so we use the timeout sleep I still added it in here but we're gonna use it kind of differently but if you're working directly with the API the advantage is that Twitter already knows when you're gonna hit that limit of hey I like too many tweets at the same time or I pull too many users or things like that so the advantage here is that we pass in this off alright so to authenticate ourselves that's the first parameter but we can actually pass in another one called wait on raped limit and what this allows us to do again is it's gonna pause or it's gonna take a break as soon as you reach a limit of likes or a limit of getting followers and we're gonna see this in action so what we're gonna do is set this one to true and make sure it's a big P because we are not in the JavaScript world anymore okay and we can also get kind of a console log or a print in notification so we can say wait on rate limit and notify and this is true as well okay so that's it that's all we need to do we have all the things that we need we can control everything that we want this is why I like Python the libraries are just so awesome and it's so simply simple to use okay so if I want to get myself what I can just do this create a user and set is equal to AP I dot me all right so API right there and just get myself perfect so let's print out my self print user let's see does that work whoa what did I do Twitter dot pi all right Python Twitter dot pi that's what we want to do there we are oh and there we go take a look we get back all the information about ourselves ooh so much so much stuff here so we can do something like my recent tweets I can get the followers I can get maybe I didn't this is so much retweet counts as you can see so many things look screen name I can get that if I want user dot screen name I believe it was like this run this there we go develop a accom by the new course I'm gonna update it this month by the way so there we go we can also mess around and do other things so let me show you a very cool example here let's try to get all my followers so walking what we can do is we can say for follower and and I'm gonna actually introduce you to a new concept here or we can use a twee P dot cursor like this now what is this cursor well on Twitter what you can have is is like a lot of pagination right you might have a lot of pages of users or followers and this kind of takes care of that it's gonna gather all that list of users or items or pages it's gonna go through them all so you can basically iterate through it okay so if you ever want to get all the tweets you ever want to get all the messages or all your followers you're gonna be using this cursor it's basically kind of iterate through all of those so we're gonna be using Twitpic cursor like this and in here what we're gonna pass in is the API dot followers like this okay so this is gonna pretty much iterate through everything and then what we can do is add a dot items here so we get back all the items okay and then what we can do is say print let's do follower dot name alright we get back all the names of our followers let's take a look and you're gonna actually see this in action that we're gonna probably hit that rate limit and it's gonna automatically pause for us so let's run this hopefully it works and there we go look at those are all the people that follow me and sooner or later you're gonna see that that API is gonna hit the limit and take a look weight limit reached and it's gonna go to sleep for 890 seconds and once that is done it's gonna keep pulling in the additional followers okay but for now I'm just gonna close this up and close this up okay so this is kind of how we can get all the users what we can also do is maybe I can follow someone if they follow me so I can say if follower dot name oops no parentheses if follower dot name is like equals I don't know Traverse e-media or something then I can do a follower dot follow back I'll follow like this okay so I can I can mess around a lot of this kind of however you want but what we want to do is gather basically search terms and based on that search terms we can either retweet or we can like them if we want so let's take a look at how we can do that what I'm gonna do is add a search term so here I'm gonna say something like Python or JavaScript like this okay and how many tweets I want to pull back so I can say number of tweets like this and I can set this equal to something like 500 okay and I can basically loop over it again for tweet and tweet pea dot cursor like this and I can do an API dot search this time okay so I can add a search and the second parameter here is gonna be our actual search so JavaScript or whatever you want to add okay and here I can just add dot items again and basically specify how many items I want to pull out of this and we created a variable so we can just pass it in here perfect okay up here we just did items actually deleted it so basically it's gonna go through all of them but here we're kind of limiting it to 500 okay so here what we can do is add a like that the double dot as I like to say don't judge me and here what we can do is create a try and we can try to basically favour it all the tweets so let's do print tweet light like that and here what I can say is tweet god favorite like that alright make sure you don't do you favorite like that it's favorite like that I don't know why please let me know it's been bothering me for three days okay but even though we have this rate limiter I still wanna I don't know I like to add a bit of time to it so it's not spamming like it's gonna 30 like 32 tweets at the same time and then it's gonna time out so I like to kind of add a stop here so I can say time don't sleep and do it like every 10 seconds okay normally probably I would do a minute because what's the rate I think you can like a thousand tweets per day so if you want to do that calculation feel free okay so after we do that we might get an error so I'm gonna add and except here if I can spell this correctly hey exc yep like that I can call twe P dot Tweep error all right it's gonna catch all the errors I'm gonna rename it as e and I'm just gonna print out e dot reason okay so in case we have any errors you know what it is and as soon as it reaches completion that sounds wrong so I'm gonna say stop iteration and I'm gonna just break and we're gonna stop the code okay and that's kind of the whole thing so let's give it a shot I'm gonna run this and let's see if it appears that we like tweets okay so one let's do two all right we're gonna have to wait here come on one more for the Teddy okay so we have two I'm gonna just stop this for now we can go on my Twitter and we're gonna see if we go to profile like and there we go we liked two tweets actually I liked more here but whatever so as you can see it has the keyword Python in it and all of them should have Python somewhere here we go Python and this one should have Python somewhere too okay so it works actually we the JavaScript on the last year okay so then we go and you know there are people that do the retweet thing so if you talk about JavaScript or something they automatically retweet that wait that's the same thing you can do here you can add a keyword like JavaScript so if you ever mentioned JavaScript rather than doing tweet dot favorites you can do retweet retweet I believe this is the one if you're unsure you can always go on Tweety here alright so twee pee let's search retweet you can't believe this is the way it was alright just go in here it's very sweet click click click click click yep there we go retweet yeah you just say retweet okay so we can do that without retweet let it sit a bit and you are done alright so this was quite easy there's still a ton you can deal with this twee pea library and the Twitter API you can check it out here I I just looked over here and saw a lot of cool things that you can do from liking tweeting making maybe scheduling your tweets so you can do maybe you can schedule like five tweets per day so you don't have to really do anything it's gonna take care of it for you there's a lot of cool things if you want to see one of those where you can schedule your tweets maybe let me know and we can create that one thank you very much for watching hope you enjoyed this episode map playing around with Python is incredibly fun it's actually very relaxing for me awesome I also want to make sure that you go over to Twitter developers the API section there and make sure you're not breaking any rules because they do have some rules that you can do with the API so just make sure to kind of read over it make sure you're not doing anything crazy to get yourself banned or something okay so that's it I told you before that in the summer I'm gonna also create a tech channel so the science does this is gonna be kind of software based and tutorials and the other one's going to be tech based physical laptops phones cameras how to film how to do this how can you get a point but I feel extremely unclean so let me know what would fit debit with a peck channel the best name will get a pen will get the common pen that's what I wanted to say okay that's gonna be it thank you so much for watching and until next time we're not gonna be playing with a snake anymore that sounds weird wait
Info
Channel: Dev Ed
Views: 73,570
Rating: 4.9324579 out of 5
Keywords: twitter, twitter bot, python, python bot, python tutorial, python twitter, python twitter bot, python app, python for beginners, learn python, python project, simple python project, python twitter app, dev ed, developedbyed
Id: ppPKeIDjcCs
Channel Id: undefined
Length: 21min 5sec (1265 seconds)
Published: Sat Jan 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.