1 ;;; org-tag-eldoc-pixiv-encyclopedia.el --- Web Scraping on Pixiv Encyclopedia -*- lexical-binding: t; -*-
2 ;; -*- coding: utf-8 -*-
4 ;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved.
8 ;; search API: https://dic.pixiv.net/en/search?query=[query]
9 ;; tag URL: https://dic.pixiv.net/en/a/Mating%20Press
15 (require 'elquery nil t
) ; optionally use elquery.el to replace dom.el.
16 (require 'org-tag-eldoc-common
)
19 (defun org-tag-eldoc-pixiv-encyclopedia--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://dic.pixiv.net/en/a/%s"
25 (string-replace "_" " " (capitalize tag
))))))
30 :parser
(lambda () (libxml-parse-html-region (point-min) (point-max))) ; convert HTML -> Elisp alist structure
32 (lambda (&key data
&allow-other-keys
)
33 ;; DEBUG: (setq request-result data)
34 ;; for `libxml' parser
35 (setq org-tag-eldoc--explanation
36 (org-tag-eldoc-common--format-explanation
37 (string-clean-whitespace
38 (dom-text (car (dom-by-class data
"summary"))))))))
40 (lambda (&rest args
&key error-thrown
&allow-other-keys
)
41 (error "[org-tag-eldoc] request error %s!" error-thrown
)
43 :status-code
'((404 .
(lambda (&rest _
) nil
))
44 (500 .
(lambda (&rest _
) nil
)))))
48 :parser
(lambda () (elquery-read-buffer (current-buffer))) ; convert HTML -> elquery object structure
50 (lambda (&key data
&allow-other-keys
)
51 ;; DEBUG: (setq request-result data)
52 ;; for `elquery' parser
53 (setq org-tag-eldoc--explanation
54 (org-tag-eldoc-common--format-explanation
55 (elquery-text (car (elquery-$
".summary" data
)))))))
57 (lambda (&rest args
&key error-thrown
&allow-other-keys
)
58 (error "[org-tag-eldoc] request error %s!" error-thrown
)
60 :status-code
'((404 .
(lambda (&rest _
) nil
))
61 (500 .
(lambda (&rest _
) nil
)))
64 ;;; TEST: https://dic.pixiv.net/en/a/Mating%20Press
65 ;; (org-tag-eldoc-pixiv-encyclopedia--request "mating_press")
67 (defun org-tag-eldoc-pixiv-encyclopedia-query (tag)
68 "Query TAG on Pixiv Encyclopedia then return a cons cell of tag and explanation."
69 ;; search: https://dic.pixiv.net/en/search?query=<tag>
70 ;; tag page: https://dic.pixiv.net/en/a/<url%20hex%20coded%20tag>
71 (org-tag-eldoc-database-query tag
)
72 (let ((explanation org-tag-eldoc--explanation
))
73 (if (stringp explanation
)
75 (org-tag-eldoc-pixiv-encyclopedia--request tag
)
77 (org-tag-eldoc-database-save tag explanation
))))
81 (provide 'org-tag-eldoc-pixiv-encyclopedia
)
83 ;;; org-tag-eldoc-pixiv-encyclopedia.el ends here