save `org-tag-eldoc--explanation` instead of `explanation`
[org-tag-eldoc.git] / org-tag-eldoc-wikipedia.el
blob9401c553e336c20a3f66b85f4afe0e56019a2c21
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.
6 ;;; Commentary:
8 ;; search API: https://wikipedia.org/w/index.php?search=[query]
9 ;; tag URL: https://en.wikipedia.org/wiki/Computer_science
11 ;;; Code:
13 (require 'request)
14 (require 'dom)
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)))
24 (summary))
25 (cond
26 ((featurep 'dom)
27 (request url
28 :type "GET"
29 :parser (lambda () (libxml-parse-html-region (point-min) (point-max))) ; convert HTML -> Elisp alist structure
30 :success (cl-function
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"))))))
37 :error (cl-function
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)))))))
42 ((featurep 'elquery)
43 (request url
44 :type "GET"
45 :parser (lambda () (elquery-read-buffer (current-buffer))) ; convert HTML -> elquery object structure
46 :success (cl-function
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)))))))
53 :error (cl-function
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)
70 (sit-for 1.0)
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