How to build a facebook post scheduler using python
Sometimes you think of something really funny and feel the need to post it on facebook or twitter right away. Depending on the time of day that you post it though, the post might not get as many likes or visibility. So, for the optimizers, I thought it would be cool to build a scheduler for facebook posts. Using this tool, you can be sure to get that extra like by posting at a common time like 8pm instead of your caffeine fueled 3:30am study sessions.
For those that just want to get to the code, the full github gist is shown at the end.
Using python’s selenium module as a web bot
Python is a fantastic language. It’s simple syntax, breath of application with module libraries, and extensive community means it is the language of choice for many a hacker — myself included.
Selenium is a python package built to control web browsers for automated testing. While testing is its initial use case, many hackers have found use for it as a web bot, allowing them to crawl the internet, or just interact with it as a human would. The basic interaction involves finding elements in the raw HTML, such as a <div> block. It would be a good idea to have Developer Tools installed in your browser, in order to inspect elements and see their HTML code. Once the element is selected, you can choose to click with a simple .click() or, for something such as a text box, you can fill in text with a .send_keys() function. This functionality works very well on simple websites, however large websites such as facebook have gotten wise and made it harder to create effective bots — they do this by employing tactics such as pseudo-random popups, un-interactable elements, and the like.
Clicking on a button is as simple as driver.find_element_by_xpath(“//a[@action=’button’]”).click()
More info on how to use the selenium package can be found here.
Using python’s dotenv module for secure storage of passwords
Oftentimes, when you want to build a web bot, you will have to authenticate yourself through a login. This is not a particularly difficult thing to do (unless there is a captcha involved, in which it is very difficult — there are ways around this too though), and the easiest method would be to hard-code your email and password credentials in your script. For those that haven’t taken Cybersecurity 101, this is a terrible idea. Ideally, your email and password would be in a different file, so that someone observing your script will not see your login info. For even better security, you would want to encrypt the credentials and decrypt them right before entering them.
dotenv is a neat module that takes care of separating your bot script and login info. Simply create a file called .env and add your info in the format of LOGIN=’logininfo’. In your bot script, you will import dotenv, then call load_dotenv() to load the variables in the .env file into your OS’s environment variables. From there, simply call os.getenv(“LOGIN”) and you can now use ‘LOGIN’ as a variable.
More on the dotenv module here.
Using python’s schedule module to schedule tasks at specific times of day
There are several ways to schedule scripts to run on a machine. On Linux machines, there is the relatively simple crontab. On newer Macs, crontab was deprecated in favor of Apple’s launchd service, which is more complicated but slightly more robust. On Windows, there are several GUI-based applications such as Task Scheduler. All of these utilities do the same thing — that is, schedule a script to run from outside the script itself. Often, it is difficult to modify these schedulers on the fly, which has begged the need for a programmatic way to schedule tasks.
Enter python’s scheduler module. scheduler’s operation is very simple. Schedule a function to run by a line of code such as schedule.every().wednesday.at(“20:36”).do(function). When the time has come to run the function, the status of the function will turn to ‘pending’. Run schedule.run_pending() to run the function. For best practices, schedule.run_pending() would be placed in a while loop in order to check if the funciton’s status has turned to ‘pending’.
More on python’s schedule module here.
Tying it all together
Now that we have the building blocks of our bot, we’ll tie them together to make our script. First we’ll have to open a browser using selenium, then go to facebook.com, and login. Using the DOM selector in our browser’s Developer Tools, we will notice that we will first have to click on the ‘What’s on your mind?’ box, then input the text that we wish to have posted, then find the ‘Share’ button and tell selenium to click it.
Full code can be found below.