Sidebar affiliate links
May 6, 2013 at 11:45 PM by Dr. Drang
If you’re reading this on the site rather than through an RSS reader, you’ll see that the sidebar over on the right has changed. Gone are the Flickr photos, which I hadn’t updated since the fall, and the tag cloud, which I doubt that anyone used. In their place are affiliate links to Amazon, the Mac App Store, and the iOS App Store.
As I mentioned a few months ago, I’m not entirely comfortable with conventional web ads on the site, but I have no problem with affiliate links. I’ve been using them in my posts for quite a while when I recommend an application or book or pen or whatever. Now they’ll be in the sidebar.
I’ll certainly be tweaking the format, but what you see now is—in broad terms, if not in detail—the way I expect it to stay: an Amazon product, a Mac application, and an iOS app. A decent-sized image of each, with a sentence or two from me explaining why I recommend it. At present, the text appears when you hover over the image via the title
attribute. That was easy to implement, but isn’t a permanent solution because hovering isn’t a concept that applies to touch devices. A popup system that works just as well on mobile and the desktop will be one of my first changes.1
At present, I expect to switch the recommendations every week or two, but we’ll see how that works out. I’m notoriously lazy2 and may leave some links up for longer.
I have, of course, written a couple of scripts to make it easier to add the link code to my WordPress template. Here’s the script that generates the Amazon link:
python:
1: #!/usr/bin/python
2:
3: from amazonproduct import API
4: import sys
5: import re
6:
7: # Get the item's ID from the URL on the command line.
8: itemID = re.search(r'dp/([^/?%]+)', sys.argv[1]).group(1)
9: # print itemID
10: # sys.exit()
11:
12: # Login.
13: api = API('myaccesskey', 'mysecretkey', 'us', 'myassociateID')
14:
15: # Lookup the item.
16: node = api.item_lookup(itemID, ResponseGroup="Images,Small")
17: itemURL = node.Items.Item.DetailPageURL
18: imageURL = node.Items.Item.LargeImage.URL
19: author = node.Items.Item.ItemAttributes.Author
20: title = node.Items.Item.ItemAttributes.Title
21:
22: # Print the HTML to be added to the sidebar
23: print '''<p>
24: <a href="{0}"><img src="{1}" alt="{2} by {3}" title="" /></a>
25: </p>'''.format(itemURL, imageURL, title, author)
This takes the URL of an Amazon product as its command-line argument and returns the HTML link code. To use it requires an Amazon Associates account, a Product Advertising API account, and the python-amazon-product-api
library.
The comments in the script pretty much tell you what’s going on. You’ll want to edit Line 8 to use your own API access and secret keys and your own Associate ID. Lines 17-20 show how deeply nested the return values are; figuring out how to extract the fields I needed was the most difficult part of this script.
The script is called this way:
amazon-sidebar-link 'http://www.amazon.com/Idiot-America-Stupidity-Became-Virtue/dp/0767926153/ref=la_B001IQXNKY_1_1?ie=UTF8&qid=1367898622&sr=1-1' | pbcopy
The URL is put in single quotes to keep the ampersands (and any other special characters) from being interpreted by the shell. I typically use my ;furl
TextExpander snippet to insert the URL of the page I’m browsing so I don’t have to jump back and forth between the Terminal and the browser. Piping the output through pbcopy
puts the HTML output into the clipboard, ready for pasting into the template.
A similar script uses the python-itunes
library to access the iTunes Search API:
python:
1: #!/usr/bin/python
2:
3: import itunes
4: import re
5: import sys
6:
7: # Get the item ID from the URL on the command line.
8: itemID = re.search(r'/id([^/?%]+)', sys.argv[1]).group(1)
9: # print itemID
10: # sys.exit()
11:
12: # Lookup the item.
13: item = itunes.lookup(itemID)
14: itemURL = item.get_url() + "&uo=4&partnerId=30&siteID=mysiteID"
15: imageURL = item.get_artwork()['512']
16: name = item.get_name()
17: publisher = item.get_artist()
18:
19: # Print the HTML to be added to the sidebar
20: print '''<p>
21: <a href="{0}"><img src="{1}" alt="{2} by {3}" title="" /></a>
22: </p>'''.format(itemURL, imageURL, name, publisher)
Here, the only things you’ll need to edit are the siteID
item and (possibly) the uo
and partnerID
items3 in Line 14. You’ll need to be a member of Apple’s Affiliate program to get those items. This script is called exactly the same way as the Amazon script above. The argument is the URL of the itunes.apple.com
web page for the product.
To help me find the right web page quickly, I use Google’s site-specific searching restriction. For example, a search on
bbedit site:itunes.apple.com
brings me immediately to the page I want. Of course, I have a TextExpander snippet that expands to site:itunes.apple.com
.
It’d be nice to get the product ID number directly from iTunes or the App Store application, but I don’t know how.
You’ll notice that in each script, I leave the title
attribute of the <img>
blank. That’s where I add my comment on the item after pasting the output into my blog template.
This site doesn’t get enough traffic for the earnings on affiliate links amount to much, but they should allow the site to pay for itself. Actually, only iTunes/App Store earnings come to me. Earnings from the Amazon account go to my daughter who, unlike many her age, didn’t come back to sponge off live with her parents after college. It’s not enough to pay for her coffee habit, but it helps.
Update 5/7/13
I should’ve mentioned that the images used in the links—which are served directly from Amazon and Apple—are larger than what you see in the sidebar. That’s because the CSS for the sidebar has max-width
and max-height
settings that tell the browser to scale the images down. This wastes a little bandwidth, but I don’t have to redesign the sidebar to match the size of the images Amazon and Apple deliver.
-
And I should probably apply that solution—whatever it turns out to be—to footnotes, too. ↩
-
See Flickr comment above. ↩
-
I confess I’ve forgotten whether those items are generic or specific to your account, and I just don’t feel like looking it up (see earlier comment on laziness). If your affiliate links use different values, then they must be account-specific. ↩