ELisp: HTML Amazon URL Linkify

By Xah Lee. Date: . Last updated: .

This page shows a example of turning a amazon product url into a customized link.

Problem

Given a URL of any of the following form:

Change it into this form:

<a class="amz" href="http://www.amazon.com/dp/B01KR1C5PY/?tag=xahh-20" title="Kinesis Advantage2 Ergonomic Keyboard KB600">Buy at amazon</a>

Solution

(defun xah-html-amazon-linkify (&optional @tracking-id)
  "Make the current amazon URL or selection into a link.

Examples of amazon product URL formats
http://www.amazon.com/Cyborg-R-T-Gaming-Mouse/dp/B003CP0BHM/ref=pd_sim_e_1
http://www.amazon.com/gp/product/B003CP0BHM
http://www.amazon.com/exec/obidos/ASIN/B003CP0BHM/xahh-20
http://www.amazon.com/exec/obidos/tg/detail/-/B003CP0BHM/
http://www.amazon.com/dp/B003CP0BHM?tag=xahhome-20
http://amzn.to/1F5M1hA
https://alexa.design/2okfMcj

Example output:
<a href=\"http://www.amazon.com/dp/B003CP0BHM/?tag=xahh-20\" title=\"Cyborg R T Gaming Mouse\">amazon</a>

ASIN is a 10 character string that's a product id.

URL `http://xahlee.info/emacs/emacs/elisp_amazon-linkify.html'
Version 2020-01-15 2021-05-02"
  (interactive)
  (let (($bds (bounds-of-thing-at-point 'url))
        $p1 $p2 $url $asin $thingName
        ($trackId (if @tracking-id @tracking-id "xahh-20" )))
    (if (use-region-p)
        (setq $p1 (region-beginning) $p2 (region-end))
      (setq $p1 (car $bds) $p2 (cdr $bds)))
    (setq $url (buffer-substring-no-properties $p1 $p2))
    (if (or (string-match "//amzn.to/" $url)
            (string-match "//alexa.design/" $url))
        (progn (delete-region $p1 $p2)
               (insert (format "<a class=\"amz_search\" href=\"%s\">amazon</a>" $url)))
      (progn
        (setq $asin
              (cond
               ((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)/?" $url) (match-string 1 $url))
               ((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)\\?tag=" $url) (match-string 1 $url))
               ((string-match "/gp/product/\\([[:alnum:]]\\{10\\}\\)" $url) (match-string 1 $url))
               ((string-match "/ASIN/\\([[:alnum:]]\\{10\\}\\)" $url) (match-string 1 $url))
               ((string-match "/tg/detail/-/\\([[:alnum:]]\\{10\\}\\)/" $url) (match-string 1 $url))
               ((string-match "/\\([[:alnum:]]\\{10\\}\\)/" $url) (match-string 1 $url))
               ((and
                 (equal 10 (length $url ))
                 (string-match "\\`\\([[:alnum:]]\\{10\\}\\)\\'" $url))
                $url)
               (t (error "no amazon ASIN found"))))
        (setq
         $thingName
         (replace-regexp-in-string
          "-" " "
          (if (string-match "amazon\.com/\\([^/]+?\\)/dp/" $url)
              (progn (match-string 1 $url))
            (progn
              (message "no product name found" ))
            ""
            )))
        (delete-region $p1 $p2)
        (insert

         (format "<a class=\"amz\" href=\"http://www.amazon.com/dp/%s/?tag=%s\" title=\"%s\">Buy at amazon</a>" $asin $trackId $thingName))
        (search-backward "\">")))))