Picture Explorer, Sort Pictures by Facebook Likes


I have created an online page that allows people to upload any single photo to server. The URL to list all the photos (visible) is https://rot47.net/picture-gallery

To add a photo (upload), you can visit the URL at https://rot47.net/picture-gallery/upload-photo.php You can specify whether the photo to be included in the Photo Explorer (public or private)

On each Photo page, there is a Facebook-like button. I have posted earlier this morning on stackoverflow that [link] how to create a page that lists the popular photos by Facebook-like counters.

The quick answer is to get the return string from the Facebook Graphic URL, for example, the URL for https://graph.facebook.com/?ids=http://www.example.com/

returns:

{
   "http://www.example.com/": {
      "id": "http://www.example.com/",
      "shares": 143917,
      "comments": 332
   }
}

The string is in JSON format where you can parse easily using PHP function json_decode.

The second parameter when set to True will convert the result into associated array.

However, this just is the basic step to retrieve the ‘likes’ counter for a single page. You certainly just cannot get the counters for a million pages at realtime, which will ‘crash’ the server (probably not, because there is a rate-limit imposed on the API calling policy) and will be time-consuming, which is infeasible.

The other work-around is to collect the counters/statistics offline, on a regular basis. To do this, we create a table that has two columns for storing the counters. The two columns are picture id (which tells which picture) and the second is the counter for Facebook likes (the number of likes at the time of collecting). Of course, the number of ‘likes’ for each page is not updated real time (it can only reflect the popularity at certain times), but this is enough, you can put the process of updating the numbers on background (for example, crontab) that runs at interval, such as once per hour, which will be good enough.

The PHP update script is given like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  // select randomly 100 photos
  $query = "select `id` from `pic` order by rand() limit 0, 100";
  $result = mysql_query($query, $link) or die(mysql_error());
  $num = mysql_numrows($result);
  $i = 0;
  while ($i < $num)   
  {     
    $id = mysql_result($result, $i, "id");     
    $url = "https://rot47.net/_p/add.php?s=".toBase($id);     
    $fburl = "https://graph.facebook.com/?ids=$url";     
    $fb = file_get_contents($fburl);     
    $obj = json_decode($fb, true);     
    $shares = (int)$obj[$url]['shares'];     
    $q = "select count(*) from `facebook` where `pid` = '$id'";     
    $r = mysql_query($q, $link) or die(mysql_error());     
    $e = (int)mysql_result($r, 0, 0);     
    if ($e > 0) // if this has been recorded before
    {
      // update the counter
      $q = "update `facebook` set `likes` = '$shares' where `pid` = '$id'";
      mysql_query($q, $link) or die(mysql_error());
    }
    else
    {
      // put a new record
      $q = "insert into `facebook`(`pid`, `likes`) values('$id', '$shares')";
      mysql_query($q, $link) or die(mysql_error());      
    }
    // sleep 0.1 second so it won't cause trouble such as too frequent, overload the server
    usleep(100);
    $i ++;
  }
  // select randomly 100 photos
  $query = "select `id` from `pic` order by rand() limit 0, 100";
  $result = mysql_query($query, $link) or die(mysql_error());
  $num = mysql_numrows($result);
  $i = 0;
  while ($i < $num)   
  {     
    $id = mysql_result($result, $i, "id");     
    $url = "https://rot47.net/_p/add.php?s=".toBase($id);     
    $fburl = "https://graph.facebook.com/?ids=$url";     
    $fb = file_get_contents($fburl);     
    $obj = json_decode($fb, true);     
    $shares = (int)$obj[$url]['shares'];     
    $q = "select count(*) from `facebook` where `pid` = '$id'";     
    $r = mysql_query($q, $link) or die(mysql_error());     
    $e = (int)mysql_result($r, 0, 0);     
    if ($e > 0) // if this has been recorded before
    {
      // update the counter
      $q = "update `facebook` set `likes` = '$shares' where `pid` = '$id'";
      mysql_query($q, $link) or die(mysql_error());
    }
    else
    {
      // put a new record
      $q = "insert into `facebook`(`pid`, `likes`) values('$id', '$shares')";
      mysql_query($q, $link) or die(mysql_error());      
    }
    // sleep 0.1 second so it won't cause trouble such as too frequent, overload the server
    usleep(100);
    $i ++;
  }

Once we have the table, we can sort the photos by connecting two tables in the SQL query:

SELECT * FROM `pic`,`facebook` WHERE (`pic`.`id`=`facebook`.`pid`) 
ORDER BY `facebook`.`likes` DESC LIMIT 0, 30

References:

[1] http://en.wikipedia.org/wiki/JSON

[2] http://www.php.net/manual/en/function.json-decode.php

[3] stackoverflow

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
793 words
Last Post: CRC32 Calculator (Javascript)
Next Post: Frequently Asked Core Java Interview Questions and Answers

The Permanent URL is: Picture Explorer, Sort Pictures by Facebook Likes

Leave a Reply