forked from jkitchin/org-ref
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doi-utils.el
1506 lines (1273 loc) · 60 KB
/
doi-utils.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; doi-utils.el --- DOI utilities for making bibtex entries
;; Copyright (C) 2015 John Kitchin
;; Author: John Kitchin <[email protected]>
;; Keywords: convenience
;; Version: 0.1
;; Package-Requires: ((org-ref))
;; 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:
;; This package provides functionality to download PDFs and bibtex entries from
;; a DOI, as well as to update a bibtex entry from a DOI. It depends slightly
;; on org-ref, to determine where to save pdf files too, and where to insert
;; bibtex entries in the default bibliography.
;; The principle commands you will use from here are:
;; - doi-utils-get-bibtex-entry-pdf with the cursor in a bibtex entry.
;; - doi-utils-insert-bibtex-entry-from-doi to insert a bibtex entry at your cursor, clean it and try to get a pdf.
;; - doi-utils-add-bibtex-entry-from-doi to add an entry to your default bibliography (cleaned with pdf if possible).
;; - doi-utils-update-bibtex-entry-from-doi with cursor in an entry to update its fields.
;;; Code:
(defvar org-ref-pdf-directory)
(defvar org-ref-bibliography-notes)
(defvar org-ref-default-bibliography)
(defvar reftex-default-bibliography)
(defvar url-http-end-of-headers)
(declare-function org-ref-bib-citation "org-ref-core")
(declare-function org-ref-find-bibliography "org-ref-core")
(declare-function org-ref-clean-bibtex-entry "org-ref-core")
(declare-function reftex-get-bib-field "reftex-cite")
(declare-function bibtex-completion-edit-notes "bibtex-completion")
(declare-function helm "helm")
(eval-when-compile
(require 'cl-lib))
(require 'bibtex)
(require 'dash)
(require 'json)
(require 'org) ; org-add-link-type
(require 'org-bibtex) ; org-bibtex-yank
(require 'url-http)
(require 'org-ref-utils)
;;* Customization
(defgroup doi-utils nil
"Customization group for doi-utils."
:tag "DOI utils"
:group 'doi-utils)
(defcustom doi-utils-download-pdf
t
"Try to download PDFs when adding bibtex entries when non-nil."
:type 'boolean
:group 'doi-utils)
(defcustom doi-utils-open-pdf-after-download
nil
"Open PDF after adding bibtex entries."
:type 'boolean
:group 'doi-utils)
(defcustom doi-utils-make-notes
t
"Whether to create notes when adding bibtex entries."
:type 'boolean
:group 'doi-utils)
(defcustom doi-utils-timestamp-field
"DATE_ADDED"
"The bibtex field to store the date when an entry has been added."
:type 'string
:group 'doi-utils)
(defcustom doi-utils-timestamp-format-function
'current-time-string
"The function to format the timestamp for a bibtex entry.
Set to a function that returns nil to avoid setting timestamps in the entries.
e.g. (lambda () nil)"
:type 'function
:group 'doi-utils)
(defcustom doi-utils-make-notes-function
(lambda ()
(bibtex-beginning-of-entry)
(bibtex-completion-edit-notes (list (cdr (assoc "=key=" (bibtex-parse-entry))))))
"Function to create notes for a bibtex entry.
Set `doi-utils-make-notes' to nil if you want no notes."
:type 'function
:group 'doi-utils)
(defcustom doi-utils-dx-doi-org-url
"https://doi.org/"
"Base url to retrieve doi metadata from. A trailing / is required."
:type 'string
:group 'doi-utils)
;;* Getting pdf files from a DOI
;; The idea here is simple. When you visit http://dx.doi.org/doi or
;; https://doi.org/doi, you get redirected to the journal site. Once you have
;; the url for the article, you can usually compute the url to the pdf, or find
;; it in the page. Then you simply download it.
;; There are some subtleties in doing this that are described here. To get the
;; redirect, we have to use url-retrieve, and a callback function. The callback
;; does not return anything, so we communicate through global variables.
;; url-retrieve is asynchronous, so we have to make sure to wait for it to
;; finish.
(defvar *doi-utils-waiting* t
"Stores waiting state for url retrieval.")
(defvar *doi-utils-redirect* nil
"Stores redirect url from a callback function.")
(defun doi-utils-redirect-callback (&optional status)
"Callback for `url-retrieve' to set the redirect.
Optional argument STATUS Unknown why this is optional."
(when (plist-get status :error)
(signal (car (plist-get status :error)) (cdr(plist-get status :error))))
(when (plist-get status :redirect) ; is nil if there none
(setq *doi-utils-redirect* (plist-get status :redirect)))
;; we have done our job, so we are not waiting any more.
(setq *doi-utils-waiting* nil))
;; To actually get the redirect we use url-retrieve like this.
(defun doi-utils-get-redirect (doi)
"Get redirect url from `doi-utils-dx-doi-org-url'/doi."
;; we are going to wait until the url-retrieve is done
(setq *doi-utils-waiting* t)
;; start with no redirect. it will be set in the callback.
(setq *doi-utils-redirect* nil)
(url-retrieve
(format "%s%s" doi-utils-dx-doi-org-url doi)
'doi-utils-redirect-callback)
;; I suspect we need to wait here for the asynchronous process to
;; finish. we loop and sleep until the callback says it is done via
;; `*doi-utils-waiting*'. this works as far as i can tell. Before I
;; had to run this a few times to get it to work, which i suspect
;; just gave the first one enough time to finish.
(while *doi-utils-waiting* (sleep-for 0.1)))
;; Once we have a redirect for a particular doi, we need to compute the url to
;; the pdf. We do this with a series of functions. Each function takes a single
;; argument, the redirect url. If it knows how to compute the pdf url it does,
;; and returns it. We store the functions in a variable:
(defvar doi-utils-pdf-url-functions nil
"Functions that return a url to a pdf from a redirect url.
Each function takes one argument, the redirect url. The function
must return a pdf-url, or nil.")
;;** APS journals
(defun aps-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://journals.aps.org" *doi-utils-redirect*)
(replace-regexp-in-string "/abstract/" "/pdf/" *doi-utils-redirect*)))
;;** Science
(defun science-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://www.sciencemag.org" *doi-utils-redirect*)
(concat *doi-utils-redirect* ".full.pdf")))
;;** Nature
(defun nature-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://www.nature.com" *doi-utils-redirect*)
(let ((result *doi-utils-redirect*))
(setq result (replace-regexp-in-string "/full/" "/pdf/" result))
(replace-regexp-in-string "\.html$" "\.pdf" result))))
;;** Elsevier/ScienceDirect
;; You cannot compute these pdf links; they are embedded in the redirected pages.
(defvar *doi-utils-pdf-url* nil
"Stores url to pdf download from a callback function.")
;;** Wiley
;; http://onlinelibrary.wiley.com/doi/10.1002/anie.201402680/abstract
;; http://onlinelibrary.wiley.com/doi/10.1002/anie.201402680/pdf
;; It appears that it is not enough to use the pdf url above. That takes you to
;; an html page. The actual link to teh pdf is embedded in that page. This is
;; how ScienceDirect does things too.
;; This is where the link is hidden:
;; <iframe id="pdfDocument" src="http://onlinelibrary.wiley.com/store/10.1002/anie.201402680/asset/6397_ftp.pdf?v=1&t=hwut2142&s=d4bb3cd4ad20eb733836717f42346ffb34017831" width="100%" height="675px"></iframe>
(defun doi-utils-get-wiley-pdf-url (redirect-url)
"Wileyscience direct hides the pdf url in html.
We get it out here by parsing the html.
Argument REDIRECT-URL URL you are redirected to."
(setq *doi-utils-waiting* t)
(url-retrieve
redirect-url
(lambda (status)
(goto-char (point-min))
(re-search-forward "<iframe id=\"pdfDocument\" src=\"\\([^\"]*\\)\"" nil t)
(setq *doi-utils-pdf-url* (match-string 1)
*doi-utils-waiting* nil)))
(while *doi-utils-waiting* (sleep-for 0.1))
*doi-utils-pdf-url*)
(defun wiley-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://onlinelibrary.wiley.com" *doi-utils-redirect*)
(doi-utils-get-wiley-pdf-url
(replace-regexp-in-string "/abstract" "/pdf" *doi-utils-redirect*))
*doi-utils-pdf-url*))
;;** Springer
(defun springer-chapter-pdf-url (*doi-utils-redirect*)
(when (string-match "^http://link.springer.com/chapter/" *doi-utils-redirect*)
(replace-regexp-in-string "/chapter" "/content/pdf"
(concat *doi-utils-redirect* ".pdf"))))
(defun springer-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://link.springer.com" *doi-utils-redirect*)
(replace-regexp-in-string "/article/" "/content/pdf/"
(concat *doi-utils-redirect* ".pdf"))))
;;** ACS
;; here is a typical url http://pubs.acs.org/doi/abs/10.1021/nl500037x
;; the pdf is found at http://pubs.acs.org/doi/pdf/10.1021/nl500037x
;; we just change /abs/ to /pdf/.
(defun acs-pdf-url-1 (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://pubs.acs.org/doi/abs/" *doi-utils-redirect*)
(replace-regexp-in-string "/abs/" "/pdf/" *doi-utils-redirect*)))
;; 1/20/2016 I noticed this new pattern in pdf urls, where there is no abs in
;; the url
(defun acs-pdf-url-2 (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://pubs.acs.org/doi/" *doi-utils-redirect*)
(replace-regexp-in-string "/doi/" "/doi/pdf/" *doi-utils-redirect*)))
;; 1/18/2019: It looks like they are using https now
(defun acs-pdf-url-3 (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^https://pubs.acs.org/doi/" *doi-utils-redirect*)
(replace-regexp-in-string "/doi/" "/doi/pdf/" *doi-utils-redirect*)))
;;** IOP
(defun iop-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://iopscience.iop.org" *doi-utils-redirect*)
(replace-regexp-in-string "/meta" "/pdf" *doi-utils-redirect*)))
;;** JSTOR
(defun jstor-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://www.jstor.org" *doi-utils-redirect*)
(concat (replace-regexp-in-string "/stable/" "/stable/pdfplus/" *doi-utils-redirect*) ".pdf")))
;;** AIP
(defun aip-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://scitation.aip.org" *doi-utils-redirect*)
;; get stuff after content
(let (p1 p2 s p3)
(setq p2 (replace-regexp-in-string
"^http://scitation.aip.org/" "" *doi-utils-redirect*))
(setq s (split-string p2 "/"))
(setq p1 (mapconcat 'identity (-remove-at-indices '(0 6) s) "/"))
(setq p3 (concat "/" (nth 0 s) (nth 1 s) "/" (nth 2 s) "/" (nth 3 s)))
(format "http://scitation.aip.org/deliver/fulltext/%s.pdf?itemId=/%s&mimeType=pdf&containerItemId=%s"
p1 p2 p3))))
;;** Taylor and Francis
(defun tandfonline-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://www.tandfonline.com" *doi-utils-redirect*)
(replace-regexp-in-string "/abs/\\|/full/" "/pdf/" *doi-utils-redirect*)))
;;** ECS
(defun ecs-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://jes.ecsdl.org" *doi-utils-redirect*)
(replace-regexp-in-string "\.abstract$" ".full.pdf" *doi-utils-redirect*)))
;; http://ecst.ecsdl.org/content/25/2/2769
;; http://ecst.ecsdl.org/content/25/2/2769.full.pdf
(defun ecst-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://ecst.ecsdl.org" *doi-utils-redirect*)
(concat *doi-utils-redirect* ".full.pdf")))
;;** RSC
(defun rsc-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://pubs.rsc.org" *doi-utils-redirect*)
(let ((url (downcase *doi-utils-redirect*)))
(setq url (replace-regexp-in-string "articlelanding" "articlepdf" url))
url)))
;;** Science Direct
(defun doi-utils-get-science-direct-pdf-url (redirect-url)
"Science direct hides the pdf url in html. We get it out here.
REDIRECT-URL is where the pdf url will be in."
(setq *doi-utils-waiting* t)
(url-retrieve
redirect-url
(lambda (status)
(goto-char (point-min))
(re-search-forward "pdf_url\" content=\"\\([^\"]*\\)\"" nil t) ; modified the search string to reflect updated science direct
(setq *doi-utils-pdf-url* (match-string 1)
*doi-utils-waiting* nil)))
(while *doi-utils-waiting* (sleep-for 0.1))
*doi-utils-pdf-url*)
(defun science-direct-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://www.sciencedirect.com" *doi-utils-redirect*)
(doi-utils-get-science-direct-pdf-url *doi-utils-redirect*)
*doi-utils-pdf-url*))
;; sometimes I get
;; http://linkinghub.elsevier.com/retrieve/pii/S0927025609004558
;; which actually redirect to
;; http://www.sciencedirect.com/science/article/pii/S0927025609004558
(defun linkinghub-elsevier-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match
"^https://linkinghub.elsevier.com/retrieve" *doi-utils-redirect*)
(doi-utils-get-science-direct-pdf-url
(replace-regexp-in-string
;; change URL to science direct and use function to get pdf URL
"https://linkinghub.elsevier.com/retrieve"
"https://www.sciencedirect.com/science/article"
*doi-utils-redirect*))
*doi-utils-pdf-url*))
;;** PNAS
;; http://www.pnas.org/content/early/2014/05/08/1319030111
;; http://www.pnas.org/content/early/2014/05/08/1319030111.full.pdf
;; with supporting info
;; http://www.pnas.org/content/early/2014/05/08/1319030111.full.pdf+html?with-ds=yes
(defun pnas-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://www.pnas.org" *doi-utils-redirect*)
(concat *doi-utils-redirect* ".full.pdf?with-ds=yes")))
;;** Copernicus Publications
(defvar copernicus-journal-urls '(
"^https://www.adv-geosci.net/"
"^https://www.adv-radio-sci.net/"
"^https://www.adv-sci-res.net/"
"^https://www.adv-stat-clim-meteorol-oceanogr.net/"
"^https://www.ann-geophys.net/"
"^https://www.arch-anim-breed.net/"
"^https://www.astra-proc.net/"
"^https://www.atmos-chem-phys.net/"
"^https://www.atmos-chem-phys-discuss.net/"
"^https://www.atmos-meas-tech.net/"
"^https://www.atmos-meas-tech-discuss.net/"
"^https://www.biogeosciences.net/"
"^https://www.biogeosciences-discuss.net/"
"^https://www.clim-past.net/recent_papers.html"
"^https://www.clim-past-discuss.net/"
"^https://www.drink-water-eng-sci.net/"
"^https://www.drink-water-eng-sci-discuss.net/"
"^https://www.eg-quaternary-sci-j.net/"
"^https://www.earth-surf-dynam.net/"
"^https://www.earth-surf-dynam-discuss.net/"
"^https://www.earth-syst-dynam.net/"
"^https://www.earth-syst-dynam-discuss.net/"
"^https://www.earth-syst-sci-data.net/"
"^https://www.earth-syst-sci-data-discuss.net/"
"^https://www.foss-rec.net/"
"^https://www.geogr-helv.net/"
"^https://www.geosci-instrum-method-data-syst.net/"
"^https://www.geosci-instrum-method-data-syst-discuss.net/"
"^https://www.geosci-model-dev.net/"
"^https://www.geosci-model-dev-discuss.net/"
"^https://www.hist-geo-space-sci.net/"
"^https://www.hydrol-earth-syst-sci.net/"
"^https://www.hydrol-earth-syst-sci-discuss.net/"
"^https://www.j-sens-sens-syst.net/"
"^https://www.mech-sci.net/"
"^https://www.nat-hazards-earth-syst-sci.net/"
"^https://www.nonlin-processes-geophys-discuss.net/"
"^https://www.ocean-sci.net/"
"^https://www.ocean-sci-discuss.net/"
"^https://www.primate-biol.net/"
"^https://www.proc-iahs.net/"
"^https://www.sci-dril.net/"
"^https://www.soil-journal.net/"
"^https://www.soil-discuss.net/"
"^https://www.solid-earth.net/"
"^https://www.solid-earth-discuss.net/"
"^https://www.stephan-mueller-spec-publ-ser.net/"
"^https://www.the-cryosphere.net/"
"^https://www.the-cryosphere-discuss.net/"
"^https://www.web-ecol.net/"
"^https://www.wind-energ-sci.net/"
"^https://www.wind-energ-sci-discuss.net/"
)
"List of Copernicus URLs.")
(defun doi-utils-get-copernicus-pdf-url (redirect-url)
"Copernicus hides the pdf url in html. We get it out here.
REDIRECT-URL is where the pdf url will be in."
(setq *doi-utils-waiting* t)
(url-retrieve
redirect-url
(lambda (status)
(goto-char (point-min))
(re-search-forward "citation_pdf_url\" content=\"\\([^\"]*\\)\"" nil t)
(setq *doi-utils-pdf-url* (match-string 1)
*doi-utils-waiting* nil)))
(while *doi-utils-waiting* (sleep-for 0.1))
*doi-utils-pdf-url*)
(defun copernicus-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(car (cl-loop for copurl in copernicus-journal-urls
when (string-match copurl *doi-utils-redirect*)
collect
(progn (doi-utils-get-copernicus-pdf-url *doi-utils-redirect*)
*doi-utils-pdf-url*))))
;;** Sage
(defun sage-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://pss.sagepub.com" *doi-utils-redirect*)
(concat *doi-utils-redirect* ".full.pdf")))
;;** Journal of Neuroscience
(defun jneurosci-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://www.jneurosci.org" *doi-utils-redirect*)
(concat *doi-utils-redirect* ".full.pdf")))
;;** Generic .full.pdf
(defun generic-full-pdf-url (*doi-utils-redirect*)
(let ((pdf (concat *doi-utils-redirect* ".full.pdf")))
(when (url-http-file-exists-p pdf)
pdf)))
;;** IEEE
;; 10.1109/re.2014.6912247
;; http(s)://ieeexplore.ieee.org/xpl/articleDetails.jsp?arnumber=6912247
;; http(s)://ieeexplore.ieee.org/ielx7/6903646/6912234/06912247.pdf
;; http(s)://ieeexplore.ieee.org/iel7/6903646/6912234/06912247.pdf?arnumber=6912247
;; <meta name="citation_pdf_url" content="http(s)://ieeexplore.ieee.org/iel7/6903646/6912234/06912247.pdf?arnumber=6912247">
;; <frame src="http(s)://ieeexplore.ieee.org/ielx7/6903646/6912234/06912247.pdf?tp=&arnumber=6912247&isnumber=6912234" frameborder=0 />
(defun ieee-pdf-url (*doi-utils-redirect*)
"Get a url to the pdf from *DOI-UTILS-REDIRECT* for IEEE urls."
(when (string-match "^https?://ieeexplore.ieee.org" *doi-utils-redirect*)
(with-current-buffer (url-retrieve-synchronously *doi-utils-redirect*)
(goto-char (point-min))
(when (re-search-forward "<meta name=\"citation_pdf_url\" content=\"\\([[:ascii:]]*?\\)\">" nil t)
(let ((framed-url (match-string 1)))
(with-current-buffer (url-retrieve-synchronously framed-url)
(goto-char (point-min))
(when (re-search-forward "<frame src=\"\\(http[[:ascii:]]*?\\)\"" nil t)
(match-string 1))))))))
;; At least some IEEE papers need the following new pdf-link parsing
;; Example: 10.1109/35.667413
(defun ieee2-pdf-url (*doi-utils-redirect*)
"Get a url to the pdf from *DOI-UTILS-REDIRECT* for IEEE urls."
(when (string-match "^https?://ieeexplore.ieee.org" *doi-utils-redirect*)
(with-current-buffer (url-retrieve-synchronously *doi-utils-redirect*)
(goto-char (point-min))
(when (re-search-forward "\"pdfUrl\":\"\\([[:ascii:]]*?\\)\"" nil t)
(let ((framed-url (match-string 1)))
(with-current-buffer (url-retrieve-synchronously (concat "http://ieeexplore.ieee.org" framed-url))
(goto-char (point-min))
(when (re-search-forward "<frame src=\"\\(http[[:ascii:]]*?\\)\"" nil t)
(match-string 1))))))))
;; Another try to get the ieee pdf
;; <iframe src="http(s)://ieeexplore.ieee.org/ielx5/8/4538127/04538164.pdf?tp=&arnumber=4538164&isnumber=4538127" frameborder=0>
(defun ieee3-pdf-url (*doi-utils-redirect*)
"Get a url to the pdf from *DOI-UTILS-REDIRECT* for IEEE urls."
(when (string-match "^https?://ieeexplore.ieee.org" *doi-utils-redirect*)
(with-current-buffer (url-retrieve-synchronously *doi-utils-redirect*)
(goto-char (point-min))
(when (re-search-forward "\"pdfUrl\":\"\\([[:ascii:]]*?\\)\"" nil t)
(let ((framed-url (match-string 1)))
(with-current-buffer (url-retrieve-synchronously (concat "http://ieeexplore.ieee.org" framed-url))
(goto-char (point-min))
(when (re-search-forward "<iframe src=\"\\(http[[:ascii:]]*?\\)\"" nil t)
(match-string 1))))))))
;; ACM Digital Library
;; http(s)://dl.acm.org/citation.cfm?doid=1368088.1368132
;; <a name="FullTextPDF" title="FullText PDF" href="ft_gateway.cfm?id=1368132&ftid=518423&dwn=1&CFID=766519780&CFTOKEN=49739320" target="_blank">
(defun acm-pdf-url (*doi-utils-redirect*)
"Get a url to the pdf from *DOI-UTILS-REDIRECT* for ACM urls."
(when (string-match "^https?://dl.acm.org" *doi-utils-redirect*)
(with-current-buffer (url-retrieve-synchronously *doi-utils-redirect*)
(goto-char (point-min))
(when (re-search-forward "<a name=\"FullTextPDF\".*href=\"\\([[:ascii:]]*?\\)\"" nil t)
(concat "http://dl.acm.org/" (match-string 1))))))
;;** Optical Society of America (OSA)
(defun osa-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^https://www.osapublishing.org" *doi-utils-redirect*)
(replace-regexp-in-string "abstract.cfm" "viewmedia.cfm" *doi-utils-redirect* )))
;;** ASME Biomechanical Journal
(defun asme-biomechanical-pdf-url (*doi-utils-redirect*)
"Typical URL: http://biomechanical.asmedigitalcollection.asme.org/article.aspx?articleid=1427237
On this page the pdf might be here: <meta name=\"citation_author\" content=\"Dalong Li\" /><meta name=\"citation_author_email\" content=\"[email protected]\" /><meta name=\"citation_author\" content=\"Anne M. Robertson\" /><meta name=\"citation_author_email\" content=\"[email protected]\" /><meta name=\"citation_title\" content=\"A Structural Multi-Mechanism Damage Model for Cerebral Arterial Tissue\" /><meta name=\"citation_firstpage\" content=\"101013\" /><meta name=\"citation_doi\" content=\"10.1115/1.3202559\" /><meta name=\"citation_keyword\" content=\"Mechanisms\" /><meta name=\"citation_keyword\" content=\"Biological tissues\" /><meta name=\"citation_keyword\" content=\"Stress\" /><meta name=\"citation_keyword\" content=\"Fibers\" /><meta name=\"citation_journal_title\" content=\"Journal of Biomechanical Engineering\" /><meta name=\"citation_journal_abbrev\" content=\"J Biomech Eng\" /><meta name=\"citation_volume\" content=\"131\" /><meta name=\"citation_issue\" content=\"10\" /><meta name=\"citation_publication_date\" content=\"2009/10/01\" /><meta name=\"citation_issn\" content=\"0148-0731\" /><meta name=\"citation_publisher\" content=\"American Society of Mechanical Engineers\" /><meta name=\"citation_pdf_url\" content=\"http://biomechanical.asmedigitalcollection.asme.org/data/journals/jbendy/27048/101013_1.pdf\" />
It is in the citation_pdf_url.
It would be better to parse this, but here I just use a regexp.
"
(when (string-match "^http://biomechanical.asmedigitalcollection.asme.org" *doi-utils-redirect*)
(setq *doi-utils-waiting* 0)
(url-retrieve
*doi-utils-redirect*
(lambda (status)
(goto-char (point-min))
(re-search-forward "citation_pdf_url\" content=\"\\(.*\\)\"" nil t)
(message-box (match-string 1))
(setq *doi-utils-pdf-url* (match-string 1)
*doi-utils-waiting* nil)))
(while (and *doi-utils-waiting* (< *doi-utils-waiting* 5))
(setq *doi-utils-waiting* (+ *doi-utils-waiting* 0.1))
(sleep-for 0.1))
*doi-utils-pdf-url*))
;; Society for Industrial and Applied Mathematics (SIAM)
(defun siam-pdf-url (*doi-utils-redirect*)
"Get url to the pdf from *DOI-UTILS-REDIRECT*."
(when (string-match "^http://epubs.siam.org" *doi-utils-redirect*)
(replace-regexp-in-string "/doi/" "/doi/pdf/" *doi-utils-redirect* )))
;;** Add all functions
(setq doi-utils-pdf-url-functions
(list
'aps-pdf-url
'science-pdf-url
'nature-pdf-url
'wiley-pdf-url
'springer-chapter-pdf-url
'springer-pdf-url
'acs-pdf-url-1
'acs-pdf-url-2
'acs-pdf-url-3
'iop-pdf-url
'jstor-pdf-url
'aip-pdf-url
'science-direct-pdf-url
'linkinghub-elsevier-pdf-url
'tandfonline-pdf-url
'ecs-pdf-url
'ecst-pdf-url
'rsc-pdf-url
'pnas-pdf-url
'copernicus-pdf-url
'sage-pdf-url
'jneurosci-pdf-url
'ieee-pdf-url
'ieee2-pdf-url
'ieee3-pdf-url
'acm-pdf-url
'osa-pdf-url
'asme-biomechanical-pdf-url
'siam-pdf-url
'generic-full-pdf-url))
;;** Get the pdf url for a doi
(defun doi-utils-get-pdf-url (doi)
"Return a url to a pdf for the DOI if one can be calculated.
Loops through the functions in `doi-utils-pdf-url-functions'
until one is found."
(doi-utils-get-redirect doi)
(unless *doi-utils-redirect*
(error "No redirect found for %s" doi))
(catch 'pdf-url
(dolist (func doi-utils-pdf-url-functions)
(let ((this-pdf-url (funcall func *doi-utils-redirect*)))
(when this-pdf-url
(throw 'pdf-url this-pdf-url))))))
;;** Finally, download the pdf
;;;###autoload
(defun doi-utils-get-bibtex-entry-pdf (&optional arg)
"Download pdf for entry at point if the pdf does not already exist locally.
The entry must have a doi. The pdf will be saved
to `org-ref-pdf-directory', by the name %s.pdf where %s is the
bibtex label. Files will not be overwritten. The pdf will be
checked to make sure it is a pdf, and not some html failure
page. You must have permission to access the pdf. We open the pdf
at the end if `doi-utils-open-pdf-after-download' is non-nil.
With one prefix ARG, directly get the pdf from a file (through
`read-file-name') instead of looking up a DOI. With a double
prefix ARG, directly get the pdf from an open buffer (through
`read-buffer-to-switch') instead. These two alternative methods
work even if the entry has no DOI, and the pdf file is not
checked."
(interactive "P")
(save-excursion
(bibtex-beginning-of-entry)
(let ( ;; get doi, removing http://dx.doi.org/ if it is there.
(doi (replace-regexp-in-string
"https?://\\(dx.\\)?.doi.org/" ""
(bibtex-autokey-get-field "doi")))
(key (cdr (assoc "=key=" (bibtex-parse-entry))))
(pdf-url)
(pdf-file))
(setq pdf-file (concat
(if org-ref-pdf-directory
(file-name-as-directory org-ref-pdf-directory)
(read-directory-name "PDF directory: " "."))
key ".pdf"))
;; now get file if needed.
(unless (file-exists-p pdf-file)
(cond
((and (not arg)
doi
(setq pdf-url (doi-utils-get-pdf-url doi)))
(url-copy-file pdf-url pdf-file)
;; now check if we got a pdf
(if (org-ref-pdf-p pdf-file)
(message "%s saved" pdf-file)
(delete-file pdf-file)
(message "No pdf was downloaded.")
(browse-url pdf-url)))
((equal arg '(4))
(copy-file (expand-file-name (read-file-name "Pdf file: " nil nil t))
pdf-file))
((equal arg '(16))
(with-current-buffer (read-buffer-to-switch "Pdf buffer: ")
(write-file pdf-file)))
(t
(message "We don't have a recipe for this journal.")))
(when (and doi-utils-open-pdf-after-download (file-exists-p pdf-file))
(org-open-file pdf-file))))))
;;* Getting bibtex entries from a DOI
;; I
;; [[http://homepages.see.leeds.ac.uk/~eeaol/notes/2013/02/doi-metadata/][found]]
;; you can download metadata about a DOI from http://dx.doi.org. You just have
;; to construct the right http request to get it. Here is a function that gets
;; the metadata as a plist in emacs.
(defun doi-utils-get-json-metadata (doi)
"Try to get json metadata for DOI. Open the DOI in a browser if we do not get it."
(let ((url-request-method "GET")
(url-mime-accept-string "application/citeproc+json")
(json-object-type 'plist)
(json-data))
(with-current-buffer
(url-retrieve-synchronously
(concat "http://dx.doi.org/" doi))
(setq json-data (buffer-substring url-http-end-of-headers (point-max)))
(if (or (string-match "<title>Error: DOI Not Found</title>" json-data)
(string-match "Resource not found" json-data)
(string-match "Status *406" json-data))
(progn
(browse-url (concat doi-utils-dx-doi-org-url doi))
(error "Resource not found. Opening website"))
(json-read-from-string json-data)))))
;; We can use that data to construct a bibtex entry. We do that by defining a
;; template, and filling it in. I wrote this template expansion code which makes
;; it easy to substitute values like %{} in emacs lisp.
(defun doi-utils-expand-template (s)
"Expand a string template S containing %{} with the eval of its contents."
(replace-regexp-in-string "%{\\([^}]+\\)}"
(lambda (arg)
(let ((sexp (substring arg 2 -1)))
(format "%s" (eval (read sexp)))))
s))
;; Now we define a function that fills in that template from the metadata.
;; As different bibtex types share common keys, it is advantageous to separate
;; data extraction from json, and the formatting of the bibtex entry.
;; We use eval-and-compile because we use the three following forms in the
;; `doi-utils-def-bibtex-type' macro. Since the macro is expanded at compile
;; time, we need to ensure these defuns and defvars are evaluated at
;; compile-time.
(eval-and-compile
(defvar doi-utils-json-metadata-extract
'((type (plist-get results :type))
(author (mapconcat (lambda (x) (concat (plist-get x :given) " " (plist-get x :family)))
(plist-get results :author) " and "))
(title (plist-get results :title))
(subtitle (plist-get results :subtitle))
(journal (plist-get results :container-title))
(series (plist-get results :container-title))
(publisher (plist-get results :publisher))
(volume (plist-get results :volume))
(issue (plist-get results :issue))
(number (plist-get results :issue))
(year (elt (elt (plist-get (plist-get results :issued) :date-parts) 0) 0))
;; Some dates don't have a month in them.
(month (let ((date (elt
(plist-get (plist-get results :issued) :date-parts) 0)))
(if (>= (length date) 2)
(elt date 1)
"-")))
(pages (or (plist-get results :page)
(plist-get results :article-number)))
(doi (plist-get results :DOI))
(url (plist-get results :URL))
(booktitle (plist-get results :container-title))))
;; Next, we need to define the different bibtex types. Each type has a bibtex
;; type (for output) and the type as provided in the doi record. Finally, we
;; have to declare the fields we want to output.
(defvar doi-utils-bibtex-type-generators nil)
(defun doi-utils-concat-prepare (lst &optional acc)
"Minimize the number of args passed to `concat' from LST.
Given a list LST of strings and other expressions, which are
intended to be passed to `concat', concat any subsequent strings,
minimising the number of arguments being passed to `concat'
without changing the results. ACC is the list of additional
expressions."
(cond ((null lst) (nreverse acc))
((and (stringp (car lst))
(stringp (car acc)))
(doi-utils-concat-prepare (cdr lst) (cons (concat (car acc) (car lst))
(cdr acc))))
(t (doi-utils-concat-prepare (cdr lst) (cons (car lst) acc))))))
(defmacro doi-utils-def-bibtex-type (name matching-types &rest fields)
"Define a BibTeX type identified by (symbol) NAME.
MATCHING-TYPES is a list of strings. FIELDS are symbols that
match to retrieval expressions in
`doi-utils-json-metadata-extract'. This type will only be used
when the `:type' parameter in the JSON metadata is contained in
MATCHING-TYPES."
`(push (lambda (type results)
(when
(or ,@(mapcar
(lambda (match-type)
`(string= type ,match-type)) matching-types))
(let ,(mapcar (lambda (field)
(let ((field-expr
(assoc field doi-utils-json-metadata-extract)))
(if field-expr
;; need to convert to string first
`(,(car field-expr) (format "%s" ,(cadr field-expr)))
(error "Unknown bibtex field type %s" field))))
fields)
(concat
,@(doi-utils-concat-prepare
(-flatten
(list (concat "@" (symbol-name name) "{,\n")
;; there seems to be some bug with mapcan,
;; so we fall back to flatten
(mapcar (lambda (field)
`(" " ,(symbol-name field) " = {" ,field "},\n"))
fields)
"}\n")))))))
doi-utils-bibtex-type-generators))
(doi-utils-def-bibtex-type article ("journal-article" "article-journal")
author title journal year volume number pages doi url)
(doi-utils-def-bibtex-type inproceedings ("proceedings-article" "paper-conference")
author title booktitle year month pages doi url)
(doi-utils-def-bibtex-type book ("book")
author title series publisher year pages doi url)
(doi-utils-def-bibtex-type inbook ("chapter" "book-chapter" "reference-entry")
author title booktitle series publisher year pages doi url)
;; With the code generating the bibtex entry in place, we can glue it to the json retrieval code.
(defun doi-utils-doi-to-bibtex-string (doi)
"Return a bibtex entry as a string for the DOI. Not all types are supported yet."
(let* ((results (doi-utils-get-json-metadata doi))
(type (plist-get results :type)))
;; (format "%s" results) ; json-data
(or (-some (lambda (g) (funcall g type results)) doi-utils-bibtex-type-generators)
(message "%s not supported yet\n%S." type results))))
;; That is just the string for the entry. To be useful, we need a function that
;; inserts the string into a buffer. This function will insert the string at the
;; cursor, clean the entry, try to get the pdf, and create a notes entry for
;; you.
(defun doi-utils-insert-bibtex-entry-from-doi (doi)
"Insert bibtex entry from a DOI.
Also cleans entry using ‘org-ref’, and tries to download the corresponding pdf."
(insert (doi-utils-doi-to-bibtex-string doi))
(backward-char)
;; set date added for the record
(let ((ts (funcall doi-utils-timestamp-format-function)))
(when ts
(bibtex-set-field doi-utils-timestamp-field
ts)))
(org-ref-clean-bibtex-entry)
(save-buffer)
;; try to get pdf
(when doi-utils-download-pdf
(doi-utils-get-bibtex-entry-pdf))
(when (and doi-utils-make-notes org-ref-bibliography-notes)
(save-excursion
(when (f-file? org-ref-bibliography-notes)
(find-file-noselect org-ref-bibliography-notes)
(save-buffer))
(let ((bibtex-completion-bibliography (list (buffer-file-name))))
(funcall doi-utils-make-notes-function)))))
;; It may be you are in some other place when you want to add a bibtex entry.
;; This next function will open the first entry in org-ref-default-bibliography
;; go to the end, and add the entry. You can sort it later.
;;;###autoload
(defun doi-utils-add-bibtex-entry-from-doi (doi &optional bibfile)
"Add DOI entry to end of a file in the current directory.
Pick the file ending with .bib or in
`org-ref-default-bibliography'. If you have an active region that
starts like a DOI, that will be the initial prompt. If no region
is selected and the first entry of the ‘kill-ring’ starts like a
DOI, then that is the intial prompt. Otherwise, you have to type
or paste in a DOI.
Argument BIBFILE the bibliography to use."
(interactive
(list (read-string
"DOI: "
;; now set initial input
(cond
;; If region is active and it starts like a doi we want it.
((and (region-active-p)
(s-match "^10" (buffer-substring
(region-beginning)
(region-end))))
(buffer-substring (region-beginning) (region-end)))
((and (region-active-p)
(s-match "^http://dx\\.doi\\.org/" (buffer-substring
(region-beginning)
(region-end))))
(replace-regexp-in-string "^http://dx\\.doi\\.org/" ""
(buffer-substring (region-beginning) (region-end))))
((and (region-active-p)
(s-match "^https://dx\\.doi\\.org/" (buffer-substring
(region-beginning)
(region-end))))
(replace-regexp-in-string "^https://dx\\.doi\\.org/" ""
(buffer-substring (region-beginning) (region-end))))
((and (region-active-p)
(s-match (regexp-quote doi-utils-dx-doi-org-url) (buffer-substring
(region-beginning)
(region-end))))
(replace-regexp-in-string (regexp-quote doi-utils-dx-doi-org-url) ""
(buffer-substring (region-beginning) (region-end)))
(buffer-substring (region-beginning) (region-end)))
;; if the first entry in the kill-ring looks
;; like a DOI, let's use it.
((and
;; make sure the kill-ring has something in it
(stringp (car kill-ring))
(s-match "^10" (car kill-ring)))
(car kill-ring))
;; maybe kill-ring matches http://dx.doi or somthing
((and
;; make sure the kill-ring has something in it
(stringp (car kill-ring))
(s-match "^http://dx\\.doi\\.org/" (car kill-ring)))
(replace-regexp-in-string "^http://dx\\.doi\\.org/" "" (car kill-ring)))
((and
;; make sure the kill-ring has something in it
(stringp (car kill-ring))
(s-match "^https://dx\\.doi\\.org/" (car kill-ring)))
(replace-regexp-in-string "^https://dx\\.doi\\.org/" "" (car kill-ring)))
((and
;; make sure the kill-ring has something in it
(stringp (car kill-ring))
(s-match (regexp-quote doi-utils-dx-doi-org-url) (car kill-ring)))
(replace-regexp-in-string (regexp-quote doi-utils-dx-doi-org-url) "" (car kill-ring)))
;; otherwise, we have no initial input. You
;; will have to type it in.
(t
nil)))))
(unless bibfile
(setq bibfile (completing-read "Bibfile: " (org-ref-possible-bibfiles))))
;; Wrap in save-window-excursion to restore your window arrangement after this
;; is done.
(save-window-excursion
(with-current-buffer
(find-file-noselect bibfile)
;; Check if the doi already exists
(goto-char (point-min))
(if (word-search-forward (concat doi) nil t)
(message "%s is already in this file" doi)
(goto-char (point-max))
;; make sure we are at the beginning of a line
(when (not (= (point) (line-beginning-position)))
(forward-char 1))
(when (not (looking-back "\n\n" (min 3 (point))))
(insert "\n\n"))
(doi-utils-insert-bibtex-entry-from-doi doi)
(save-buffer)))))
(defalias 'doi-add-bibtex-entry 'doi-utils-add-bibtex-entry-from-doi
"Alias function for convenience.")
;;;###autoload
(defun doi-utils-doi-to-org-bibtex (doi)
"Convert a DOI to an ‘org-bibtex’ form and insert it at point."
(interactive "sDOI: ")
(with-temp-buffer
(insert (doi-utils-doi-to-bibtex-string doi))
(bibtex-clean-entry)
(kill-region (point-min) (point-max)))
(org-bibtex-yank)
(org-metaright)
(org-metaright))