1 ;;; org-tag-eldoc-baidu-baike.el --- Web Scraping on Baidu Baike -*- lexical-binding: t; -*-
2 ;; -*- coding: utf-8 -*-
4 ;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved.
8 ;; search API: http://baike.baidu.com/search/none?word=[query]
9 ;; tag URL: https://baike.baidu.com/item/[tag]
14 (require 'url-http
) ; for `url-http-end-of-headers'
17 (require 'elquery nil t
) ; optionally use elquery.el to replace dom.el.
18 (require 'org-tag-eldoc-common
)
21 (defun org-tag-eldoc-baidu-baike--request (tag)
22 "Send HTTP GET request to get TAG explanation data."
23 (let ((request-backend 'url-retrieve
) ; use `url-retrieve' backend for proxy.
24 (request-message-level -
1)
25 (url-proxy-services org-tag-eldoc-request-proxy
)
26 (url (url-encode-url ; encode Chinese query text into %X URL.
27 (format "https://baike.baidu.com/item/%s" (capitalize tag
)))))
32 ((fboundp 'libxml-parse-html-region
) ; convert HTML -> Elisp alist structure
33 (libxml-parse-html-region (point-min) (point-max)))
34 ((featurep 'elquery
) ; convert HTML -> elquery object structure
35 (elquery-read-buffer (current-buffer)))))
37 (lambda (&key data
&allow-other-keys
)
38 ;; DEBUG: (setq request-result data)
41 ;; for `libxml' parser
42 (setq org-tag-eldoc--explanation
43 (org-tag-eldoc-common--format-explanation
44 (dom-texts (dom-by-class data
"lemmaSummary_wv32m\\ J-summary")))))
46 ;; for `elquery' parser
47 (setq org-tag-eldoc--explanation
48 (elquery-text (car (elquery-$
"#J-lemma-main-wrapper > div.contentWrapper_QxCiu > div > div.mainContent_E1dlD > div > div.lemmaSummary_wv32m.J-summary" data
))))))))
50 (lambda (&rest args
&key error-thrown
&allow-other-keys
)
51 ;; (message "[org-tag-eldoc] (百度百科) request error %s!" error-thrown)
53 :status-code
'((404 .
(lambda (&rest _
) nil
))
54 (500 .
(lambda (&rest _
) nil
))))
56 ;; (with-current-buffer (url-retrieve-synchronously url)
58 ;; (goto-char url-http-end-of-headers)
59 ;; (libxml-parse-html-region (point) (point-max)))))
60 ;; ;; DEBUG: (setq request-result dom)
61 ;; (org-tag-eldoc-common--format-explanation
65 ;; (dom-search (dom-by-class dom "mw-content-ltr\\ mw-parser-output")
66 ;; (lambda (node) (and (eq (cl-first node) 'p) (null (cl-second node)))))))
67 ;; ((featurep 'elquery)
68 ;; (elquery-text (car (elquery-$ "#mw-content-text > div.mw-content-ltr.mw-parser-output > p" data))))))))
71 ;;; TEST: https://baike.baidu.com/item/Linux
72 ;; (org-tag-eldoc-baidu-baike--request "Linux")
75 ;; (let ((url-proxy-services org-tag-eldoc-request-proxy))
76 ;; (with-current-buffer
77 ;; (url-retrieve-synchronously "https://baike.baidu.com/item/Linux")
79 ;; (goto-char url-http-end-of-headers)
80 ;; (libxml-parse-html-region (point) (point-max)))))
81 ;; (setq request-result dom)
82 ;; (setq org-tag-eldoc--explanation
83 ;; (dom-texts (dom-by-class dom "lemmaSummary_wv32m\\ J-summary"))))))
86 ;; (dom-texts (dom-by-class request-result "lemmaSummary_wv32m\\ J-summary"))
89 (defun org-tag-eldoc-baidu-baike-query (tag)
90 "Query TAG on Baidu Baike then return a cons cell of tag and explanation."
91 (if (stringp org-tag-eldoc--explanation
)
92 org-tag-eldoc--explanation
93 (org-tag-eldoc-baidu-baike--request tag
)
95 (org-tag-eldoc-database-save tag org-tag-eldoc--explanation
)))
99 (provide 'org-tag-eldoc-baidu-baike
)
101 ;;; org-tag-eldoc-baidu-baike.el ends here