This function will inspect a url and if it is a well formed amazon product url it will modify it to use the given affiliateId, and if it is not, it returns the url unchanged.
I am assuming that the code base knows when there is a url (which should be part of the implementation of mark down). Is there anyone more familiar with the code base who could use this function to make the proposed feature? Also we still need to know what affiliateId to use, which should probably be configurable by an administrator.
import urlparse;
def SetAmazonAffiliate(url, affiliateId):
uri = urlparse.urlparse(url);
address = uri[1];
if ((address.lower() == "amazon.com") | address.lower().endswith(".amazon.com")):
path = uri[2];
i = path.find("/dp/");
if (i < 0):
return url;
i = path.find("/", i+4);
if (i < 0):
return url;
return uri[0]+"://"+address+path[:i+1]+"ref=nosim?tag="+affiliateId;
return url;
Semi-colons in python code? That just looks wrong.
My site, which gets slightly less traffic than Less Wrong, makes me $50-$200 per month with Amazon referral links and no advertising at all. I don't profit this way, but I recoup some of my costs.
I think the Less Wrong audience is much more likely to purchase books - including expensive textbooks - than my audience is. And it would be easy to automatically transform all Amazon links on Less Wrong so that they include a Less Wrong Amazon Associates referral code, which would make Less Wrong some money on every purchase made through one of those links.
Below is the pseudocode, assuming Less Wrong's Amazon Associates referral code is "lesswrong". Note that every Amazon page for an item contains at least the following string: "/dp/XXXXXXXXXX/" where XXXXXXXXXX is alphanumeric.
That's it! For example, let's say somebody wrote a post and pasted in the following Amazon hyperlink from their browser's address bar:
This would be transformed into:
This is precisely what I do on my own site. Works like a charm. If somebody clicks my book link but ends up buying a $1000 DSLR instead, I get about $65 for that one purchase.
Somebody who didn't give up programming in 8th grade could do this fairly easily I imagine, and it would help cover Less Wrong's expenses.