Hack 70 Simplify Amazon Associate Links in Your Blosxom Weblog


Create an easier way to link to Amazon products with the ASIN plug-in for the Blosxom weblog application.

Blosxom (http://www.blosxom.com) is a full-featured weblog authoring tool written in a single Perl script. Its simplicity belies its many capabilities, and one of these is a plug-in interface that allows other developers to add features easily. Blosxom is popular among newcomers and hackers alike, and it's not surprising that as of this writing there are well over 100 plug-ins available (http://www.blosxom.com/plugins).

Developer Nelson Minar has used Blosxom to create a fast way to link to Amazon products called ASIN. With the plug-in installed, you can use a special link syntax:

<a href="asin:insert ASIN">Link Text</a>

With this syntax, the ASIN plug-in creates a properly formatted link to Amazon with your associate tag. So, linking to Amazon Hacks while you're writing your post might look this:

<a href="asin:0596005423">Amazon Hacks</a>

But it ends up on the site like this:

<a href="http://www.amazon.com/exec/obidos/ASIN/0596005423/ref=nosim/your 
associate tag">Amazon Hacks</a>

If you're posting about the latest book you're reading or your favorite CD, all you need is the ASIN number, and the ASIN plug-in will format everything for you.

Even if you don't use Blosxom, this method of quickly creating links can be added to other content management systems.

70.1 The Code

This script uses a regular expression to match the special linking format inside any post. If the pattern is found, it replaces asin:ASIN with the proper URL. Add the following code to a file called asin (no extension necessary).

# Blosxom Plugin: ASIN
# Author: Nelson Minar <nelson@monkey.org>
# Version: 20030301
# http://www.nelson.monkey.org/~nelson/weblog/
# License: Public Domain

# ASIN is a Blosxom plugin that simplifies linking into Amazon's catalog.
# The link format is not a guaranteed standard, in particular the 
# ref=nosim part. For the authority see
# https://associates.amazon.com/exec/panama/associates/resources/build-[RETURN]

# Installation:
#   Modify the $associateID (or don't :-)
#   Drop asin into your plugins directory

package asin;

$associateID = 'insert associate ID';

sub start {

sub rewriteASIN {
    my ($url) = @_;
    $url =~ s%"asin:(.+)"%"\"http://www.amazon.com/exec/obidos/ASIN/[RETURN]
    return $url;    

# Modify the body of stories by rewriting asin: URIs to real URLs
sub story {
    my($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
    # Rewrite any <a href="asin:..."> tag in the post body
    $$body_ref =~ s/(<a\s[^>]*href="asin:[^>]+>)/rewriteASIN($1)/geis;


70.2 Running the Hack

This hack is run from within Blosxom every time you add a post. To set it up, drop the file asin into your configured plug-in directory. Your new shortcut method should be ready to go!

70.3 See Also

  • The buy_from_amazon Blosxom plug-in (http://www.raelity.org/apps/blosxom/plugins/link/amazon/asin.individual)