-
-
Notifications
You must be signed in to change notification settings - Fork 244
/
openalex.el
1179 lines (1007 loc) · 36.2 KB
/
openalex.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
;;; openalex.el --- Org-ref interface to OpenAlex
;;; Commentary:
;; This is an elisp interface to OpenAlex (https://docs.openalex.org/) for org-ref.
;;
;; This provides functionality for the OpenAlex APIs.
;;
;; `oa-query' provides a general interface to all the endpoints with a filter.
;; It is not interactive though.
;;
;; `oa-author' provides an interactive way to search for an author and then see
;; a Google Scholar like org buffer with information about the author.
;;
;; `oa-fulltext-search' provides an interactive full text search of Works in
;; OpenAlex. You get an org-buffer of results, with links to subsequent pages of
;; results.
;;
;; `oa-coa' is an interactive command to generate the NSF COA form data for
;; coauthors.
;;
;; `oa-get-bibtex-entries' is an interactive command to download all bibtex
;; entries from headings in the current buffer with a DOI property.
;;
;; if you have an OpenAlex API you can set `oa-api-key' to use it. The
;; `user-mail-address' value will be added to the queries if it exists so you
;; will get the polite pool.
;;
;; This library extends the `org-ref-citation-hydra' and adds keys to get to
;; cited by, references and related documents in OpenAlex.
(require 'dash)
(require 's)
(require 'request)
(require 'doi-utils)
(require 'org-ref-citation-links)
(declare-function org-ref-possible-bibfiles "org-ref-core")
(declare-function org-ref-find-bibliography "org-ref-core")
(declare-function org-ref-get-bibtex-key-and-file "org-ref-core")
(declare-function bibtex-completion-show-entry "bibtex-completion")
(declare-function bibtex-completion-apa-format-reference "bibtex-completion")
(declare-function ivy-read "ivy")
(defcustom oa-api-key
nil
"Your API key if you have one."
:group 'openalex
:type 'string)
;;; Code:
(defun oa--response-parser ()
"Parse the response from json to elisp."
(let ((json-array-type 'list)
(json-object-type 'plist)
(json-key-type 'keyword)
(json-false nil)
(json-encoding-pretty-print nil))
(json-read)))
(defun oa-get (data query &optional iterable)
"Get fields from DATA with QUERY.
QUERY is a dot notation string.
key1.key2.key3 represents a nested item.
key1.key2[].key3 represents key3 on all items in key1.key2.
Tested with up to two [] in the query.
Assumes data is in plist form."
(let* ((fields (and query (split-string query "\\.")))
(current-field (and fields (pop fields))))
(cond
;; return condition
((null query)
data)
;; query[] means get query then turn iteration on
((and (s-ends-with-p "[]" current-field) (null iterable))
(setq current-field (substring current-field 0 -2))
(oa-get (plist-get data (intern-soft (concat ":" current-field)))
(when fields
(string-join fields "."))
t))
;; this means another level of iteration. You already have a collection. we
;; have to iterate over each one I think.
((and (s-ends-with-p "[]" current-field) iterable)
(setq current-field (substring current-field 0 -2))
(cl-loop for item in data collect
(oa-get (plist-get item (intern-soft (concat ":" current-field)))
(when fields
(string-join fields "."))
t)))
;; single keyword, iterate over collection
(iterable
(oa-get (cl-loop for item in data collect
(plist-get item (intern-soft (concat ":" current-field))))
(when fields
(string-join fields "."))
t))
;; single keyword
(t
(oa-get (plist-get data (intern-soft (concat ":" current-field)))
(when fields
(string-join fields ".")))))))
;; * General query
(defun oa--query-all-data (endpoint &rest filter)
(let* ((page 1)
(url (concat "https://api.openalex.org/" endpoint))
(filter-string (string-join (cl-loop for key in (plist-get-keys filter) collect
(concat (substring (symbol-name key) 1)
":"
(url-hexify-string
(plist-get filter key))))
","))
(params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)
("filter" . ,filter-string)
("page" . ,page)))
(req (request url :sync t :parser 'oa--response-parser :params params))
(data (request-response-data req))
(meta (plist-get data :meta))
(count (plist-get meta :count))
(per-page (plist-get meta :per_page))
(pages (ceiling (/ (float count) per-page)))
(results (plist-get data :results)))
(cl-loop for i from 2 to pages do
(setf (cdr (assoc "page" params)) i)
(setq req (request purl :sync t :parser 'oa--response-parser :params params)
data (request-response-data req)
results (append results (plist-get data :results))))
results))
(defun oa-query (endpoint &rest filter)
"Run a query at ENDPOINT with FILTER.
ENDPOINT can be works, authors, sources, institutions, concepts,
publishers, or funders.
FILTER is a plist (:field value) where :field is a valid filter
field (see
https://docs.openalex.org/how-to-use-the-api/get-lists-of-entities/filter-entity-lists),
and value is what you want to filter on. The following logic is supported:
!value is negation
<value is less than
>value is greater than
value1+value2 is and within a field
value1|value2 is or within a field
Your email address will be added if `user-mail-address' is
non-nil, and `oa-api-key' if it is non-nil to the API url."
(let* ((page (if (plist-get filter :page)
(prog1
(string-to-number (plist-get filter :page))
(setq filter (org-plist-delete filter :page)))
1))
(base-url "https://api.openalex.org")
(url (concat base-url "/" endpoint "?filter="))
(filter-string (string-join
(cl-loop for key in (plist-get-keys filter) collect
(concat (substring (symbol-name key) 1)
":"
(url-hexify-string
(plist-get filter key))))
","))
(req (request url :sync t :parser 'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)
("filter" . ,filter-string)
("page" . ,page))))
(data (request-response-data req))
(meta (plist-get data :meta))
(count (plist-get meta :count))
(per-page (plist-get meta :per_page))
(pages (ceiling (/ (float count) per-page)))
(results (plist-get data :results))
(next-page (format "[[elisp:(oa-query \"%s\" %s :page \"%s\")][Next page: %s]]"
endpoint
(string-join (cl-loop for x in filter
collect
(if (keywordp x)
(format "%s" x)
(format "%S" x)))
" ")
(+ page 1)
(+ page 1)))
(buf (generate-new-buffer "*OpenAlex - Query*")))
(with-current-buffer buf
(erase-buffer)
(org-mode)
(insert (concat
(format "#+title: %s
** Results
:PROPERTIES:
:FILTER: %s
:COUNT: %s
:END:
%s
\n\n"
filter-string
filter-string
count
next-page)
(cond
((string= endpoint "works")
(string-join
(cl-loop for wrk in results collect
(s-format "*** ${title}
:PROPERTIES:
:HOST: ${primary_location.source.display_name}
:YEAR: ${publication_year}
:CITED_BY_COUNT: ${cited_by_count}
:AUTHOR: ${authors}
:DOI: ${doi}
:OPENALEX: ${id}
:CREATED_DATE: ${created_date}
:END:
${get-bibtex}
- ${oa-refs}
- ${oa-related}
- ${oa-cited}
${abstract}
"
(lambda (key data)
(or (cdr (assoc key data)) ""))
`(("title" . ,(oa--title wrk))
("primary_location.source.display_name" . ,(oa-get wrk "primary_location.source.display_name"))
("publication_year" . ,(oa-get wrk "publication_year"))
("cited_by_count" . ,(oa-get wrk "cited_by_count"))
("authors" . ,(oa--authors wrk))
("doi" . ,(oa-get wrk "doi"))
("id" . ,(oa-get wrk "id"))
("created_date" . ,(oa-get wrk "created_date"))
("get-bibtex" . ,(oa--elisp-get-bibtex wrk))
("oa-refs" . ,(oa--elisp-get-oa-refs wrk))
("oa-related" . ,(oa--elisp-get-oa-related wrk))
("oa-cited" . ,(oa--elisp-get-oa-cited-by wrk))
("abstract" . ,(oa--abstract wrk)))))
"\n"))
(t
(format "%s" results)
)))))
(pop-to-buffer buf)
(goto-char (point-min))))
;; * Work object
(defun oa--work (entity-id &optional filter)
"Retrieve json data for a Work object for ENTITY-ID.
ENTITY-ID is an OpenAlex ID, DOI, Pubmed id,etc.
ENTITY-ID may also be a list of ids with a filter.
If FILTER is non-nil it should be a string like \"filter=openalex:\"
https://docs.openalex.org/api-entities/works"
(let* ((url (concat "https://api.openalex.org/works"
;; This is hackier than I prefer, but sometimes entity-id
;; is nil, or starts with ? for a filter, and I couldn't
;; see a cleaner way to solve this. this function is used
;; in a lot of places.
(cond
((string-prefix-p "?" entity-id)
entity-id)
(t
(format "/%s" entity-id)))))
(req (request url :sync t :parser 'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)
("filter" . ,filter))))
(data (request-response-data req)))
;; this is for convenience to inspect data in a browser, e.g. you can click
;; on the url in Emacs and it opens in a browser.
(plist-put data :oa-url url)
data))
;; * autocomplete works
(defun oa--works-candidates (query)
"Retrieve autocomplete works from OpenAlex."
(or
(ivy-more-chars)
(let* ((url "https://api.openalex.org/autocomplete/works")
(req (request url :sync t :parser 'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)
("q" . ,query))))
(data (request-response-data req))
(results (plist-get data :results)))
(cl-loop for work in results collect
(propertize
(format "%s, %s"
(plist-get work :display_name)
(plist-get work :hint))
'oaid (plist-get work :id))))))
(defun oa-works ()
"Autocomplete works.
This doesn't seem as useful as it could be."
(interactive)
(ivy-read "Work: " #'oa--works-candidates
:dynamic-collection t
:action
'(1
("o" (lambda (candidate)
(browse-url (get-text-property 0 'oaid candidate)))
"Open in browser"))))
;; * Viewing works
;; ** help functions for complex data
;;
;; Some things like authors need to be constructed, and cannot just be looked
;; up. In other cases, I want logic, e.g. if data is there provide something,
;; and if not return an empty string. These functions do that work.
(defun oa--authors (wrk)
"Return an author string for WRK.
The string is a comma-separated list of links to author pages in OpenAlex."
(s-join ", " (cl-loop for author in (plist-get wrk :authorships)
collect
(format "[[elisp:(oa--author-org \"%s\")][%s]]"
(plist-get
(plist-get author :author)
:id)
(plist-get
(plist-get author :author)
:display_name)))))
(defun oa--title (wrk)
"Return a title from WRK with linebreaks removed."
(string-replace "\n" " " (plist-get wrk :title)))
;; I want some links if they can be made so the buffer is interactive. It might
;; be nice to integrate M-, navigation.
(defun oa--elisp-get-bibtex (wrk)
"Return a elisp link to get a bibtex entry for WRK if there is a doi."
(if-let ((doi (plist-get wrk :doi)))
(format "[[elisp:(doi-add-bibtex-entry \"%s\")][Get bibtex entry]]" doi)
""))
(defun oa--elisp-get-oa-related (wrk)
"Return a elisp link to get related works for WRK."
(format "[[elisp:(progn (xref--push-markers (current-buffer) (point)) (oa--related-works \"%s\"))][Get related work (%s)]]"
(plist-get wrk :id)
(length (plist-get wrk :related_works))))
(defun oa--elisp-get-oa-refs (wrk)
"Return a elisp link to get references for WRK."
(format "[[elisp:(progn (xref--push-markers (current-buffer) (point)) (oa--referenced-works \"%s\"))][Get references (%s)]]"
(plist-get wrk :id)
(length (plist-get wrk :referenced_works))))
(defun oa--elisp-get-oa-cited-by (wrk)
"Return a elisp link to get works that cite WRK."
(format "[[elisp:(progn (xref--push-markers (current-buffer) (point)) (oa--cited-by-works \"%s\"))][Get cited by (%s)]]"
(plist-get wrk :id)
(plist-get wrk :cited_by_count)))
(defun oa--works-entries (works)
"Return a list of org-formatted entries in WORKS.
WORKS is a list of results from OpenAlex."
(cl-loop for wrk in (plist-get works :results)
collect
(s-format "** ${title}
:PROPERTIES:
:HOST: ${primary_location.source.display_name}
:YEAR: ${publication_year}
:CITED_BY_COUNT: ${cited_by_count}
:AUTHOR: ${authors}
:DOI: ${doi}
:OPENALEX: ${id}
:END:
${get-bibtex}
- ${oa-refs}
- ${oa-related}
- ${oa-cited}
"
(lambda (key data)
(or (cdr (assoc key data)) ""))
`(("title" . ,(oa-get wrk "title"))
("primary_location.source.display_name" . ,(oa-get wrk "primary_location.source.display_name"))
("publication_year" . ,(oa-get wrk "publication_year"))
("cited_by_count" . ,(oa-get wrk "cited_by_count"))
("authors" . ,(oa--authors wrk))
("doi" . ,(oa-get wrk "doi"))
("id" . ,(oa-get wrk "id"))
("get-bibtex" . ,(oa--elisp-get-bibtex wrk))
("oa-refs" . ,(oa--elisp-get-oa-refs wrk))
("oa-related" . ,(oa--elisp-get-oa-related wrk))
("oa-cited" . ,(oa--elisp-get-oa-cited-by wrk))))))
(defun oa--works-buffer (bufname header entries)
"Create an org-buffer with BUFNAME representing the results in WORKS.
HEADER is the first thing in the buffer
WORKS is usually a list of results from OpenAlex.
Argument ENTRIES A list of strings for each org entry."
(let ((buf (get-buffer-create bufname)))
(with-current-buffer buf
(erase-buffer)
(insert header)
(insert "#+COLUMNS: %25ITEM %YEAR %CITED_BY_COUNT
elisp:org-columns elisp:org-columns-quit
#+caption: Sort
| year | [[elisp:(oa-buffer-sort-year t)][old first]] | [[elisp:(oa-buffer-sort-year)][new first]] |
| cited by | [[elisp:(oa-buffer-sort-cited-by-count t)][low first]] | [[elisp:(oa-buffer-sort-cited-by-count)][high first]] |
")
(insert (s-join "\n" entries))
(org-mode)
(goto-char (point-min))
(org-next-visible-heading 1))
;; (display-buffer-in-side-window buf '((side . right)))
(pop-to-buffer buf)))
(defun oa--related-works (entity-id)
"Show the Related works buffer for ENTITY-ID."
(let* ((wrk (oa--work entity-id))
(related-work (plist-get wrk :related_works))
split
entries)
(while related-work
(setq split (-split-at 25 related-work)
related-work (nth 1 split))
;; split is what we process now
(setq entries (append entries
(oa--works-entries
(oa--work (format "?filter=openalex:%s" (s-join "|" (nth 0 split))))))))
(oa--works-buffer
"*OpenAlex - Related works*"
(format "* OpenAlex - Related works for %s ([[%s][json]])
%s\n\n"
entity-id
(plist-get wrk :oa-url)
(s-format ":PROPERTIES:
:TITLE: ${title}
:HOST: ${primary_location.source.display_name}
:AUTHOR: ${authors}
:DOI: ${doi}
:YEAR: ${publication_year}
:OPENALEX: ${id}
:END:
Found ${nentries} results.
"
(lambda (key data)
(or (cdr (assoc key data)) ""))
`(("title" . ,(oa-get wrk "title"))
("primary_location.source.display_name" . ,(oa-get wrk "primary_location.source.display_name"))
("authors" . ,(oa--authors wrk))
("doi" . ,(oa-get wrk "doi"))
("publication_year" . ,(oa-get wrk "publication_year"))
("id" . ,(oa-get wrk "id"))
("nentries" . ,(length entries)))))
entries)))
(defun oa--referenced-works (entity-id)
"Show the Referenced work for ENTITY-ID."
(let* ((wrk (oa--work entity-id))
(referenced-work (plist-get wrk :referenced_works))
split
(entries '()))
(while referenced-work
(setq split (-split-at 25 referenced-work)
referenced-work (nth 1 split))
;; split is what we process now
(setq entries (append entries
(oa--works-entries
(oa--work (format "?filter=openalex:%s"
(s-join "|" (nth 0 split))))))))
(oa--works-buffer
"*OpenAlex - References*"
(format "* OpenAlex - References from %s ([[%s][json]])
%s\n\n"
entity-id
(plist-get wrk :oa-url)
(s-format ":PROPERTIES:
:TITLE: ${title}
:HOST: ${primary_location.source.display_name}
:AUTHOR: ${authors}
:DOI: ${doi}
:YEAR: ${publication_year}
:OPENALEX: ${id}
:END:
Found ${nentries} results.
"
(lambda (key data)
(or (cdr (assoc key data)) ""))
`(("title" . ,(oa-get wrk "title"))
("primary_location.source.display_name" . ,(oa-get wrk "primary_location.source.display_name"))
("authors" . ,(oa--authors wrk))
("doi" . ,(oa-get wrk "doi"))
("publication_year" . ,(oa-get wrk "publication_year"))
("id" . ,(oa-get wrk "id"))
("nentries" . ,(length entries)))))
entries)))
;; This function is different than the previous two. First we follow a URL
;; provided by the data, and second, here we do follow pages.
(defun oa--cited-by-works (entity-id)
"Show the Cited by buffer for ENTITY-ID."
(let* ((wrk (oa--work entity-id))
(url (plist-get wrk :cited_by_api_url))
(cited-by-works (request-response-data
(request url
:sync t
:parser 'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)))))
(count (plist-get (plist-get cited-by-works :meta) :count))
(per-page (plist-get (plist-get cited-by-works :meta) :per_page))
(entries '())
(page 2))
;; get first page
(setq entries (oa--works-entries cited-by-works))
(while (> count (* per-page (- page 1)))
(setq cited-by-works (request-response-data
(request url
:sync t
:parser 'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)
("page" . ,page)))))
(setq entries (append entries (oa--works-entries cited-by-works)))
(cl-incf page))
(oa--works-buffer
"*OpenAlex - Cited by*"
(format "* OpenAlex - %s Cited by ([[%s][json]])
%s"
entity-id
url
(s-format ":PROPERTIES:
:TITLE: ${title}
:HOST: ${primary_location.source.display_name}
:AUTHOR: ${authors}
:DOI: ${doi}
:YEAR: ${publication_year}
:OPENALEX: ${id}
:END:
Found ${nentries} results.
"
(lambda (key data)
(or (cdr (assoc key data)) ""))
`(("title" . ,(oa-get wrk "title"))
("primary_location.source.display_name" . ,(oa-get wrk "primary_location.source.display_name"))
("authors" . ,(oa--authors wrk))
("doi" . ,(oa-get wrk "doi"))
("publication_year" . ,(oa-get wrk "publication_year"))
("id" . ,(oa-get wrk "id"))
("nentries" . ,(length entries)))))
entries)))
;; ** buffer utilities for sorting entries
(defun oa-buffer-sort-year (&optional ascending)
"Sort org headings by year in descending order (new to old).
With prefix arg ASCENDING, sort in ascending order (old to new)"
(interactive "P")
(if ascending
(org-sort-entries nil ?f
(lambda () (string-to-number (or (org-entry-get (point) "YEAR") "0")))
(lambda (y1 y2)
(< y1 y2)))
(org-sort-entries nil ?f
(lambda () (string-to-number (or (org-entry-get (point) "YEAR") "0")))
(lambda (y1 y2)
(> y1 y2))))
(org-fold-show-all))
(defun oa-buffer-sort-cited-by-count (&optional ascending)
"Sort org headings by cited by count in descending order high to low.
With prefix arg ASCENDING sort from low to high."
(interactive "P")
(if ascending
(org-sort-entries nil ?f
(lambda ()
(string-to-number
(or (org-entry-get (point) "CITED_BY_COUNT")
"0")))
#'<)
(org-sort-entries nil ?f
(lambda ()
(string-to-number
(or
(org-entry-get (point) "CITED_BY_COUNT")
"0")))
#'>))
(org-fold-show-all))
;; * Interactive versions for org-ref citations
(defun oa-related-works ()
"Open the side window for Related works on cite at point."
(interactive)
(oa--related-works (concat "https://doi.org/" (org-ref-get-doi-at-point))))
(defun oa-referenced-works ()
"Open the side window for References from the cite at point."
(interactive)
(oa--referenced-works (concat "doi:" (org-ref-get-doi-at-point))))
(defun oa-cited-by-works ()
"Open the side window for Citing works for the cite at point."
(interactive)
(oa--cited-by-works (concat "doi:" (org-ref-get-doi-at-point))))
(defun oa-open ()
"Open the cite at point in OpenAlex."
(interactive)
(let* ((url (concat
"https://api.openalex.org/works/https://doi.org/"
(org-ref-get-doi-at-point)))
(req (request url :sync t :parser 'oa--response-parser))
(data (request-response-data req)))
(browse-url (plist-get data :id))))
(defhydra+ org-ref-citation-hydra ()
"Add open from action to `org-ref-citation-hydra'."
("xa" oa-open "Open in OpenAlex" :column "OpenAlex")
("xr" oa-related-works "Related documents" :column "OpenAlex")
("xc" oa-cited-by-works "Cited by documents" :column "OpenAlex")
("xf" oa-referenced-works "References from" :column "OpenAlex"))
;; * Author object
(defun oa--author (entity-id &optional filter)
"Get an Author object for ENTITY-ID.
FILTER is an optional string to add to the URL."
(let* ((url (concat "https://api.openalex.org/authors"
entity-id))
(req (request url :sync t :parser 'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)
("filter" . ,filter))))
(data (request-response-data req)))
;; this is for convenience to inspect data in a browser.
(plist-put data :oa-url url)
data))
(defun oa--author-entries (works-data url)
"Get entries from WORKS-DATA."
(let* ((meta (plist-get works-data :meta))
(per-page (plist-get meta :per_page))
(count (plist-get meta :count))
(pages (/ count per-page))
(entries '())
purl)
;; if there is a remainder we need to get the rest
(when (> (mod count per-page) 0) (cl-incf pages))
;; Now we have to loop through the pages
(cl-loop for i from 1 to pages
do
(setq works-data (request-response-data
(request url
:sync t
:parser 'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)
("page" . ,i))))
entries (append entries
(cl-loop for result in (plist-get works-data :results)
collect
(s-format "*** ${title}
:PROPERTIES:
:ID: ${id}
:DOI: ${ids.doi}
:PDF: ${primary_location.pdf_url}
:LANDING_PAGE: ${primary_location.landing_page_url}
:YEAR: ${publication_year}
:HOST_VENUE: ${primary_location.source.display_name}
:AUTHORS: ${authors}
:CITED_BY_COUNT: ${cited_by_count}
:END:
${get-bibtex}
- ${oa-refs}
- ${oa-related}
- ${oa-cited}
${abstract}
" (lambda (key data)
(or (cdr (assoc key data)) ""))
`(("title" . ,(oa--title result))
("id" . ,(oa-get result "id"))
("ids.doi" . ,(oa-get result "ids.doi"))
("publication_year" . ,(oa-get result "publication_year"))
("primary_location.pdf_url" . ,(oa-get result "primary_location.pdf_url"))
("primary_location.landing_page_url" . ,(oa-get result "primary_location.landing_page_url"))
("primary_location.source.display_name" . ,(oa-get result "primary_location.source.display_name"))
("authors" . ,(oa--authors result))
("cited_by_count" . ,(oa-get result "cited_by_count"))
("get-bibtex" . ,(oa--elisp-get-bibtex result))
("oa-refs" . ,(oa--elisp-get-oa-refs result))
("oa-related" . ,(oa--elisp-get-oa-related result))
("oa-cited" . ,(oa--elisp-get-oa-cited-by result))
("abstract" . ,(oa--abstract result))))))))
entries))
(defun oa--abstract (wrk)
"Construct an abstract from a WRK."
(let* ((aii (plist-get wrk :abstract_inverted_index))
(word_index '())
sorted)
(cl-loop for (k v) on aii by 'cddr
do
(cl-loop for index in v
do
(push (list k index) word_index)))
(setq sorted (sort
word_index
(lambda (a b)
(<
(nth 1 a)
(nth 1 b)))))
(string-join (mapcar
(lambda (x)
(substring
(symbol-name (car x))
1))
sorted)
" ")))
(defun oa--author-candidates (query)
"Retrieve autocomplete authors from OpenAlex."
(or
(ivy-more-chars)
(let* ((url "https://api.openalex.org/autocomplete/authors")
(req (request url :sync t :parser 'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)
("q" . ,query))))
(data (request-response-data req))
(results (plist-get data :results)))
(cl-loop for author in results collect
(propertize
(format "%s - %s"
(plist-get author :display_name)
(plist-get
author :hint))
'oaid (plist-get author :id))))))
(defun oa--counts-by-year (data)
"Get citation counts by year and make a graph.
DATA is an author from OpenAlex.
Requires gnuplot. Generates a temporary file."
(if (executable-find "gnuplot")
(let* ((pngfile (make-temp-file "oa-" nil ".png"))
(counts (sort
(cl-loop for i from 1 for item in (plist-get data :counts_by_year)
collect
(list
(plist-get item :year)
(plist-get item :cited_by_count)
(plist-get item :works_count)))
(lambda (a b)
(< (nth 0 a)
(nth 0 b)))))
(count-string (cl-loop for i from 1 for (year cites works) in counts
concat
(format "%s \"%s\" %s %s\n"
i year cites works)))
(gnuplot (format "set terminal \"png\" size 800,400
set output \"%s\"
$counts << EOD
%s
EOD
set boxwidth 0.5
set style fill solid noborder
set style line 1 lc rgb \"grey\"
set ylabel \"Citation count\"
set y2label \"Document count\"
set y2tics nomirror
set key left top
plot $counts using 1:3:xtic(2) with boxes lc rgb \"grey\" title \"Citations per year\", \"\" using 1:4 axes x1y2 with lines title \"Document count\"
"
pngfile
count-string))
(cmdfile (make-temp-file "gnuplot-cmds-" nil ".gpl"))
(shellcmd (format "gnuplot --persist -c \"%s\"" cmdfile)))
(with-temp-file cmdfile
(insert gnuplot))
(shell-command shellcmd)
(delete-file cmdfile)
(format "[[%s]]" pngfile))
"Gnuplot required to see citation graph. Please install it."))
(defun oa--author-org (entity-id)
"Generate an org-buffer for an author with ENTITY-ID."
(let* ((buf (get-buffer-create "*OpenAlex - Author*"))
(data (oa--author entity-id))
(citations-image (oa--counts-by-year data))
(works-count (plist-get data :works_count))
(works-url (plist-get data :works_api_url))
(works-data (request-response-data
(request works-url
:sync t
:parser 'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key))))))
(with-current-buffer buf
(erase-buffer)
(insert (s-format "* ${display_name} ([[${oa-url}][json]])
:PROPERTIES:
:OPENALEX: ${id}
:ORCID: ${orcid}
:SCOPUS: ${ids.scopus}
:WORKS_COUNT: ${works_count}
:CITED_BY_COUNT: ${cited_by_count}
:INSTITUTION: ${last_known_institution.display_name}, ${last_known_institution.country_code}
:END:
#+COLUMNS: %25ITEM %YEAR %CITED_BY_COUNT
elisp:org-columns elisp:org-columns-quit
${citations-image}
** Articles
#+caption: Sort
| year | [[elisp:(oa-buffer-sort-year t)][old first]] | [[elisp:(oa-buffer-sort-year)][new first]] |
| cited by | [[elisp:(oa-buffer-sort-cited-by-count t)][low first]] | [[elisp:(oa-buffer-sort-cited-by-count)][high first]] |
"
(lambda (key data)
(or (cdr (assoc key data)) ""))
`(("display_name" . ,(oa-get data "display_name"))
("oa-url" . ,(oa-get data "oa-url"))
("id" . ,(oa-get data "id"))
("orcid" . ,(oa-get data "orcid"))
("ids.scopus" . ,(oa-get data "ids.scopus"))
("works_count" . ,(oa-get data "works_count"))
("last_known_institution.display_name" . ,(oa-get data "last_known_institution.display_name"))
("last_known_institution.country_code" . ,(oa-get data "last_known_institution.country_code"))
("citations-image" . ,citations-image))))
(insert (s-join "\n" (oa--author-entries works-data works-url)))
(org-mode)
(goto-char (point-min))
(org-next-visible-heading 1))
(pop-to-buffer buf)))
(defun oa-author ()
"Get data and act on it for an author."
(interactive)
(ivy-read "Author: " #'oa--author-candidates
:dynamic-collection t
:action
'(1
("o" (lambda (candidate)
(oa--author-org (get-text-property 0 'oaid candidate)))
"Open org file")
("l" (lambda (candidate)
(insert (format "[[%s][%s]]"
(get-text-property 0 'oaid candidate)
candidate))))
("u" (lambda (candidate)
(browse-url (get-text-property 0 'oaid candidate)))
"Open in browser"))))
;; * Full text search
(defun oa-fulltext-search (query &optional page)
"Perform a fulltext search on QUERY.
PAGE is optional, and loads that page of results. Defaults to 1."
(interactive (list (read-string "Query: ")
nil))
(when (null page) (setq page 1))
(let* ((url "https://api.openalex.org/works")
(req (request url
:sync t
:parser #'oa--response-parser
:params `(("mailto" . ,user-mail-address)
("api_key" . ,oa-api-key)
("page" . ,page)
("filter" . ,(format "fulltext.search:%s" query)))))
(data (request-response-data req))
(metadata (plist-get data :meta))
(count (plist-get metadata :count))
(per-page (plist-get metadata :per_page))
(npages (+ (/ count per-page) (if (= 0 (mod count per-page)) 0 1)))
(results (plist-get data :results))
(next-page (format "[[elisp:(oa-fulltext-search \"%s\" %s)][Next page: %s of %s]]"
query
(+ page 1)
(+ page 1)
npages))
(buf (get-buffer-create "*OpenAlex Full-text search*")))
(with-current-buffer buf
(erase-buffer)
(org-mode)
(insert (s-format "#+title: Full-text search: ${query}
[[elisp:(oa-fulltext-search \"${query}\" ${page})]]"
'aget
`(("query" . ,query)
("page" . ,page))))
(insert (s-format
"
${meta.count} results: Page ${meta.page} of ${s1} ${s2}
\n\n"
(lambda (key data)
(or (cdr (assoc key data)) ""))
`(("meta.page" . ,(oa-get data "meta.page"))
("s1" . ,(format "%s" npages))
("s2" . ,(format "%s" next-page)))))
(insert
(cl-loop for result in results concat
(s-format "* ${title}
:PROPERTIES:
:JOURNAL: ${primary_location.source.display_name}
:AUTHOR: ${authors}
:YEAR: ${publication_year}
:OPENALEX: ${id}
:DOI: ${ids.doi}
:END:
${get-bibtex}
- ${oa-refs}
- ${oa-related}
- ${oa-cited}
" (lambda (key data)
(or (cdr (assoc key data)) ""))
`(("title" . ,(oa--title result))
("primary_location.source.display_name" . ,(oa-get result "primary_location.source.display_name"))
("authors" . ,(oa--authors result))
("publication_year" . ,(oa-get result "publication_year"))
("id" . ,(oa-get result "id"))
("ids.doi" . ,(oa-get result "ids.doi"))