Fix query function returned data processing logic
[org-tag-eldoc.git] / org-tag-eldoc-pixiv-encyclopedia.el
blob47fa5d12a7e13da0e34c5eaebfaccb011167037d
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 'request)
14 (require 'dom)
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"
24 (url-hexify-string
25 (string-replace "_" " " (capitalize tag))))))
26 (cond
27 ((featurep 'dom)
28 (request url
29 :type "GET"
30 :parser (lambda () (libxml-parse-html-region (point-min) (point-max))) ; convert HTML -> Elisp alist structure
31 :success (cl-function
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"))))))))
39 :error (cl-function
40 (lambda (&rest args &key error-thrown &allow-other-keys)
41 (error "[org-tag-eldoc] request error %s!" error-thrown)
42 nil))
43 :status-code '((404 . (lambda (&rest _) nil))
44 (500 . (lambda (&rest _) nil)))))
45 ((featurep 'elquery)
46 (request url
47 :type "GET"
48 :parser (lambda () (elquery-read-buffer (current-buffer))) ; convert HTML -> elquery object structure
49 :success (cl-function
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)))))))
56 :error (cl-function
57 (lambda (&rest args &key error-thrown &allow-other-keys)
58 (error "[org-tag-eldoc] request error %s!" error-thrown)
59 nil))
60 :status-code '((404 . (lambda (&rest _) nil))
61 (500 . (lambda (&rest _) nil)))
62 )))))
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)
74 explanation
75 (org-tag-eldoc-pixiv-encyclopedia--request tag)
76 (sit-for 1)
77 (org-tag-eldoc-database-save tag explanation))))
81 (provide 'org-tag-eldoc-pixiv-encyclopedia)
83 ;;; org-tag-eldoc-pixiv-encyclopedia.el ends here