The same image of a fried egg, everyday

Do you want fries with that?

Published on December 22, 2015

So a few days ago I saw this page on facebook. The page uploads the same image of a toast, every day. The concept is rather.. strange, but at the time of writing this, nearly 12000 profiles had liked the page. Reading a bit about how the page functions revealed that some guy actually uploads the image manually every day, at least the days where he remember to do so. 

The manual part got me thinking. Surely it must be possible to automate the upload to facebook such that all the likers truly receive the same image every day and not just on the days where the guy remember to do it?

Spoiler alert: it is possible.

What we need

The task at hand is relatively easy if all the following steps are done correctly. First of we need a facebook Access Token which we will be using when uploading. This is done to avoid hardcoding our own private login credentials into a Python script.
Secondly, we will be using the Python for facebook SDK to communicate with facebook.

Getting the facebook Access Token

Figuring out exactly how to get facebook Access Tokens was a horrible experience. Broken or just really bad tutorials are available online but, as stated, they all basically sucked. Until I found this. Note however, that the publish_stream Permission is no longer available so just in case that site is removed in the future I will write a short HOW TO here:

  • Create a page by going here. If you already have a page, this step is optional.
  • Create an app.
  • Go here and select the application you just created. After hitting Get Access Token you have to select Extended Permissions  where we need the following three 
    manage_pages, publish_actions, publish_pages
     (I am not 100% sure we really need the last one. First try without). Then  Click Get Access Token  and boom, we have a temporary (valid for 60 days) token.
  • Authorize the app such that a small green dot appear beside the app name. This means we can now use it for API usage.
  • Extend the temporary token by issuing
    https://graph.facebook.com/me/accounts?access_token=TEMPORARY_ACCCESS_TOKEN
    which will return a page with a access_token line. This is our Permanent Access Token. Also note the id line which will be our page_id in the following.

Using the facebook SDK

When the hard part of getting the Access Token is over, using the facebook SDK is quite easy. Start by grabbing a copy of the facebook folder from Python for facebook. Then move it into your work folder where we will also be creating our main.py file looking like this

# Enter your page id and access token
cfg = {
    "page_id"       : "your_page_id",
        "access_token"  : "your_access_token"
        }
api = get_api(cfg)

'''
Post an image
'''
status = api.put_photo(image=open("path_to/my_image.file_type", 'rb'), album_path=cfg['page_id'] + "/photos")

Initially I received this error (#324) Requires upload file which I resolved after asking for help at Stackoverflow. Go to that thread if you receive errors. In my case, the error was caused by my image being corrupted.

Finally, I added this as a cronjob so that my script will be run every morning at 8 am. Then all the likers will have a beautiful picture of an egg on a mirror (Danish play on words: fried egg = spejlæg = mirror egg)

spejlaeg

A beautiful picture of an egg on a mirror (Danish play on words: fried egg = spejlæg = mirror egg).

Simply issue

crontab -e

without sudo infront. Then add the following line (change it to where you have placed your folder).

0 8 * * * python /home/pi/fb_upload/main.py &

That's all.

If you suddenly crave a daily "fried egg" feel free to like the page.

Full code can be found at Github.