Add backend name to error message
[org-tag-eldoc.git] / org-tag-eldoc-moegirl.el
blobee450c58c9410338275a01b7609ff2db6632075e
1 ;;; org-tag-eldoc-moegirl.el --- org-tag-eldoc backend with moegril.org.cn -*- lexical-binding: t; -*-
2 ;; -*- coding: utf-8 -*-
4 ;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved.
6 ;;; Commentary:
8 ;;; Code:
9 \f
10 (require 'url)
11 (require 'url-http) ; for `url-http-end-of-headers'
12 (require 'request)
13 (require 'dom)
14 (require 'elquery nil t) ; optionally use elquery.el to replace dom.el.
15 (require 'org-tag-eldoc-common)
18 (defvar org-tag-eldoc-moegirl-url nil
19 "A variable to store the url of moegirl.")
21 (defun org-tag-eldoc-moegirl--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 (url-proxy-services org-tag-eldoc-request-proxy)
25 (url (format "https://zh.moegirl.org.cn/%s" (url-hexify-string tag))))
26 (request url
27 :type "GET"
28 :parser (lambda ()
29 (cond
30 ((fboundp 'libxml-parse-html-region) ; convert HTML -> Elisp alist structure
31 (libxml-parse-html-region (point-min) (point-max)))
32 ((featurep 'elquery) ; convert HTML -> elquery object structure
33 (elquery-read-buffer (current-buffer)))))
34 :success (cl-function
35 (lambda (&key data &allow-other-keys)
36 ;; DEBUG: (setq request-result data)
37 (cond
38 ((featurep 'dom)
39 ;; for `libxml' parser
40 (setq org-tag-eldoc--explanation
41 (dom-texts
42 ;; FIXME: The content text is dynamically loaded by JavaScript.
43 (dom-search (dom-by-id data "mw-content-text")
44 (lambda (node) (and (eq (cl-first node) 'p) (null (cl-second node))))))))
45 ((featurep 'elquery)
46 ;; for `elquery' parser
47 (setq org-tag-eldoc--explanation
48 (elquery-text (car (elquery-$ "#mw-content-text > div > p" data))))))))
49 :error (cl-function
50 (lambda (&rest args &key error-thrown &allow-other-keys)
51 (error "[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:
61 ;; (setq request-result dom)
62 ;; (org-tag-eldoc-common--format-explanation
63 ;; (cond
64 ;; ((featurep 'dom)
65 ;; (dom-texts
66 ;; ;; FIXME: The content text is dynamically loaded by JavaScript.
67 ;; (dom-search (dom-by-id dom "mw-content-text")
68 ;; (lambda (node) (and (eq (cl-first node) 'p) (null (cl-second node)))))))
69 ;; ((featurep 'elquery)
70 ;; (elquery-text (car (elquery-$ "#mw-content-text > div > p" data))))))))
73 ;;; TEST:
74 ;;; search tag: https://zh.moegirl.org.cn/index.php?search=linux
75 ;;; tag page: https://zh.moegirl.org.cn/Linux娘, https://zh.moegirl.org.cn/Linux%E5%A8%98
76 ;; (org-tag-eldoc-moegirl--request "Linux娘")
78 ;;; TEST
79 ;; (dom-texts
80 ;; (dom-search (dom-by-id request-result "mw-content-text")
81 ;; (lambda (node) (and (eq (car node) 'p) (null (cl-second node))))))
84 (defun org-tag-eldoc-moegirl-query (tag)
85 "Query TAG on moegirl.org.cn then return a cons cell of tag and explanation."
86 (org-tag-eldoc-database-query tag)
87 (if (stringp org-tag-eldoc--explanation)
88 org-tag-eldoc--explanation
89 (org-tag-eldoc-moegirl--request tag)
90 (sit-for 1.0)
91 (org-tag-eldoc-database-save tag org-tag-eldoc--explanation)))
95 (provide 'org-tag-eldoc-moegirl)
97 ;;; org-tag-eldoc-moegirl.el ends here