Facebook page like count to Pushover

In order to track my sites Facebook like count I made a small Python script that would notify me whenever there’s a change.

2015-08-30 07.28.15I love notifications and statistics so of cause I had to make a little script that keeps me updated on how many likes IDmedia has. This does not mean I’m a Facebook addict, I just like statistics and hope to reach more people then I would without social media.

Just copy it, change the three variables in the beginning of the script and it should run without issues. I run mine in a cron job every 10 minutes or so. The Pushover token and user key may be found on your Pushover profile.




import sys
from lxml import etree
import urllib2, os, httplib, urllib

FACEBOOK_URLS = [["IDmedia", "https://www.facebook.com/idmedia.no", "XXXXXXXXXXXXXXXX"],
                ["AnimeNord", "https://www.facebook.com/animenord", "XXXXXXXXXXXXXXXX"]]

def main():
    # Change to our directory
    abspath = os.path.abspath(__file__)
    dname = os.path.dirname(abspath)

    for fburl in FACEBOOK_URLS:
        # Setup variables
        name = "".join([c for c in fburl[0] if c.isalpha() or c.isdigit() or c==' ']).rstrip()
        url = fburl[1]
        filename = 'like_count_%s.txt' % name.lower()

        # Verify that the likes.txt file exists
        if not os.path.isfile(filename):
            createfile(filename, url)

        # Collect likes count
        previous_likes_count = int(readfile(filename))
        new_likes_count = int(readlikescount(url))
        diff_likes_count = new_likes_count - previous_likes_count

        if diff_likes_count != 0:
            # The number of likes has changed
            # Send notification and update like_count.txt
            createnotification(name, fburl[2], new_likes_count, diff_likes_count)
            createfile(filename, url)

def readlikescount(url):
    headers = { 'User-Agent' : 'Mozilla/5.0' }
    req = urllib2.Request('https://api.facebook.com/method/fql.query?query=select%20%20like_count%20from%20link_stat%20where%20url=%22' + url + '%22', None, headers)

    facebook_likes = urllib2.urlopen(req).read()
    facebook = etree.fromstring(facebook_likes)
    like_count = facebook.find('.//{http://api.facebook.com/1.0/}like_count')
    return like_count.text

def createfile(filename, url):
    like_count = readlikescount(url)

    if like_count:
        with open(filename, "w") as text_file:

def readfile(filename):
    with open (filename, "r") as text_file:
        return text_file.read().replace('\n', '')

def createnotification(name, pushover_token, new_likes_count, diff_likes_count):
    conn = httplib.HTTPSConnection("api.pushover.net:443")
    conn.request("POST", "/1/messages.json",
    "token": pushover_token,
    "user": PUSHOVER_USER,
    "message": "Likes for %s: %s\nChange since last update: %s" % (name, new_likes_count, diff_likes_count),
    }), { "Content-type": "application/x-www-form-urlencoded" })

if __name__ == '__main__':

Edit 25.11.2015: The script now supports a list of Facebook URL’s. This way you don’t need to run multiple copies of the same script.


Share this:

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.