1 ;;; org-tag-eldoc-wikipedia.el --- Web Scraping on Wikipedia -*- lexical-binding: t; -*-
2 ;; -*- coding: utf-8 -*-
4 ;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved.
8 ;; search API: https://wikipedia.org/w/index.php?search=[query]
9 ;; tag URL: https://en.wikipedia.org/wiki/Computer_science
15 (require 'elquery nil t
) ; optionally use elquery.el to replace dom.el.
16 (require 'org-tag-eldoc-common
)
19 (defun org-tag-eldoc-wikipedia--request (tag)
20 "Send HTTP GET request to get TAG explanation data."
21 (let ((request-backend 'url-retrieve
) ; use `url-retrieve' backend for proxy.
22 (url-proxy-services org-tag-eldoc-request-proxy
)
23 (url (format "https://en.wikipedia.org/wiki/%s" (capitalize tag
)))
29 :parser
(lambda () (libxml-parse-html-region (point-min) (point-max))) ; convert HTML -> Elisp alist structure
31 (lambda (&key data
&allow-other-keys
)
32 ;; DEBUG: (setq request-result data)
33 ;; for `libxml' parser
34 (setq org-tag-eldoc--explanation
35 (org-tag-eldoc-common--format-explanation
36 (dom-texts (dom-by-class data
"mw-content-container"))))))
38 (lambda (&rest args
&key error-thrown
&allow-other-keys
)
39 (error "request error!")))
40 :status-code
'((404 .
(lambda (&rest _
) (message (format "Endpoint %s does not exist." url
))))
41 (500 .
(lambda (&rest _
) (message (format "Error from %s." url
)))))))
45 :parser
(lambda () (elquery-read-buffer (current-buffer))) ; convert HTML -> elquery object structure
47 (lambda (&key data
&allow-other-keys
)
48 ;; DEBUG: (setq request-result data)
49 ;; for `elquery' parser
50 (setq org-tag-eldoc--explanation
51 (org-tag-eldoc-common--format-explanation
52 (elquery-text (car (elquery-$
".mw-content-container" data
)))))))
54 (lambda (&rest args
&key error-thrown
&allow-other-keys
)
55 (error "[org-tag-eldoc] request error!")
56 "request error" ; return identity string for `org-tag-eldoc-tag-explanation''s `seq-some' -> `pcase'.
58 :status-code
'((404 .
(lambda (&rest _
) (message (format "Endpoint %s does not exist." url
))))
59 (500 .
(lambda (&rest _
) (message (format "Error from %s." url
))))))))))
61 ;;; TEST: https://en.wikipedia.org/wiki/Computer_science
62 ;; (org-tag-eldoc-wikipedia--request "computer_science")
64 (defun org-tag-eldoc-wikipedia-query (tag)
65 "Query TAG on Wikipedia then return a cons cell of tag and explanation."
66 (org-tag-eldoc-database-query tag
)
67 (if (stringp org-tag-eldoc--explanation
)
68 org-tag-eldoc--explanation
69 (org-tag-eldoc-wikipedia--request tag
)
71 (org-tag-eldoc-database-save tag org-tag-eldoc--explanation
)))
75 (provide 'org-tag-eldoc-wikipedia
)
77 ;;; org-tag-eldoc-wikipedia.el ends here