enable `eldoc-mode` on `org-tag-eldoc-mode` enabled
[org-tag-eldoc.git] / org-tag-eldoc-pixiv-encyclopedia.el
blobb3de90dc29f216bf1761dcbae8cd00093ed4456d
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.
6 ;;; Commentary:
8 ;; search API: https://dic.pixiv.net/en/search?query=[query]
9 ;; tag URL: https://dic.pixiv.net/en/a/Mating%20Press
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-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"
27 (url-hexify-string
28 (string-replace "_" " " (capitalize tag))))))
29 (request url
30 :type "GET"
31 :parser (lambda ()
32 (cond
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)))))
37 :success (cl-function
38 (lambda (&key data &allow-other-keys)
39 ;; DEBUG: (setq request-result data)
40 (cond
41 ((featurep 'dom)
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"))))))
46 ((featurep 'elquery)
47 ;; for `elquery' parser
48 (setq org-tag-eldoc--explanation
49 (org-tag-eldoc-common--format-explanation
50 (elquery-text (car (elquery-$ ".summary" data)))))))))
51 :error (cl-function
52 (lambda (&rest args &key error-thrown &allow-other-keys)
53 ;; (message "[org-tag-eldoc] (Pixiv Encyclopedia) request error %s!" error-thrown)
54 nil))
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)
68 (sit-for 1.0)
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