aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-semantic-scholar.el
blob: 4b22390cfbd9485d6f126c98ad0a5bb0ee5d6397 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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