This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
forked from emacsmirror/pubmed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pubmed-pmc.el
189 lines (158 loc) · 5.65 KB
/
pubmed-pmc.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
188
189
;;; pubmed-pmc.el --- Fetch fulltext PDFs from PubMed Central (PMC) -*- lexical-binding: t; -*-
;; Author: Folkert van der Beek <[email protected]>
;; This file is NOT part of GNU Emacs.
;; This program 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 of the License, or
;; (at your option) any later version.
;; This program 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License along with
;; this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Download fulltext PDFs of Open Access articles using PubMed Central® (PMC)
;; <https://www.ncbi.nlm.nih.gov/pmc/>. PubMed Central® (PMC) is a free
;; full-text archive of biomedical and life sciences journal literature at the
;; U.S. National Institutes of Health's National Library of Medicine (NIH/NLM).
;;; Code:
;;;; Requirements
(require 'deferred)
(require 'eww)
(require 'esxml)
(require 'esxml-query)
(require 'url)
;;;; Variables
(defvar pubmed-pmc-url "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi"
"PMC base URL.")
;;;; Customization
(defgroup pubmed-pmc nil
"Fetch fulltext PDFs from PubMed Central (PMC)."
:group 'pubmed)
(defcustom pubmed-pmc-timeout 5000
"PMC timeout in milliseconds."
:group 'pubmed-pmc
:type 'integer)
;;;; Commands
;;;###autoload
(defun pubmed-get-pmc (&optional entries)
"In PubMed, try to fetch the fulltext PDF of the marked entries, the current entry or the optional argument ENTRIES."
;; TODO: optional argument NOQUERY non-nil means do not ask the user to confirm.
(interactive "P")
(pubmed--guard)
(let (mark
mark-list
pubmed-uid)
(save-excursion
(goto-char (point-min))
(while (not (eobp))
(setq mark (char-after))
(setq pubmed-uid (tabulated-list-get-id))
(when (eq mark ?*)
(push pubmed-uid mark-list))
(forward-line)))
(cond
(entries
(mapcar #'pubmed--get-pmc entries))
(mark-list
(mapcar #'pubmed--get-pmc mark-list))
((tabulated-list-get-id)
(pubmed--get-pmc (tabulated-list-get-id)))
(t
(error "No entry selected")))))
;;;; Functions
(defun pubmed--get-pmc (uid)
"Try to fetch the fulltext PDF of UID, using PMC."
(deferred:$
(deferred:call #'pubmed-pmc uid)
(deferred:nextc it
(lambda (result)
(when (bufferp result)
(pubmed--view-pdf result))))))
(defun pubmed-pmc (uid)
"Deferred chain to retrieve the fulltext PDF of the UID."
(deferred:$
;; try
(deferred:$
(deferred:next
(lambda ()
(let ((url-request-method "GET")
(arguments (concat "?"
"dbfrom=pubmed"
"&cmd=prlinks"
"&retmode=ref"
"&id=" uid
(when (not (string-empty-p pubmed-api-key))
(concat "&api_key=" pubmed-api-key)))))
(concat pubmed-pmc-url arguments))))
(deferred:nextc it
(lambda (url)
(if url
(lexical-let ((d (deferred:new #'identity)))
(url-retrieve url (lambda (status)
;; Start the following callback queue now.
(deferred:callback-post d status)))
;; Return the unregistered (not yet started) callback
;; queue, so that the following queue will wait until it
;; is started.
d)
(deferred:cancel it))))
;; You can connect deferred callback queues
(deferred:nextc it
(lambda (status)
(let ((url-error (plist-get status :error))
(url-redirect (plist-get status :redirect)))
(cond
(url-error
(signal (car url-error) (cdr url-error)))
(url-redirect
(progn
(message "Redirected-to: %s" url-redirect)
url-redirect))
(t
(error "PMC found no fulltext article"))))))
(deferred:nextc it
(lambda (url)
(deferred:timeout pubmed-pmc-timeout (error "Timeout")
(deferred:url-retrieve url))))
(deferred:nextc it
(lambda (buffer)
"Parse the HTML in BUFFER. Return the url of the iframe or nil if none is found."
;; Extract the url of the pdf. The url is marked up as follows:
;; <meta name="citation_pdf_url" content="URL_FOR_PDF"/>
;; TODO: Some journals require a captcha, e.g. Indian Journal of Psychological Medicine.
(let* ((dom (with-current-buffer buffer (libxml-parse-html-region (point-min) (point-max))))
(url (esxml-node-attribute 'content (esxml-query "meta[name=citation_pdf_url]" dom))))
(if url
url
(error "PMC found no fulltext article")))))
(deferred:nextc it
(lambda (url)
(deferred:timeout pubmed-pmc-timeout (error "Timeout")
(deferred:url-retrieve url))))
(deferred:nextc it
(lambda (buffer)
"Parse the HTML object in BUFFER and show the PDF."
(let* ((headers (with-current-buffer buffer (eww-parse-headers)))
(content-type (cdr (assoc "content-type" headers))))
(cond
;; Return buffer if the iframe contains a pdf file
((equal content-type "application/pdf")
buffer)
(t
(error "Unknown content-type: %s" content-type)))))))
;; catch
(deferred:error it
(lambda (deferred-error)
"Catch any errors that occur during the deferred chain and return nil."
(message "%s" (cadr deferred-error))
nil))
;; finally
(deferred:nextc it
(lambda (result)
"Return non-nil if a fulltext article is found, otherwise nil."
result))))
;;;; Footer
(provide 'pubmed-pmc)
;;; pubmed-pmc.el ends here