AmazReplacer - Convert Amazon's URL into your referrer URL automatically
I've created a small utility app that does that automatically takes whatever URL is in the clipboard , reformats it to be an Amazon referral URL and puts the result in the clipboard. Very easy and fast.
You can download the utility(with source) here, but code is pretty simple to do and provides a simple example of using regular expressions and the clipboard object. Here's the gist of it:
//happens on startup
//so all is automatic. You just open the utility and BAM
//you have the good URL in your clipboard.
//Just press ESC and you're done
private void TryToAutoReplace()
{
string current;
try
{
//get the current URL from the clipboard
//and put it in a text bo
current = (String)Clipboard.GetDataObject().GetData(typeof(string));
}
catch(Exception e){return;}
txtBefore.Text= current;
//replace the text in that textbox to the URL
ReplaceAmazon();
}
private void ReplaceAmazon()
{
//find the Book ID from the URL
//could start with either 'ASIN/[ID]/' or 'tg/detail/-/[ID]/'
const string pattern = @".*(ASIN|tg/detail/-)/(?<book>.\d*)/.*";
Match book = Regex.Match(txtBefore.Text,pattern);
if (book.Success)
{
//add you referrer ID to the end of the 'good' URL
// thanks to Hugh Brown for showing the short version of the url
txtAfter.Text =@"http://www.amazon.com/o/ASIN/" +
book.Groups["book"].Value + @"/iserializable-20";
//make the result available via "paste"
Clipboard.SetDataObject(txtAfter.Text ,true);
}
}