Add commands to query, insert, update, delete row in database
[org-tag-eldoc.git] / org-tag-eldoc-pixiv-encyclopedia.el
blob58829c93b609a2201d4b07d53275ce8ab131b5c6
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 (url-proxy-services org-tag-eldoc-request-proxy)
25 (url (format "https://dic.pixiv.net/en/a/%s"
26 (url-hexify-string
27 (string-replace "_" " " (capitalize tag))))))
28 (request url
29 :type "GET"
30 :parser (lambda ()
31 (cond
32 ((fboundp 'libxml-parse-html-region) ; convert HTML -> Elisp alist structure
33 (libxml-parse-html-region (point-min) (point-max)))
34 ((featurep 'elquery) ; convert HTML -> elquery object structure
35 (elquery-read-buffer (current-buffer)))))
36 :success (cl-function
37 (lambda (&key data &allow-other-keys)
38 ;; DEBUG: (setq request-result data)
39 (cond
40 ((featurep 'dom)
41 ;; for `libxml' parser
42 (setq org-tag-eldoc--explanation
43 (org-tag-eldoc-common--format-explanation
44 (dom-text (car (dom-by-class data "summary"))))))
45 ((featurep 'elquery)
46 ;; for `elquery' parser
47 (setq org-tag-eldoc--explanation
48 (org-tag-eldoc-common--format-explanation
49 (elquery-text (car (elquery-$ ".summary" data)))))))))
50 :error (cl-function
51 (lambda (&rest args &key error-thrown &allow-other-keys)
52 (error "[org-tag-eldoc] (Pixiv Encyclopedia) request error %s!" error-thrown)
53 nil))
54 :status-code '((404 . (lambda (&rest _) nil))
55 (500 . (lambda (&rest _) nil))))))
57 ;;; TEST: https://dic.pixiv.net/en/a/Mating%20Press
58 ;; (org-tag-eldoc-pixiv-encyclopedia--request "mating_press")
60 (defun org-tag-eldoc-pixiv-encyclopedia-query (tag)
61 "Query TAG on Pixiv Encyclopedia then return a cons cell of tag and explanation."
62 ;; search: https://dic.pixiv.net/en/search?query=<tag>
63 ;; tag page: https://dic.pixiv.net/en/a/<url%20hex%20coded%20tag>
64 (if (stringp org-tag-eldoc--explanation)
65 org-tag-eldoc--explanation
66 (org-tag-eldoc-pixiv-encyclopedia--request tag)
67 (sit-for 1.0)
68 (org-tag-eldoc-database-save tag org-tag-eldoc--explanation)))
72 (provide 'org-tag-eldoc-pixiv-encyclopedia)
74 ;;; org-tag-eldoc-pixiv-encyclopedia.el ends here