Emacs: HTML. Amazon URL to Link ðŸ’
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:
- https://www.amazon.com/Kinesis-Advantage2-Ergonomic-Keyboard-KB600/dp/B01KR1C5PY/
- https://www.amazon.com/gp/product/B01KR1C5PY
- https://www.amazon.com/exec/obidos/ASIN/B01KR1C5PY/
- https://www.amazon.com/exec/obidos/tg/detail/-/B01KR1C5PY/
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
(defvar xah-html-amazon-asso-id nil "A string, amazon associate tracking id, used by `xah-html-amazon-url-to-link'. It can be empty string, if none.") (setq xah-html-amazon-asso-id "xahh-20") (defun xah-html-amazon-get-asin-id (Url) "Return the amazon ASIN product id. If cannot find, return nil. Version: 2023-07-16" (let ((xurl Url)) (cond ((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)/?" xurl) (match-string 1 xurl)) ((string-match "/dp/\\([[:alnum:]]\\{10\\}\\)\\?tag=" xurl) (match-string 1 xurl)) ((string-match "/gp/product/\\([[:alnum:]]\\{10\\}\\)" xurl) (match-string 1 xurl)) ((string-match "/ASIN/\\([[:alnum:]]\\{10\\}\\)" xurl) (match-string 1 xurl)) ((string-match "/tg/detail/-/\\([[:alnum:]]\\{10\\}\\)/" xurl) (match-string 1 xurl)) ((string-match "/\\([[:alnum:]]\\{10\\}\\)/" xurl) (match-string 1 xurl)) ((and (equal 10 (length xurl)) (string-match "\\`\\([[:alnum:]]\\{10\\}\\)\\'" xurl)) xurl) (t nil)))) (defun xah-html-amazon-url-to-link (&optional Begin End) "Make the current amazon URL or selection into a link. When called interactively, `xah-html-amazon-asso-id' is added to the link for amazon affliates. Example result: <a class=\"amz\" href=\"https://www.amazon.com/dp/B088BC5HMM/?tag=xahh-20\" title=\"ASUS PA278QV DisplayPort Anti Glare Adjustable\" target=\"_blank\">Buy at amazon</a> URL `http://xahlee.info/emacs/emacs/elisp_amazon-linkify.html' Created: 2020-01-15 Version: 2024-06-22" (interactive (let (xbeg xend) (if (region-active-p) (setq xbeg (region-beginning) xend (region-end)) (save-excursion (progn (skip-chars-backward "^ \t\n") (setq xbeg (point)) (skip-chars-forward "^ \t\n") (setq xend (point))))) (list xbeg xend))) (let (xbeg xend xurl xasin xprodName xoutput (xassId xah-html-amazon-asso-id)) (if (and Begin End) (setq xbeg Begin xend End) (if (region-active-p) (setq xbeg (region-beginning) xend (region-end)) (progn (skip-chars-backward "^ \t\n") (setq xbeg (point)) (skip-chars-forward "^ \t\n") (setq xend (point))))) (setq xurl (buffer-substring-no-properties xbeg xend)) ;; (print (format "xurl %s" xurl)) (if (string-match "www\.amazon\.com/\\|//amzn\.to/" xurl) (if (string-match "//amzn.to/" xurl) (progn (delete-region xbeg xend) (setq xoutput (format "<a class=\"amz_search\" href=\"%s\" target=\"_blank\">amazon</a>" xurl)) (insert xoutput) xoutput) (progn (setq xasin (xah-html-amazon-get-asin-id xurl)) (when (or (not xasin) (string-equal "" xasin)) (error "no amazon ASIN found in url %s" xurl)) (setq xprodName (string-replace "-" " " (if (string-match "amazon\.com/\\([^/]+?\\)/dp/" xurl) (match-string 1 xurl) (progn (message "no product name found") "")))) (delete-region xbeg xend) (setq xoutput (format "<a class=\"amz\" href=\"https://www.amazon.com/dp/%s/?tag=%s\" title=\"%s\" target=\"_blank\">Buy at amazon</a>" xasin xassId xprodName)) (insert xoutput)) xoutput) nil)))