Last active
February 19, 2025 04:33
-
-
Save alphapapa/736c429687d6b13400073d7ebd9256fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(cl-defun ap/feed-for-url (url &key (prefer 'atom) (all nil)) | |
"Return RSS/ATOM feed URLs for web page at URL." | |
(cl-flet ((add-protocol (protocol url) | |
(unless (s-starts-with? protocol url) | |
(setq url (if (s-starts-with? "//" url) | |
(concat protocol ":" url) | |
(concat protocol "://" url)))) | |
url)) | |
(let* ((protocol (second (s-match (rx (group "http" (optional "s")) "://") | |
url))) | |
(preferred-type (format "application/%s+xml" (symbol-name prefer))) | |
(html (org-web-tools--get-url url)) | |
(dom (with-temp-buffer | |
(insert html) | |
(libxml-parse-html-region (point-min) (point-max)))) | |
(potential-feeds (esxml-query-all "link[rel=alternate]" dom))) | |
;; Return one or more URLs | |
(if all | |
;; Return all URLs | |
(cl-loop for (tag . (attrs)) in potential-feeds | |
collect (add-protocol protocol (alist-get 'href attrs))) | |
;; Return the first URL of preferred type | |
(cl-loop for (tag . (attrs)) in potential-feeds | |
when (equal preferred-type (alist-get 'type attrs)) | |
return (add-protocol protocol (alist-get 'href attrs))))))) | |
;; Use like: | |
;; (ap/feed-for-url "http://soylentnews.org/") | |
;; (ap/feed-for-url "http://soylentnews.org/" :prefer 'rss) | |
;; (ap/feed-for-url "http://soylentnews.org/" :all t) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment