Creating a Twitterbot thanks to Casey Neistat
I have been a fan of Casey Neistat for a great long while dating back to his daily vlogs. I have been wanting to do a project for a long time that involved having his YouTube videos come up as tweets throughout the day, but without closed captioning on a great deal of the video's this became impossible. Or at least without a great deal of work. So I began to comprise.
Comprise
Well, what was one aspect of his video's that was always in the background? The godfather. On-screen in the background the godfather 1 and the godfather 2 would play. In a loop forever. And this was my way in. Both of these movie scripts would have availability online. Holding on to the main idea. Which is take bits and pieces of it, from it, and put it on Twitter.
Now, this is not a new idea. I follow a few Bot accounts from Miyamoto Musashi (check out my review of Musashi: Here) to another account called Medieval Death Bot, to modern writers like Matthew Kobach and David Perell, but this only validates the idea that I could possibly do this.
So where to start. The first place is to get the raw data that I am going to be needing. The data in this case is going to be the scripts from both movies. Being two of the most popular movies of the previous decades finding its script online was inherently easy. A simple google search of ‘Godfather screenplay’ gave me the URL that I needed.
Saving the files as .txt files to my local hard drive I can easily find them when coming back through them. Finding things locally is as simple as creating a variable and linking them. Now that I have the file it is going to be read and written with
Reading and writing
With the reader file object, it’s possible to look inside the file. With the information read I created another file with the same exact data. This way I can edit, and play with it, without editing the original file.
With this list, I can start to delete all the things that I don’t need. I won’t list it out all here, if you want to see it check out my GitHub post. Here
While still a bit far away from being able to actually tweet any of this. It was a good first step. The next problem to encounter was the "\t" which is going to show up a lot in a lot of this. This is the metadata that the computer recognizes as a tab or a new line or such things. I am going to assume the reader knows something about this, and won’t spend much time explaining it. Getting rid of these is an easy step to do in python. It is worth mentioning as it does create a hiccup in converting from computer speak to something that is more legible.
The process then gets repeated for the second godfather script creating a long comma-separated value (CSV) list of all the things said or done in the movies.
Now I will admit I did spend quite a long time trying to get this converted into an excel file that could be then used at a whim. But ultimately found that this route of thinking was not worth the trouble.
One thing I will mention about these lists is that they are all in order. If one person says "how are you?" they are not getting in response to a gory violent scene.
They are treated with a "good, how are you." Easy to understand now, but important later on. With the list’s all set up it is time now to connect virtually to the mega-highway that is Twitter.
Using Twitter as a Devloper
I relied pretty heavily on a few different guides to getting twitter connected. It's a pretty easy process to go through. The best guide that I found I will link to: here
The only thing I will mention about this article is that Python allows for the importing of different modules, so it piggybacks off the tweepy module.
The guide is perhaps the best guide on how to do a lot of this back-end work that I used. One that I went over and over to trying to get this to work. But once it did and the Authethican comes back as ‘Ok’ then it is time to start tweeting.
I also did tweet out hello world as the first real tweet. If you don't are you really programming?
Now one of the things I did not want to do was simply just tweet lines from this. Taking each list item could have been sent out, and that could have been the end of the day. But from the get-go, I really wanted to create dynamic tweets. Multi-line tweets that pop off the page like some of my favorite tweets do. Single sentences? Aren't going to do it.
Another problem that occurred was the splitting of the lines did create rather short sentences. Or values. To counteract this. I took to taking the line before and after it and using this to create a string object that appeared to be long enough.
As I mentioned earlier there are plenty of Twitter bots online, and wanting to fit in. I realized that I was just going line for line of both movies, and this was rather predictable, and a little boring. A line for line part of it didn't really fit into what everyone else was doing.
It was at this point that I started to add a random effect to the mixture. So creating a random point in the long list of items is as simple as using the random function.
So the challenge now became how to actually do it and get the lines to actually split at the right point. And create multi-line tweets that have the potential to look better than a single sentence from the movie. What follows is plenty of trial and error trying to get the right flow of the script to actually tweet out at the right moment and at the right intervals.
What I found to work best was putting "\n\n" at certain marks. Such as periods, exclamation marks, and semi-colons. These seemed to give me that dynamic tweet effect that I had been hoping for.
So the challenge now became how to actually do it and get the lines to actually split at the right point. And create multi-line tweets that have the potential to look better than a single sentence from the movie. What follows is plenty of trial and error trying to get the right flow of the script to actually tweet out at the right moment and at the right intervals.
What I found to work best was putting "\n\n" at certain marks. Such as periods, exclamation marks, and semi-colons. These seemed to give me that dynamic tweet effect that I had been hoping for.
Tweeting this out at regular intervals is now the only challenge left my computer now has a program that takes from a pre-made list, splits it apart, and re-creates it into a passable tweet. Now there are a lot of ways to have this tweet out regularly.
My pathway in doing this was creating a .bat file of the python file and having my computer run it at set times using the windows scheduler.
If you would like to check out the godfather bot I will link it. https://twitter.com/RingFingerInc
While this took me a bit of a month, as a side project. I had a lot of fun doing it, and overcoming tiny goals in coding is something that I do treasure a great deal. There are plenty of things that I left on the table with this one. Ways it could be better. Faster and easier ways to do it.
If there are those things that you do notice. Tweet at me! @eagerartistian if you enjoyed this or learned something, I am pretty happy as well.
Thanks,
Greg