display org-tag-eldoc text on sideline left for better view indentation
[org-tag-eldoc.git] / org-tag-eldoc-baidu-baike.el
blob45130701836b7f5608f0d1b1f62617ae2e76c36c
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.
6 ;;; Commentary:
8 ;; search API: http://baike.baidu.com/search/none?word=[query]
9 ;; tag URL: https://baike.baidu.com/item/[tag]
11 ;;; Code:
13 (require 'url)
14 (require 'url-http) ; for `url-http-end-of-headers'
15 (require 'request)
16 (require 'dom)
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)))))
28 (request url
29 :type "GET"
30 :parser (lambda ()
31 (cond
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)))))
36 :success (cl-function
37 (lambda (&key data &allow-other-keys)
38 ;; DEBUG: (setq request-result data)
39 (cond
40 ((featurep 'dom)
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")))))
45 ((featurep 'elquery)
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))))))))
49 :error (cl-function
50 (lambda (&rest args &key error-thrown &allow-other-keys)
51 ;; (message "[org-tag-eldoc] (百度百科) request error %s!" error-thrown)
52 nil))
53 :status-code '((404 . (lambda (&rest _) nil))
54 (500 . (lambda (&rest _) nil))))
56 ;; (with-current-buffer (url-retrieve-synchronously url)
57 ;; (let ((dom (progn
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
62 ;; (cond
63 ;; ((featurep 'dom)
64 ;; (dom-texts
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")
74 ;;; TEST
75 ;; (let ((url-proxy-services org-tag-eldoc-request-proxy))
76 ;; (with-current-buffer
77 ;; (url-retrieve-synchronously "https://baike.baidu.com/item/Linux")
78 ;; (let ((dom (progn
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"))))))
85 ;;; TEST
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)
94 (sit-for 1.0)
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