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;
Re-reading my comment it seems harsher than I’d intended. You did the work to figure out the code so someone can just plug it in, where I did nothing at all. It was merely meant to be a tip, so I hope it didn’t offend.
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.
Semi-colons in python code? That just looks wrong.
I am not a native python speaker. I am used to C#, Java, and (when I have to) javascript, which all require/encourage semi-colons.
Re-reading my comment it seems harsher than I’d intended. You did the work to figure out the code so someone can just plug it in, where I did nothing at all. It was merely meant to be a tip, so I hope it didn’t offend.