-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconsult-web-scopus.el
187 lines (164 loc) · 7.92 KB
/
consult-web-scopus.el
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
;;; consult-web-scopus.el --- Consulting Scopus -*- lexical-binding: t -*-
;; Copyright (C) 2024 Armin Darvish
;; Author: Armin Darvish
;; Maintainer: Armin Darvish
;; Created: 2024
;; Version: 0.1
;; Package-Requires: ((emacs "28.1") (consult "1.1"))
;; Homepage: https://github.com/armindarvish/consult-web
;; Keywords: convenience
;;; Commentary:
;;; Code:
(require 'consult-web)
(defun consult-web--scopus-format-candidate (table &optional face &rest args)
"Returns a formatted string for candidates of `consult-web-scopus'.
TABLE is a hashtable from `consult-web--scopus-fetch-results'."
(let* ((pl (consult-web-hashtable-to-plist table))
(title (format "%s" (gethash :title table)))
(source (gethash :source table))
(source (if (stringp source) (propertize source 'face 'consult-web-source-face)))
(query (gethash :query table))
(date (gethash :date table))
(date (if (stringp date) (propertize date 'face 'consult-web-path-face)))
(journal (gethash :journal table))
(journal (if (stringp journal) (propertize journal 'face 'consult-web-domain-face)))
(authors (gethash :authors table))
(authors (cond
((and authors (listp authors))
(concat (first authors) ",..., " (car (last authors))))
((stringp authors)
authors)
))
(authors (if (and authors (stringp authors)) (propertize authors 'face 'consult-web-source-face)))
(doi (gethash :doi table))
(doi (if (stringp doi) (propertize doi 'face 'link)))
(match-str (if (stringp query) (consult--split-escaped (car (consult--command-split query))) nil))
(title-str (consult-web--set-string-width title (floor (* (frame-width) 0.4))))
(title-str (propertize title-str 'face (or 'consult-web-scholar-source-face)))
(extra-args (consult-web-hashtable-to-plist table '(:title :url :search-url :query :source :journal :date :volume :pages :authors :doi :pmid :eid)))
(str (concat title-str
(if journal (format "\t%s" journal))
(if date (format "\s\s%s" date))
(if authors (format "\s\s%s" authors))
(if source (concat "\t" source))
(if extra-args (format "\t%s" extra-args))))
(str (apply #'propertize str pl))
)
(if consult-web-highlight-matches
(cond
((listp match-str)
(mapcar (lambda (match) (setq str (consult-web--highlight-match match str t))) match-str))
((stringp match-str)
(setq str (consult-web--highlight-match match-str str t)))))
str))
(defun consult-web--scopus-callback (cand)
"Callback function for `consult-web-scopus'."
(let* ((doi (get-text-property 0 :doi cand))
(url (if doi (consult-web--doi-to-url doi)
(get-text-property 0 :url cand))))
(funcall consult-web-default-browse-function url)))
(defun consult-web--scopus-preview (cand)
"Preview function for `consult-web-scopus'."
(let* ((doi (get-text-property 0 :doi cand))
(url (if doi (consult-web--doi-to-url doi)
(get-text-property 0 :url cand))))
(funcall consult-web-default-preview-function url)))
(defvar consult-web-scopus-search-url "https://www.scopus.com/record/display.uri?")
(defvar consult-web-scopus-api-url "https://api.elsevier.com/content/search/scopus")
(defcustom consult-web-scopus-api-key nil
"Key for Scopus API.
See URL `https://dev.elsevier.com/documentation/SCOPUSSearchAPI.wadl' for more info"
:group 'consult-web
:type '(choice (const :tag "Scopus API Key" string)
(function :tag "Custom Function")))
(cl-defun consult-web--scopus-fetch-results (input &rest args &key count page &allow-other-keys)
"Retrieve search results from SCOPUS for INPUT.
COUNT is passed as count in query parameters.
(* PAGE COUNT) is passed as start in query paramters.
"
(let* ((count (or (and (integerp count) count)
(and count (string-to-number (format "%s" count)))
consult-web-default-count))
(page (or (and (integerp page) page)
(and page (string-to-number (format "%s" page)))
consult-web-default-count))
(count (min (max count 1) 25))
(page (* count page))
(params `(("query" . ,(replace-regexp-in-string " " "+" input))
("count" . ,(format "%s" count))
("start" . ,(format "%s" page))
("apiKey" . ,(consult-web-expand-variable-function consult-web-scopus-api-key))))
(headers `(("Accept" . "application/json")
)))
(funcall consult-web-retrieve-backend
consult-web-scopus-api-url
:params params
:headers headers
:parser
(lambda ()
(goto-char (point-min))
;; (buffer-substring (point-min) (point-max))
(let* ((content (json-parse-buffer))
(results (gethash "search-results" content))
(items (gethash "entry" results)))
(cl-loop for a across items
collect
(let* ((table (make-hash-table :test 'equal))
(title (gethash "dc:title" a))
(journal (gethash "prism:publicationName" a))
(volume (gethash "prism:volume" a))
(pages (gethash "prism:pageRange" a))
(authors (gethash "dc:creator" a))
(authors (cond
((stringp authors) (list authors))
(t authors)))
(date (gethash "prism:coverDate" a))
(eid (gethash "eid" a))
(doi (gethash "prism:doi" a))
(url (concat consult-web-scopus-search-url "&eid=" eid "&origin=inward"))
(search-url (concat consult-web-scopus-search-url "&eid=" eid "&origin=inward"))
)
(puthash :url url
table)
(puthash :search-url search-url
table)
(puthash :title title
table)
(puthash :journal journal
table)
(puthash :volume volume
table)
(puthash :pages pages
table)
(puthash :date date
table)
(puthash :authors authors
table)
(puthash :doi doi
table)
(puthash :eid eid
table)
(puthash :source "Scopus"
table)
(puthash :query input
table)
table
)))
)
)))
(consult-web-define-source "Scopus"
:narrow-char ?s
:face 'consult-web-scholar-source-face
:request #'consult-web--scopus-fetch-results
:format #'consult-web--scopus-format-candidate
:preview-key consult-web-preview-key
:on-preview #'consult-web--scopus-preview
:on-return #'identity
:on-callback #'consult-web--scopus-callback
:search-history 'consult-web--search-history
:selection-history 'consult-web--selection-history
:dynamic 'both)
;;; provide `consult-web-scopus' module
(provide 'consult-web-scopus)
(add-to-list 'consult-web-sources-modules-to-load 'consult-web-scopus)
;;; consult-web-scopus.el ends here