Do People Ever Sleep? Have you ever wondered how businesses stay at the top of your Twitter feed? Yeah, me too. It turns out they don’t actually make those posts everyday. They use a company like Crowdfire to schedule their posts! Who cares how it got there as long as the content is relevant to you right? I mean….it’s not like my boss would be thrilled to see me Tweeting incessantly during the morning meeting.
How To Be A Lazy Tweeter
Forget paying Crowdfire, I can be lazy without losing money. It’s not really lazy if I’ve spent some of my vacation (while there’s a blizzard outside) hammering away at an app that automates my Twitter account right? Nope. Not lazy here.
First, let’s discuss the method. We already know about Tweepy from a previous blog post and we know that Tweepy’s api.update_status(‘someText’) really takes care of the tough part of posting. How should we approach the scheduling? Since Python has such a lovely community, I found an amazing package! pip install schedule.
It Can’t Be This Easy…
Or maybe it can be. Let’s define our features first before we get to the code.
Features I need:
- app that can ingest a list of URLs, Image URLs, Catchy Text and Twitter Handles
- able to schedule posts on days of the week, every hour, every minute (you get the idea)
- some way to space out our posts so people don’t think we’re a robot?
- maybe we can crawl our own website to update itself? Have you checked out this blogpost?
Make The Schedule Function
Let’s make the function called runAMLautoPoster() and the subsequent makeSchedule(job,interval). In runAMLautoPoster() you will notice that the “interval” variable is being sent a number and unit as a string (i.e. ’30min’, ‘5hr’). It’s important for your program to be half-smart about the time interval.
Thanks to Daniel Bader @dbader for making the Schedule module so Pythonic, you can now create a function like makeSchedule(job,interval) that totally makes sense.
Now Let’s Tweet Something
Tweepy makes it crazy simple to post using api.update_status(‘someText’) or if you want to add an image, api.update_with_media(fileName,status=message). I created two separate functions to handle it just because typing tweet(‘xyz’) takes less time. Lazy programming is focused programming.
tweet_image() does some fancy bits by fetching the URL for you, creating a local image file on your computer, uploading that to Twitter and finish by deleting the temporary file. That whole process is wrapped up in the function getImageAndSave(url).
Now that we have the Schedule function and the Tweet functions, let’s talk about the main function that does most of the work called tweetScheduled(). Remember that one of our features required the ability to ingest a URL and text from an input source? We’re going to create an input CSV file that contains a list of all the URLs to my blog post, the links to the related images, and also the text. Using that CSV, Python will read it in as a Pandas dataframe and loop through each line and post to Twitter for us!! Below is the last method to Tweet the scheduled posts.
Happy Lazy Tweeting
*Note: if you want to run your script as a daemon on a Linux server use the command:
…$ nohup nice python <yourScriptName> &