;;; my-semantic-scholar.el -- Semantic Scholar client -*- lexical-binding: t -*- ;; Copyright (C) 2023 Free Software Foundation. ;; Author: Yuchen Pei ;; 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 . ;;; 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