Automate Everything w/ Bash, Linux & Command Line
  1. Social Share Reporting - Facebook Likes

    This is the second post in a series titled “Social Share Reporting”. The first post demonstrated how to get the Google +1 count for any URL. If you’re reading this then I’ll just assume you understand why you’d want to pull that data for a page, perhaps for a domain you don’t own.

    Required Tools

    1. cURL
    2. sed
    3. cut
    4. grep

    cURL is the only tool in the list which shouldn’t already be installed by default. The others are installed on most Linux variations and OSX.

    My methodology for figuring out where to make the calls is done basically by using tools native to Firefox or Google Chrome to see where the GET requests are sent. If you’ve read my previous post you’ll understand what’s happening with this code.

    This code mimics the called made to get the Facebook Like count in a “Like button” for YouTube.

    user@linux:~$ curl -s --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20100101 Firefox/10.0" "http://www.facebook.com/plugins/like.php?href=http://www.youtube.com"
    

    With the Facebook Like button, it’s a good idea to feed in a user-agent. I’ve seen inconsistent results when the user-agent is left blank.

    If you’ve executed the command above then you’ve seen the CSS/HTML/JavaScript vomit that is your terminal. Now it’s time to filter everything but the count. The following series of strung together tools, pipes and regular expressions will leave you with the like count.

    sed 's/<span/\n<span/g' | grep 'class="connect_widget_not_connected_text">' | cut -d" " -f2 | cut -d">" -f2 | sed 's/<.*//g'
    

    Now you just have to string together the first command and the filtering string to complete your one-liner.

    user@linux:~$ curl -s --user-agent "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20100101 Firefox/10.0" "http://www.facebook.com/plugins/like.php?href=http://www.youtube.com" | sed 's/<span/\n<span/g' | grep 'class="connect_widget_not_connected_text">' | cut -d" " -f2 | cut -d">" -f2 | sed 's/<.*//g'
    

    This is what I get:

    user@linux:~$ 9473604
    

    Apparently a lot of people like YouTube. Anyway, you can plug in any URL you’d like and get the count back. Be aware that if no one has liked the page yet, you won’t get back a ‘0’ value. It’ll just be blank. Also, I’m sure you don’t want to have to edit the call every time you want to check the count for a new page, right?

    Here’s the solution. Past the following code into a text file, give it a name ending in ‘.sh’.

    #!/bin/bash
    
    # this script should output the number of times a page has been liked on Facebook.
    
    url="$1"
    agent='Mozilla/5.0 (Windows NT 6.1; WOW64; rv:10.0) Gecko/20100101 Firefox/10.0'
    
    count=$(curl -s --user-agent "$agent" "http://www.facebook.com/plugins/like.php?href=$url" | sed 's/<span/\n<span/g' | grep 'class="connect_widget_not_connected_text">' | cut -d" " -f2 | cut -d">" -f2 | sed 's/<.*//g')
    
    if [[ "$count" == "" ]]
    then
        count=0
    fi
    
    echo "$count"
    

    Back at your terminal, execute it and give the URL as an argument.

    user@linux:~$ bash fb_like_count.sh "http://www.youtube.com"
    

    In this full version of the script I added in a check to see if the $count variable returns empty, which indicates 0 likes. If it’s empty, I assign a value of 0 to $count so it will always return a like count.

    Yet to come are posts on Tweets and maybe even “pins” on Pinterest!

    Happy automating.

     
    1. automateeverything posted this