Daily Dose of Quotes: Twitter Bot
Technologies
Python Libraries
Description
Images
Tweet Limit Calculation
Tracked Users
• Python • MongoDB • Heroku • Twitter API v2 • Git
• tweepy • MongoDB Atlas API • PYgithub • requests • loadenv • multithreading
Daily dose of Quotes is a Twitter bot coded in Python which uses the Twitter API endpoint to tweet a motivational quote every 3 hours while tracking a list of users to reply to their latest tweet with a randomly generated quote.
Technology: Python, MongoDB, Heroku, Git.
Detailed Description
- The Twitter bot is powered by a 50 thousand tweet database hosted on MongoDB Atlas.
- It sends a random number as an Index and extracts the corresponding quote using MongoDB Atlas API. Raw data is formatted to show the author's name, tags, and other information while not exceeding the tweet character limit.
- The Tweet data is then posted to Twitter using the tweepy library and Twitter's API v2 endpoint.
- This process running on the first thread sleeps for the next 3 hours.
- Simultaneously, the second thread tracks a list of users hosted on the same database and updates the ID of the latest tweet of every user.
- As soon as the user tweets, within 5 minutes, the bot replies to them with a new random quote and updates the ID of the tweet.
My Learnings
- The project underwent many iterations and setbacks. I started with the 1st version and incrementally upgraded the project to the final version 4.
- I began with the tweepy library in python to GET and POST data via the Twitter V2 endpoint, which used quotes from a third-party quote API. Eventually, the quote API proved unreliable.
- I designed version 2 with a 50-thousand quote database hosted locally, but it had memory problems and frequent crashes due to its sheer size.
- For version 3, I debugged the solution to host the database on MongoDB Atlas and fetched/updated data through its web API.
- I Upgraded the bot to version 4 by implementing user tracking and tweet replying. I had to engineer various logical syntaxes like tracking the optimal number of users without exceeding Twitter's limit, keeping track of tweet IDs to only reply to non-replied or latest tweets, and formatting raw data into a readable Tweet.
- Also assembled the above processes using python multithreading on Heroku worker dyno and streamlined the project by integrating it with Heroku CLI.
- Moreover, I studied eclectic Git commands and practices and maintained failsafe versions of production code in branches.
max tweets allowed:
bot scans all day with
let the time interval between each scan be:
let the no. of users we want to track be:
$$\therefore y = {x * MaxTweets \over DaysHoursMinutes*TweetsPerScan}$$
$$y = {500000 * x \over 312460*5}$$
Bot tracks the most famous personalities on twitter.
- Adam Grant
- Elon Musk
- Narendra Modi
- NASA
- Amitabh
- spaceX
- Ronaldo
- isro
- Joe biden
- Barack Obama