Logo

Twitter Bot

Automate uploading more than 4.5K images to twitter.

Python
Twitter Bot

This is where my journey with Python started. First, you will need to know one thing about me. I wanted to learn this language for a long time but was waiting for the right project to make me start it. Nothing would compare to my passion and devotion to memes (that made me stay up til 4am when I was 9 to browse memes on reddit). Since this time, I started having this folder on my first phone, I would download like 5 images each day of the ones that made me laugh or I would save them for later use when I see my friends. Fast forward until reaching the level of being a programmer, especially at the age of 21, I decided to start this project.

Having more than 4.5K images of memes on my phone and being transferred between 3 phones was something I couldn't keep any longer. That is when I decided to give the world some of my knowledge, and share my good taste in memes to enrich the people around me. I found this cool course by CS Dojo, I remember staying after my exams to watch this whole course, but I have to be honest about it, it wasn't that hard. Just getting to know the basics of the language and then I was good to go. Now is the part of getting to write the code itself.

First, I had to lay down the steps starting from the phone until they were uploaded to the platform:

  1. Find a way to move all these images from the phone (easiest)
  2. Make the bot (no jokes, Sherlock)
  3. Find VPS to host the bot (so it can work 24/7), preferably for free (I'm still a student, man).

#Visiting New Lands

As someone who was using C++ at the beginning and then moved to C#, I could stay for like 20 mins with an error message for just a meaningless semicolon I forgot at the end of the line, so finding out that I didn't have to write it was a big deal for me. Also, not having to specify the data type was something good, not that much as it was confusing sometimes (good to know that the language got your back). Now getting to work

#What Does What

Getting into the code part isn't just about opening VS code and using the Dracula theme. I believe that first you need to plan how your code will work (having a flowchart will help) even though you will change it later as you get into the process more, so I laid down what (maybe) exactly the bot will do

  • Search for memes in the folder
  • Sort them according to their modification date.
  • Upload the first one.
  • Move it to another folder (so it doesn't upload again).
  • Send me a message when the meme is uploaded.
  • Accept some commands only from my account (flexing ( ͡~ ͜ʖ ͡°) )

Searching for memes was the hard part to work with. As I went first, through a while loop until the end of the file, but it wasn't that efficient, I switched to for loop (and I like it more), but I needed to sort them with last modification date.

def release_the_meme():
  imagePath = '/Memes'
  images = os.listdir(imagePath) # get a list of all files in the directory
  images.sort(key=lambda x: os.path.getmtime(x)) # sort by data of modification</code>

A caption is needed when you upload to twitter, something that came to my mind was to make a text file with a caption for each image, then the bot would take the corresponding number of the image with the line number in the.txt file, but that would take a long time to do. Instead, I came up with the idea of just using the name of the image. Although I wanted something authentic, there is something in it. Take Im-sure-Mrs-Marvel-will-keep-the-fans-happy.jpg as an example. There are two things here in the name. First is the extension of the image that can be removed by using pureName [:-4] that will remove the last character in the string. The next step is to use replace() to remove the hyphen. Now you have a clean name without any extras

Uploading the image as the caption now is ready with api.update_with_media(filename, pureName[:-4])and the same time uploading it (it might sound easy, but I spent 2 weeks just trying to sort them (っ˘̩╭╮˘̩)っ ), to make sure the image won't be uploaded again , it must be moved to another folder and show in terminal that the image has been moved to the folder so it can move to the next one.

Keeping track of what was uploaded, especially during the testing process, was critical because the Twitter API has limitations in place to keep the platform safe and free of spam, as well as to keep an eye on the bot. I made it send me a message every time it uploads something, because different VPS had their own way of closing any process that run for a long time on their server (it isn't a virus, trust me).

  • Step one: Fetch the id of the bot account.
  • Step Two: Get the tweet number.
  • Step Three: Set the upload time to 12 hour format.
  • Step Four: Send me a DM to tell that the whole process is done.

2nd milestone, there are 2 hours difference because of where the server is located

#Hosting

Money talks and I can't hear anything because I don't have money. As this project is a hobbyist one, so there isn't any revenue from it, it would be unwise to host it at the beginning with a lot of money. I mean, the whole idea of the bot is fun, and as much as there isn't money coming back from it, I was looking for the cheapest solution out there, I remember in one of the GDG Delta Sessions about G-Cloud, that they give you around 300$ or 90 days, so this was the first option. After the 90 days were over I decided to close the project and wait for another chance.

Until one day, that was 2 weeks before the new year of 2022, I had my new year resolution, you know, walking up early at 6am and starting a morning routine that doesn't include social media for 40 min in bed. One of them was hosting the project again (also I started applying for internships and it was good to show that I have a project hosted online ) and decided to go with  VULTR  because it offers the cheapest VPS out there that costs $4.45 each month.

#Conclusion

This is one of the closest projects to me, not because I spent a lot of time working on it, but more due to it being the first real implementation with new technologies to me. Working with Python, the Twitter API, and VPS, I didn't have that person with me to guide every step I had. Maybe this was a good point to make me search for the right question, but it was totally worth it to see the bot now reaching more than 2000 tweets and going to this from just an idea.