From fbf71a4f6f781df0258553f6d7c1ec878bda4f47 Mon Sep 17 00:00:00 2001 From: stardiviner Date: Wed, 8 May 2024 13:52:20 +0800 Subject: [PATCH] Add tag request backends: Pixiv Encyclopedia, Wikipeida support --- org-tag-eldoc-common.el | 20 ++++++++++ org-tag-eldoc-pixiv-encyclopedia.el | 78 +++++++++++++++++++++++++++++++++++++ org-tag-eldoc-wikipedia.el | 73 ++++++++++++++++++++++++++++++++++ org-tag-eldoc.el | 7 +++- 4 files changed, 177 insertions(+), 1 deletion(-) create mode 100644 org-tag-eldoc-common.el create mode 100644 org-tag-eldoc-pixiv-encyclopedia.el create mode 100644 org-tag-eldoc-wikipedia.el diff --git a/org-tag-eldoc-common.el b/org-tag-eldoc-common.el new file mode 100644 index 0000000..3ccea1d --- /dev/null +++ b/org-tag-eldoc-common.el @@ -0,0 +1,20 @@ +;;; org-tag-eldoc-common.el --- common functions for org-tag-eldoc -*- lexical-binding: t; -*- +;; -*- coding: utf-8 -*- + +;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved. + +;;; Commentary: + + + +;;; Code: + +(defun org-tag-eldoc-common--format-explanation (text) + "Format TEXT for better displaying." + (string-truncate-left text (window-width))) + + + +(provide 'org-tag-eldoc-common) + +;;; org-tag-eldoc-common.el ends here diff --git a/org-tag-eldoc-pixiv-encyclopedia.el b/org-tag-eldoc-pixiv-encyclopedia.el new file mode 100644 index 0000000..50b8e52 --- /dev/null +++ b/org-tag-eldoc-pixiv-encyclopedia.el @@ -0,0 +1,78 @@ +;;; org-tag-eldoc-pixiv-encyclopedia.el --- Web Scraping on Pixiv Encyclopedia -*- lexical-binding: t; -*- +;; -*- coding: utf-8 -*- + +;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved. + +;;; Commentary: + +;; search API: https://dic.pixiv.net/en/search?query=[query] +;; tag URL: https://dic.pixiv.net/en/a/Mating%20Press + +;;; Code: + +(require 'request) +(require 'dom) +(require 'elquery nil t) ; optionally use elquery.el to replace dom.el. +(require 'org-tag-eldoc-common) + + +(defun org-tag-eldoc-pixiv-encyclopedia--request (tag) + "Send HTTP GET request to get TAG explanation data." + (let ((request-backend 'url-retrieve) ; use `url-retrieve' backend for proxy. + (url-proxy-services '(("http" . "127.0.0.1:7890") + ("https" . "127.0.0.1:7890"))) + (url (format "https://dic.pixiv.net/en/a/%s" + (url-hexify-string + (string-replace "_" " " (capitalize tag)))))) + (cond + ((featurep 'dom) + (request url + :type "GET" + :parser (lambda () (libxml-parse-html-region (point-min) (point-max))) ; convert HTML -> Elisp alist structure + :success (cl-function + (lambda (&key data &allow-other-keys) + ;; DEBUG: (setq request-result data) + ;; for `libxml' parser + (setq summary + (org-tag-eldoc-common--format-explanation + (string-clean-whitespace + (dom-text (car (dom-by-class data "summary")))))))) + :error (cl-function + (lambda (&rest args &key error-thrown &allow-other-keys) + (error "[org-tag-eldoc] request error %s!" error-thrown) + nil)) + :status-code '((404 . (lambda (&rest _) nil)) + (500 . (lambda (&rest _) nil))))) + ((featurep 'elquery) + (request url + :type "GET" + :parser (lambda () (elquery-read-buffer (current-buffer))) ; convert HTML -> elquery object structure + :success (cl-function + (lambda (&key data &allow-other-keys) + ;; DEBUG: (setq request-result data) + ;; for `elquery' parser + (setq summary + (org-tag-eldoc-common--format-explanation + (elquery-text (car (elquery-$ ".summary" data))))))) + :error (cl-function + (lambda (&rest args &key error-thrown &allow-other-keys) + (error "[org-tag-eldoc] request error %s!" error-thrown) + nil)) + :status-code '((404 . (lambda (&rest _) nil)) + (500 . (lambda (&rest _) nil))) + ))))) + +;;; TEST: https://dic.pixiv.net/en/a/Mating%20Press +;; (org-tag-eldoc-pixiv-encyclopedia--request "mating_press") + +(defun org-tag-eldoc-pixiv-encyclopedia-query (tag) + "Query TAG on Pixiv Encyclopedia then return a cons cell of tag and explanation." + ;; search: https://dic.pixiv.net/en/search?query= + ;; tag page: https://dic.pixiv.net/en/a/ + (org-tag-eldoc-pixiv-encyclopedia--request tag)) + + + +(provide 'org-tag-eldoc-pixiv-encyclopedia) + +;;; org-tag-eldoc-pixiv-encyclopedia.el ends here diff --git a/org-tag-eldoc-wikipedia.el b/org-tag-eldoc-wikipedia.el new file mode 100644 index 0000000..f6a7a36 --- /dev/null +++ b/org-tag-eldoc-wikipedia.el @@ -0,0 +1,73 @@ +;;; org-tag-eldoc-wikipedia.el --- Web Scraping on Wikipedia -*- lexical-binding: t; -*- +;; -*- coding: utf-8 -*- + +;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved. + +;;; Commentary: + +;; search API: https://wikipedia.org/w/index.php?search=[query] +;; tag URL: https://en.wikipedia.org/wiki/Computer_science + +;;; Code: + +(require 'request) +(require 'dom) +(require 'elquery nil t) ; optionally use elquery.el to replace dom.el. +(require 'org-tag-eldoc-common) + + +(defun org-tag-eldoc-wikipedia--request (tag) + "Send HTTP GET request to get TAG explanation data." + (let ((request-backend 'url-retrieve) ; use `url-retrieve' backend for proxy. + (url-proxy-services '(("http" . "127.0.0.1:7890") + ("https" . "127.0.0.1:7890"))) + (url (format "https://en.wikipedia.org/wiki/%s" (capitalize tag))) + (summary)) + (cond + ((featurep 'dom) + (request url + :type "GET" + :parser (lambda () (libxml-parse-html-region (point-min) (point-max))) ; convert HTML -> Elisp alist structure + :success (cl-function + (lambda (&key data &allow-other-keys) + ;; DEBUG: (setq request-result data) + ;; for `libxml' parser + (setq org-tag-eldoc--explanation + (org-tag-eldoc-common--format-explanation + (dom-texts (dom-by-class data "mw-content-container")))))) + :error (cl-function + (lambda (&rest args &key error-thrown &allow-other-keys) + (error "request error!"))) + :status-code '((404 . (lambda (&rest _) (message (format "Endpoint %s does not exist." url)))) + (500 . (lambda (&rest _) (message (format "Error from %s." url))))))) + ((featurep 'elquery) + (request url + :type "GET" + :parser (lambda () (elquery-read-buffer (current-buffer))) ; convert HTML -> elquery object structure + :success (cl-function + (lambda (&key data &allow-other-keys) + ;; DEBUG: (setq request-result data) + ;; for `elquery' parser + (setq org-tag-eldoc--explanation + (org-tag-eldoc-common--format-explanation + (elquery-text (car (elquery-$ ".mw-content-container" data))))))) + :error (cl-function + (lambda (&rest args &key error-thrown &allow-other-keys) + (error "[org-tag-eldoc] request error!") + "request error" ; return identity string for `org-tag-eldoc-tag-explanation''s `seq-some' -> `pcase'. + )) + :status-code '((404 . (lambda (&rest _) (message (format "Endpoint %s does not exist." url)))) + (500 . (lambda (&rest _) (message (format "Error from %s." url)))))))))) + +;;; TEST: https://en.wikipedia.org/wiki/Computer_science +;; (org-tag-eldoc-wikipedia--request "computer_science") + +(defun org-tag-eldoc-wikipedia-query (tag) + "Query TAG on Wikipedia then return a cons cell of tag and explanation." + (org-tag-eldoc-wikipeida--request tag)) + + + +(provide 'org-tag-eldoc-wikipedia) + +;;; org-tag-eldoc-wikipedia.el ends here diff --git a/org-tag-eldoc.el b/org-tag-eldoc.el index d4f8a70..b437193 100644 --- a/org-tag-eldoc.el +++ b/org-tag-eldoc.el @@ -2,11 +2,13 @@ ;; -*- coding: utf-8 -*- ;; Authors: stardiviner -;; Package-Requires: ((emacs "26.1")) +;; Package-Requires: ((emacs "26.1") (request "0.3.3")) ;; Version: 0.1.0 ;; Keywords: org ;; Homepage: https://repo.or.cz/org-tag-eldoc.git +;; Copyright (C) 2024-2025 Christopher M. Miles, all rights reserved. +;; ;; org-tag-eldoc is free software; you can redistribute it and/or modify it ;; under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 3, or (at your option) @@ -27,6 +29,9 @@ ;;; Code: (require 'org) +(require 'org-tag-eldoc-pixiv-encyclopedia) +(require 'org-tag-eldoc-wikipedia) + (defcustom org-tag-eldoc-tag-explanation-functions '(org-tag-eldoc-pixiv-encyclopedia-query org-tag-eldoc-wikipedia-query) -- 2.11.4.GIT