New Apple affiliate link scripts

This afternoon, I followed James Thompson’s tweet to learn about Apple’s new affiliate program. It’s dropping the slow and exceedingly clumsy LinkShare and moving to the Performance Horizon Group (PHG).1 The changeover will be complete on October 1, so if you don’t switch over by then, your affiliate earnings will dry up.

I clicked from one Apple page to the next, trying to learn how to transfer my account details from LinkShare to PHG. Luckily, just as my frustration was about to hit the boiling point, Underscore David Smith came through with this post that answered my question.

To get setup with the new program the first thing you need to do is sign up for a new account with PHG. Your old LinkShare account is essentially deprecated at this point and won’t carry forward.

Would it have been so hard for Apple to just come out and say this? I signed up for a new account with PHG and was approved in minutes.

UDS’s post continues his excellent tradition of giving fellow affiliates exactly what they need to create their own affiliate links without the rigamarole of the Link Maker tool. I immediately changed my sidebar affiliate link script to match his latest recommendations and updated the links over there to the PHG style.

The changes are minimal. Here’s the new script:

 1:  #!/usr/bin/python
 3:  import itunes
 4:  import re
 5:  import sys
 7:  # Called from the command line as
 8:  #
 9:  #   apple-sidebar-link <url>
10:  #
11:  # The URL may need to be quoted to avoid problems with shell special characters.
13:  # Personalize
14:  atoken = 'xxxxxx'
15:  ctoken = 'sidebarlinks'
17:  # Dictionaries keyed on media type. For the full list of types, see
18:  #
19:  linkIDs = {"12": "macapp-popup",
20:              "8": "iosapp-popup"}
21:  headerText = {"12": "Mac App Store link:",
22:                 "8": "App Store link:"}
24:  # Get the item ID and media type (if present) from the URL on the command line.
25:  # "itunes-popup" is the default link ID.
26:  itemID ='/id([^/?%]+)', sys.argv[1]).group(1)
27:  try:
28:    mt ='[&?]mt=(\d+)', sys.argv[1]).group(1)
29:    linkID = linkIDs.get(mt, "itunes-popup")
30:    header = headerText.get(mt, "iTunes Store link:")
31:  except AttributeError:
32:    linkID = "itunes-popup"
33:    header = "iTunes Store link:"
35:  # Lookup the item.
36:  item = itunes.lookup(itemID)
37:  itemURL = item.get_url()
38:  if '?' in itemURL:
39:    itemURL += "&at=%s&ct=%s" % (atoken, ctoken)
40:  else:
41:    itemURL += "?at=%s&ct=%s" % (atoken, ctoken)
43:  # Apps always seem to have a link to 512x512 artwork, but music and movies
44:  # top out at 100x100, even though iTunes has much larger images. Since the
45:  # image URLs include the size, we can link to the 600x600 image by changing
46:  # that part of the URL.
47:  if linkID == "itunes-popup":
48:    imageURL = item.get_artwork()['100'].replace('100x100', '600x600')
49:  else:
50:    imageURL = item.get_artwork()['512']
51:  name = item.get_name()
53:  # Print the HTML to be added to the sidebar
54:  print '''<h1><a href="{0}" rel="nofollow" class="affiliate-header">{4}<br />&nbsp;{2}</a></h1>
56:  <div class="affiliate">
57:    <img onclick="javascript:$('#{3}').show()" src="{1}" alt="{2}" title="Click for description" />
58:    <div id="{3}" class="affiliate-popup">
60:    <a onclick="javascript:$('#{3}').hide()">&#x2612</a>
61:    </div>
62:  </div>'''.format(itemURL, imageURL, name, linkID, header)

The new stuff is in Lines 13-15, where I’ve added my PHG token and a “campaign token” (see UDS’s post for details on that), and Lines 38-41, where they get used to generate the affiliate link.

The general workings and motivation for the script are given in the post that described the original version of it. Basically, I find an Apple web page or iTunes or App Store link to the product I want to feature and use that URL as the argument to the script:

apple-sidebar-link ''

What pops out is a chunk of HTML to insert into my sidebar, complete with links to the store and artwork. All I need to add is my personalized description of the product.

I’ve also updated my TextExpander shell snippet for generating Apple affiliate links to the new system. It assumes a regular Apple product URL is on the clipboard and outputs an affiliate URL. Handy when I want to add a link to the body of a post.

Apple affiliate link snippet

The script itself is here:

 1:  #!/usr/bin/python
 3:  from subprocess import check_output
 4:  from sys import stdout
 6:  # My affiliate ID.
 7:  myID = 'xxxxxx'
 9:  # Get the URL from the clipboard.
10:  clipURL = check_output('pbpaste')
12:  # Add my ID and the partnerId parameter to the URL. If there are already
13:  # queries, add them as additional ones; if not, add them as the only ones.
14:  if '?' in clipURL:
15:    itemURL = '%s&at=%s' % (clipURL, myID)
16:  else:
17:    itemURL = '%s?at=%s' % (clipURL, myID)
19:  # Write it out
20:  stdout.write(itemURL)

  1. I’m writing this from a US perspective. Check the Apple page to get the details right for your country.