diff options
author | Yuchen Pei <hi@ypei.me> | 2023-06-12 19:37:49 +1000 |
---|---|---|
committer | Yuchen Pei <id@ypei.org> | 2023-06-17 16:26:14 +1000 |
commit | a9627518a51f5dc536fa22629a2da680dbc052d1 (patch) | |
tree | aec3610cc0f19c2f9bfc44d80a410bdb66d013f4 /.emacs.d/lisp/my/my-semantic-scholar.el |
first commit
Diffstat (limited to '.emacs.d/lisp/my/my-semantic-scholar.el')
-rw-r--r-- | .emacs.d/lisp/my/my-semantic-scholar.el | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/.emacs.d/lisp/my/my-semantic-scholar.el b/.emacs.d/lisp/my/my-semantic-scholar.el new file mode 100644 index 0000000..4b22390 --- /dev/null +++ b/.emacs.d/lisp/my/my-semantic-scholar.el @@ -0,0 +1,100 @@ +;;; my-semantic-scholar.el -- Semantic Scholar client -*- lexical-binding: t -*- + +;; Copyright (C) 2023 Free Software Foundation. + +;; Author: Yuchen Pei <id@ypei.org> +;; Package-Requires: ((emacs "28.2")) + +;; This file is part of dotfiles. + +;; dotfiles is free software: you can redistribute it and/or modify it under +;; the terms of the GNU Affero General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; dotfiles is distributed in the hope that it will be useful, but WITHOUT +;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General +;; Public License for more details. + +;; You should have received a copy of the GNU Affero General Public +;; License along with dotfiles. If not, see <https://www.gnu.org/licenses/>. + +;;; Commentary: + +;; Semantic Scholar client. + +;;; Code: + +(require 'my-utils) +(require 'my-scihub) + +(defvar my-semantic-scholar-host + "https://api.semanticscholar.org/graph/v1") +(defun my-semantic-scholar-fetch-papers-for-completion (query) + (with-current-buffer + (url-retrieve-synchronously + (format "%s/paper/search?query=%s" my-semantic-scholar-host query)) + (my-skip-http-header) + (mapcar + (lambda (entry) + (concat + (alist-get 'title entry) + (propertize + (concat " " + (alist-get 'paperId entry)) + 'invisible t))) + (alist-get 'data (json-read))))) + +(defun my-semantic-scholar-make-paper-alist (paper-info) + (list (cons "Authors" + (string-join + (mapcar (lambda (entry) (alist-get 'name entry)) + (alist-get 'authors paper-info)) + " and ")) + (cons "Title" (alist-get 'title paper-info)) + (cons "Published" + (number-to-string (alist-get 'year paper-info))) + (cons "Abstract" (my-clean-property-value + (alist-get 'abstract paper-info))) + (cons "Venue" (alist-get 'venue paper-info)) + (cons "arXiv" (alist-get + 'ArXiv (alist-get 'externalIds paper-info))) + (cons "DOI" (alist-get + 'DOI (alist-get 'externalIds paper-info))) + (cons "Semantic-scholar" (alist-get 'paperId paper-info)))) + +(defun my-semantic-scholar-lookup-paper () + "looks up a paper using semantic scholar api, prompts for selection and creates a org entry." + (interactive) + (let* ((query (read-string "Query: ")) + (selected (completing-read + "Select paper:" ;; '("a" "b") + (my-semantic-scholar-fetch-papers-for-completion query)))) + (with-current-buffer + (url-retrieve-synchronously + (format + "%s/paper/%s?fields=title,abstract,authors,venue,year,externalIds" + my-semantic-scholar-host + (progn (string-match "^.* \\(.*\\)$" selected) + (match-string 1 selected)))) + (my-skip-http-header) + (my-org-create-node (my-semantic-scholar-make-paper-alist (json-read))) + (my-org-attach-scihub)))) + +(defun my-semantic-scholar-lookup-doi () + "looks up a paper using semantic scholar api, prompts for selection and creates a org entry." + (interactive) + (let ((doi (read-string "DOI: "))) + (with-current-buffer + (url-retrieve-synchronously + (format + "%s/paper/%s?fields=title,abstract,authors,venue,year,externalIds" + my-semantic-scholar-host + doi)) + (my-skip-http-header) + (my-org-create-node (my-semantic-scholar-make-paper-alist (json-read))) + (my-org-attach-scihub)))) + +(provide 'my-semantic-scholar) +;;; my-semantic-scholar.el ends here |