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
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-pixiv-encyclopedia--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 (format "https://dic.pixiv.net/en/a/%s"
28 (string-replace "_" " " (capitalize tag
))))))
33 ((fboundp 'libxml-parse-html-region
) ; convert HTML -> Elisp alist structure
34 (libxml-parse-html-region (point-min) (point-max)))
35 ((featurep 'elquery
) ; convert HTML -> elquery object structure
36 (elquery-read-buffer (current-buffer)))))
38 (lambda (&key data
&allow-other-keys
)
39 ;; DEBUG: (setq request-result data)
42 ;; for `libxml' parser
43 (setq org-tag-eldoc--explanation
44 (org-tag-eldoc-common--format-explanation
45 (dom-text (car (dom-by-class data
"summary"))))))
47 ;; for `elquery' parser
48 (setq org-tag-eldoc--explanation
49 (org-tag-eldoc-common--format-explanation
50 (elquery-text (car (elquery-$
".summary" data
)))))))))
52 (lambda (&rest args
&key error-thrown
&allow-other-keys
)
53 ;; (message "[org-tag-eldoc] (Pixiv Encyclopedia) request error %s!" error-thrown)
55 :status-code
'((404 .
(lambda (&rest _
) nil
))
56 (500 .
(lambda (&rest _
) nil
))))))
58 ;;; TEST: https://dic.pixiv.net/en/a/Mating%20Press
59 ;; (org-tag-eldoc-pixiv-encyclopedia--request "mating_press")
61 (defun org-tag-eldoc-pixiv-encyclopedia-query (tag)
62 "Query TAG on Pixiv Encyclopedia then return a cons cell of tag and explanation."
63 ;; search: https://dic.pixiv.net/en/search?query=<tag>
64 ;; tag page: https://dic.pixiv.net/en/a/<url%20hex%20coded%20tag>
65 (if (stringp org-tag-eldoc--explanation
)
66 org-tag-eldoc--explanation
67 (org-tag-eldoc-pixiv-encyclopedia--request tag
)
69 (org-tag-eldoc-database-save tag org-tag-eldoc--explanation
)))
73 (provide 'org-tag-eldoc-pixiv-encyclopedia
)
75 ;;; org-tag-eldoc-pixiv-encyclopedia.el ends here