Using the Digg API to Prepare your Site for the Digg Effect - Part 2

June 7, 2007 | Comments (3) | Filed under: Tools

Last week I started to discuss some of the recent efforts I’ve been making to try and use the new Digg API to prepare a site to survey the Digg Effect. In part 1, I described the idea behind the project and my plans to use the API to detect a rising Digg article, and take the appropriate steps to prepare. Now that I’ve had the chance to experiment a bit, I’m actually quite impressed with the API and some of the amazing things you can do with it.

Here is a really basic idea of how it works (if you want a really comprehensive idea, there is a large set of documentation at the official site):

  1. You query the Digg server by building a customized query string and passing in any one of a wide range of identifiers to let them know which article is yours. Some examples are your permalink, the title of the article, your URL, etc. Basically, whatever you have at hand that can uniquely identify your content will work. In our case, I’m using the URL/Permalink to the video being digg’d.
  2. Depending on a number of variables you can customize, Digg returns a response for your query in the form of XML, JSON, JavaScript, or PHP. The choice is yours, so whichever is most compatible with your site is probably the best to go with. In this document, I’ll be using the PHP response type.
  3. The response from Digg really depends on what you queried with and what you’re looking for. You can search very broad through topics and users or very narrow at individual stories or even just the comments and Diggs of a story. You can see a full list of all the supported Digg endpoints on their API site.

So, to continue with the idea, my plan was to automatically query Digg using the API and determine how many Diggs an article has currently received. If the number is low or even zero, leave the site operation alone because there is no fear of reaching the main page of Digg.

In the case that our Digg count does start to climb, and eventually reaches our specified cutoff, we automatically invoke steps to prepare the site for the incoming Digg Effect. The best way to accomplish this is to remove all dynamic aspects of the site including database lookups and JavaScript tools (such as MyBlogLog or Text-Link-Ads) to completely minimize the server load for your page. We also export the entire page into a plain static HTML file, and through the duration of the Digg Effect, this file is used for incoming requests in place of the original dynamic page.

For example:

function queryDigg( $link ) {

     ini_set( ‘user_agent’, ‘Digg-a-ling/0.0.1′ );
     $diggURI = “http://services.digg.com/stories”
     $appKey = $SITE_URL;
     $sort = “submit_date-desc”
     $count = “1″
     $type = “php”

     $queryURI = $diggURI . “?link=” .
            urlencode( $link ) . “&appkey=” . urlencode( $appKey ) .
            “&type=” . $type . “&sort=” . $sort . “&count=” . $count;

    $response = file_get_contents( $queryURI );

    return $response;

}

The above query looks up specific stories containing the permalink of our page. Once you get your response, you can then go ahead and implement any code you need to prepare your site for the Digg Effect. You can add something like this to the top of your diggable pages:

if( $diggInfo[’DIGGS’] > $diggThreshold  ) {
        if( file_exists( $CACHE_FILE ) ) {
             print( file_get_contents( $CACHE_FILE ) );
             return;
       }
}

This is assuming you’ve taken the time to create $CACHE_FILE if your page started to build in Diggs.

if( $diggInfo[’DIGGS’] == $diggThreshold ) {

        $CACHE_FILE = $CACHE_PATH . “/” . $PAGE_TITLE . “.html”;   
        $fileHandler = fopen( $CACHE_FILE, ‘w’ );
        fwrite( $fileHandler, $THIS_PAGE->getDocument( ) );
        fclose( $fileHandler );

}

Where $THIS_PAGE->getDocument( ) is whatever your template system uses to generate the markup that is printed to the screen. You can also use this technique to decide when to show your Digg button on your site. If, for instance, you don’t want to show a digg rating of zero, you can simply setup a check and only display when it grows to a certain value. You can get a full list of the available digg buttons for your site by visiting the Digg buttons webpage.

function show_digg( $diggs ) {

      $diggURL = “http://digg.com/tools/diggthis.js”;

      if( $diggs > 0 ) {
          return ‘<script src=”‘ . $diggURL . ‘” type=”text/javascript”></script>’;
      }

      return “”;

}

As I hope you can see from this very small and minor example, the Digg API opens many avenues for much more robust integration between your site and Digg. The skies the limit on what you can accomplish, which is more than you can say for many of the other major social news sites. Say what you want about Digg, but this API is a great step forward in terms of making the users drive the ingenuity of the Digg engine, and I can’t wait to see what new tools come out in the months and years ahead.

3 people have left comments

Maybe you have a poor server, otherwise you must be getting very nuch traffic from digg.
Can you please elaborate a bit on how you get such great digg traffic? Or maybe you can just point me to the article on the site (I didn’t find one yet).
I think you can do much better using the Digg API than to protect your server.
Benny.

Benny Shoham wrote on June 7, 2007 - 2:13 pm | Visit Link

You can definitely do much more than just protecting your web server, but that was an issue I was having with the site I was working on, so I used the API as a solution to that problem. The API is extremely powerful, and there is a lot of stuff you can do with it once you get into it.

Here is a list of techniques for reaching the Digg Font Page I posted last week:
http://www.fuzzyfuture.com/social-networking/10-techniques-for-reaching-the-digg-front-page/

These are the tutorials I have followed…

stark wrote on June 7, 2007 - 3:38 pm | Visit Link

That’s a pretty sweet way to use the API. Keep up the good work!

~Jaxia

Nursing Student wrote on June 12, 2007 - 6:08 pm | Visit Link

feel free to leave a comment

Comment Guidelines: Basic XHTML is allowed (a href, strong, em, code). All line breaks and paragraphs are automatically generated. Off-topic or inappropriate comments will be edited or deleted. Email addresses will never be published. Keep it PG-13 people!

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

All fields marked with " * " are required.