forked from USGCRP/gcis-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-article.pl
More file actions
executable file
·593 lines (462 loc) · 13.1 KB
/
add-article.pl
File metadata and controls
executable file
·593 lines (462 loc) · 13.1 KB
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
#!/usr/bin/env perl
=head1 NAME
add-article.pl -- Add articles to GCIS.
=head1 DESCRIPTION
add-article.pl adds a list of articles to GCIS based on their DOIs. Journals
containing the article may also be added (optional).
If an article (or journal) already exists in GCIS, it is not added.
'crossref.org' is used to obtain biblio information for the articles and
journals.
An errata file may be used when verifying the journal information. This allows for the journal information obtained from the doi to be different from
the information stored in gcis (the gcis information is not changed).
[Note: Books and book sections are not fully implemented and so are not added.]
=head1 SYNOPSIS
./add-article.pl [OPTIONS]
=head1 OPTIONS
=over
=item B<--url>
GCIS url, e.g. http://data-stage.globalchange.gov
=item <stdin>
List of article dois (one per line)
=item B<--max_updates>
Maximum number of update (defaults to 10)
=item B<--do_not_add_journal>
Flag indicating journals are not to be added
=item B<--errata>
Errata file (yaml) - contains aliases for articles or journals that
already exists (see below for file example)
=item B<--verbose>
Verbose option
=item B<--dry_run> or B<--n>
Dry run
=back
=head1 EXAMPLES
# add a set of articles to gcis
./add-article.pl -u http://data-stage.globalchange.gov < article_list.txt
Example errata file (value corresponds to gcis, alias to crossref):
---
article:
- uri: /article/10.9999/1999.abc
errata:
- item: issn
source: crossref
value: 0001-0002
alias: 9991-9992
=cut
use Gcis::Client;
use YAML::XS;
use Data::Dumper;
use Clone::PP qw(clone);
use Getopt::Long qw/GetOptions/;
use Pod::Usage;
use strict;
use v5.14;
GetOptions(
'url=s' => \(my $url),
'max_updates=i' => \(my $max_updates = 10),
'do_not_add_journal' => \(my $do_not_add_journal),
'errata=s' => \(my $errata),
'verbose' => \(my $verbose),
'dry_run|n' => \(my $dry_run),
'help|?' => sub { pod2usage(verbose => 2) },
) or die pod2usage(verbose => 1);
pod2usage(msg => "missing url", verbose => 1) unless $url;
my $n_updates = 0;
&main;
sub main {
say " adding articles";
say " url : $url";
say " max updates : $max_updates";
say " do not add journal" if $do_not_add_journal;
say " errata file : $errata" if $errata;
say " verbose on" if $verbose;
say " dry run" if $dry_run;
my $g = $dry_run ? Gcis::Client->new(url => $url) :
Gcis::Client->connect(url => $url);
my $d = Gcis::Client->new
->accept("application/vnd.citationstyles.csl+json;q=0.5")
->url("http://api.crossref.org");
my $e = load_errata($errata) if $errata;
$d->logger($g->logger);
for (<STDIN>) {
s/\r//;
chomp;
my $doi = $_;
say " doi : $doi";
if (doi_exists($g, $doi)) {
say " already in gcis : $doi";
next;
}
my $art = get_article($d, $doi) or do {
say " not found or error in crossref : $doi";
next;
};
say " art :\n".Dumper($art) if $verbose;
fix_article($e, $art);
$art->{title} or do {
say " no title (after errata) for $doi";
next;
};
my $pub;
if ($art->{issn}) {
$pub = get_journal_gcis($g, $art);
next if $pub eq 1;
if ($pub) {
fix_journal($e, $pub);
titles_match($art, $pub) or next;
} else {
$pub = get_journal_doi($d, $art);
fix_journal($e, $pub);
titles_match($art, $pub) or next;
put_journal($g, $pub) or next;
}
} elsif ($art->{isbn}) {
say " skipping book sections for now : $doi";
next;
$pub = get_book($d, $art) or next;
put_book($g, $pub) or next;
} else {
say " no issn or isbn for $doi";
next;
}
$art->{journal_identifier} = $pub->{identifier};
put_article($g, $art);
last if $n_updates >= $max_updates;
}
say " done";
}
sub load_errata {
my $file = shift;
open my $f, '<:encoding(UTF-8)', $file or die "can't open file : $file";
my $yml = do { local $/; <$f> };
my $y = Load($yml);
my @resource_list = qw(journal article);
my @detail_list = qw(item source value alias);
my @source_list = qw(crossref);
my $e;
ref $y eq 'HASH' or die "top level not a hash";
for my $r (keys %{ $y }) {
grep $r eq $_, @resource_list or next;
ref $y->{$r} eq 'ARRAY' or die "second level must be an array";
for my $h (@{ $y->{$r} }) {
ref $h eq 'HASH' or die "third level must be a hash";
my $uri = $h->{uri} or die "no uri";
$h->{errata} or die "no errata";
ref $h->{errata} eq 'ARRAY' or die "forth level must be an array";
for my $g (@{ $h->{errata} }) {
my $i = $g->{item} or die "no item";
grep $g->{source}, $_, @source_list or next;
for my $d (keys %{ $g }) {
next if $d eq 'item';
grep $d eq $_, @detail_list or die "invalid detail";
$e->{$uri}->{$i}->{$d} = $g->{$d};
}
}
}
}
return $e;
}
sub doi_exists {
my ($g, $doi) = @_;
return 1 if $g->get("/article/$doi");
my $r = $g->get("/autocomplete?q=$doi");
my $n = @$r;
return 0 if $n eq 0;
if ($n > 1) {
say " more than one entry with this doi";
return 1;
}
my ($u) = (@$r[0] =~ /^\[(.*?)\]/);
return 0 if $u eq 'dataset';
if ($u eq 'chapter') {
my ($d, $t) = (@$r[0] =~ /{(.*?)} {(.*?)}/);
$u = "/report/$t/$u/$d";
} else {
my ($d) = (@$r[0] =~ /{(.*?)}/);
$u = "/$u/$d";
}
return 1 if $r = $g->get("$u");
say " doi not found (autocomplete)";
return 0;
}
sub get_article {
my ($d, $doi) = @_;
my $c = $d->get("/works/$doi") or do {
say " no article for $doi";
return 0;
};
my $cr = $c->{message} or do {
say " no article content for $doi";
return 0;
};
my $a;
$a->{doi} = $doi;
$a->{identifier} = $doi;
$a->{title} = $cr->{title}[0] or
say " no title for $doi";
$a->{year} = $cr->{issued}{'date-parts'}[0][0] or do {
say " no year for $doi";
return 0;
};
if ($cr->{volume}) {
$a->{journal_vol} = $cr->{volume};
}
if ($cr->{page}) {
$a->{journal_pages} = $cr->{page};
}
my $isn;
if ($cr->{ISSN}[0]) {
$a->{issn} = clone($cr->{ISSN});
} elsif ($cr->{ISBN}[0]) {
$a->{isbn} = clone($cr->{ISBN});
} else {
say " no ISSN or ISBN for $doi";
}
if ($cr->{'container-title'}[0]) {
$a->{parent_title} = clone($cr->{'container-title'});
}
$a->{type} = $cr->{type};
return $a;
}
sub fix_article {
my ($e, $a) = @_;
my $uri = "/article/$a->{identifier}";
my $f = $e->{$uri} or return 0;
for (keys %{ $f }) {
my $i = $f->{$_};
if (ref $a->{$_} eq 'ARRAY') {
for my $v (@{ $a->{$_} }) {
$v = $i->{alias} if $v eq $i->{value};
}
} else {
$a->{$_} = $i->{alias} if $a->{$_} eq $i->{value};
}
}
return 0;
}
sub fix_journal {
my ($e, $p) = @_;
my $uri = "/journal/$p->{identifier}";
my $f = $e->{$uri} or return 0;
for (keys %{ $f }) {
$p->{$_} or next;
my $i = $f->{$_};
if (ref $p->{$_} eq 'ARRAY') {
for my $v (@{ $a->{$_} }) {
$v = $i->{alias} if $v eq $i->{value};
}
} else {
$p->{$_} = $i->{alias} if $p->{$_} eq $i->{value};
}
$_ eq 'title' or next;
$p->{identifier} = make_id($i->{alias});
}
return 0;
}
sub get_journal_gcis {
my ($g, $art) = @_;
my $n = @{ $art->{issn} } or return 0;
my $issn;
my $id;
my $r;
for $issn (@{ $art->{issn} }) {
$r = $g->get("/autocomplete?type=journal&q=$issn") or do {
say " can not do autocomplete for : $issn";
next;
};
}
return 0 if @$r eq 0;
if (@$r gt 1) {
say " more than one journal with same issn : $issn";
return 1;
}
my ($t) = (@$r[0] =~ /^\[(.*?)\]/);
if ($t ne 'journal') {
say " not a journal : @$r[0]";
return 1;
};
my ($id) = (@$r[0] =~ /{(.*?)}/);
my $p = $g->get("/journal/$id") or do {
say " journal not found : $id";
return 1;
};
return $p;
}
sub titles_match {
my ($a, $p) = @_;
my $match = 0;
for (@{ $a->{parent_title} }) {
$match = $p->{title} eq $_;
last if $match;
}
return 1 if $match;
say " same journal with different title : $p->{identifier}";
say " article title(s) : $_" for @{ $a->{parent_title} };
say " journal title : $p->{title}";
return 0;
}
sub get_journal_doi {
my ($d, $art) = @_;
my $n = @{ $art->{issn} } or return 0;
my $c;
my $issn;
for (@{ $art->{issn} }) {
$c = $d->get("/journals/issn:$_") or next;
$issn = $_;
last;
}
if (!$c) {
say " no journal for $art->{doi}";
return 0;
}
my $cr = $c->{message} or do {
say " no journal content for $issn";
return 0;
};
my $p;
$p->{print_issn} = $cr->{ISSN}[0] or do {
say " no issn for $issn";
return 0;
};
if ($cr->{ISSN}[1]) {
$p->{online_issn} = $cr->{ISSN}[1];
}
$p->{title} = $cr->{title} or do {
say " no title for $issn";
return 0;
};
$p->{publisher} = $cr->{publisher} or do {
say " no publisher for $issn";
return 0;
};
$p->{identifier} = make_id($p->{title});
return $p;
}
sub get_book {
my ($d, $art) = @_;
my $n = @{ $art->{isbn} } or return 0;
my $id = $art->{doi};
$id =~ s/\/.*//;
my $c;
my $isbn;
for (@{ $art->{isbn} }) {
s/.*\///;
$c = $d->get("/works/$id/$_") or next;
$isbn = $_;
last;
}
if (!$c) {
say " no book for $art->{doi}";
return 0;
}
my $cr = $c->{message} or do {
say " no journal content for $isbn";
return 0;
};
my $a;
$a->{isbn} = $isbn;
$a->{title} = $cr->{title} or do {
say " no title for $isbn";
return 0;
};
$a->{publisher} = $cr->{publisher} or do {
say " no publisher for $isbn";
return 0;
};
$a->{year} = $cr->{issued}{'date-parts'}[0][0] or do {
say " no year for $isbn";
return 0;
};
$a->{type} = $cr->{type};
return $a;
}
sub check_book {
my ($g, $pub) = @_;
my $isbn = $pub->{isbn};
my $r = $g->get("/autocomplete?type=book&q=$isbn") or do {
say " can not do autocomplete for : $isbn";
return 0;
};
if (@$r gt 1) {
say " more than one book with same isbn : $isbn";
return 1;
}
return 0 unless @$r eq 1;
my ($t) = (@$r[0] =~ /^\[(.*?)\]/);
if ($t ne 'book') {
say " not a book : @$r[0]";
return 1;
};
my ($id) = (@$r[0] =~ /{(.*?)}/);
my $p = $g->get("/book/$id") or do {
say " book not found : $id";
return 0;
};
if ($p->{title} ne $pub->{title}) {
say " same book with different title : $isbn";
return 1;
}
return $id;
}
sub make_id {
my $t = shift;
my $id;
for (split / /, $t) {
chomp;
s/&/and/;
s/:|;|,|\.|!|\"|\'|_/-/g;
s/\(|\)|\[|\]|\{|\}/-/g;
$id = $id.($id ? "-" : "").(lc $_);
}
$id =~ s/-+/-/g;
$id =~ s/^-|-$//g;
return $id;
}
sub put_book {
my ($g, $pub) = @_;
my $p = clone($pub);
delete $p->{type};
my $t = $p->{title}[0];
delete $p->{title};
$p->{title} = $t;
my $id = check_book($g, $p);
return $id if $id;
if ($dry_run) {
say " would add book : $p->{title}";
return 1;
}
say " adding book : $p->{title}";
$p->{identifier} = make_id($p->{title});
$g->post("/book" => $p) or die $g->error;
$n_updates++;
return check_book($g, $p);
}
sub put_journal {
my ($g, $pub) = @_;
my $id;
my $p = clone($pub);
delete $p->{type};
if ($dry_run or $do_not_add_journal) {
say " would add journal : $p->{identifier}";
return 1;
}
say " adding journal : $p->{identifer}";
$g->post("/journal" => $p) or die $g->error;
$n_updates++;
return 0;
}
sub put_article {
my ($g, $art) = @_;
my $a = clone($art);
delete $a->{$_} for qw(issn isbn type parent_title);
my $doi = $a->{doi};
if ($dry_run) {
say " would add article : $doi";
return 0;
}
say " adding article : $doi";
$g->post("/article" => $a) or die $g->error;
$n_updates++;
return 0;
}
1;