-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsoncat
186 lines (172 loc) · 9.7 KB
/
jsoncat
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
#!/usr/bin/perl
# -------------------------------------------------------------------
# Install JSON: cpan -i JSON JSON::Parse Documentation: https://metacpan.org/pod/JSON , https://metacpan.org/dist/JSON-Parse/view/lib/JSON/Parse.pod
# -------------------------------------------------------------------
use JSON;use JSON::Parse 'read_json';use JSON::Parse ':all';use Data::Dumper;use utf8::all;
#use BibTeX::Parser;use IO::File;
use Getopt::Std;
use Switch 'Perl6'; use Sys::Hostname; use Time::HiRes qw(usleep);#usleep($microseconds)
use File::Basename; use File::Copy; use File::Find; use Cwd;use File::Path;# -------------------------------------------------------------------
# Default preferences: in order of priority: if many are one, the last 1 prevails
$showtype = 0; # Option: -T
$showtitle = 0; # Option: -t
$showauth = 0; # Option: -a
$showdoi = 0; # Option: -d
$showjournal = 0; # Option: -j
$long = 0; # Option: -l
$Long = 0; # Option: -L
$makereport = 0; # Option: -r
$showabstract = 0; # Option: -A
$dumpalldata = 0; # Option: -D
$dumpDepth = 8; # Option: -B
# --------------------- OPTIONS --------------------------------
$cdir = cwd;chomp($cdir);
$prog = basename($0);
getopt('B', \%opts); #here we add only options with args like: getopt('Xx', \%opts);
foreach $opt (keys %opts){
given($opt){
# when /[xX]/ {$t = $opts{$opt};print "${opt}: handling X\n";last;}
when /[T]/ {$showtype = 1;last;}
when /[t]/ {$showtitle = 1;last;}
when /[a]/ {$showauth = 1;last;}
when /[d]/ {$showdoi = 1;last;}
when /[j]/ {$showjournal = 1;last;}
when /[l]/ {$long = 1;last;}
when /[L]/ {$Long = 1;last;}
when /[r]/ {$makereport = 1;last;}
when /[A]/ {$makereport = 1;$showabstract = 1;last;}
when /[D]/ {$dumpalldata = 1;last;}
when /[B]/ {$dumpDepth = $opts{$opt};last;}
when 'h' {usage();last;}
default {usage("Not a valid option: $opt");}
}
}
# -------------------------------------------------------------------
# File handles to open:
@filehandles = ();
foreach my $file (@ARGV){
my $in;
unless( open ($in, "<", $file) ){die "${prog}: could not open $file for reading.";}
@filehandles = (@filehandles, $in);
}
if($#filehandles < 0){@filehandles = (*STDIN)}
# -------------------------------------------------------------------
# Analyze each file:
FILELOOP: foreach my $fh (@filehandles){
$t=parse_json (<$fh>); # from filename: $t = read_json ('filename')
if($dumpalldata){dumpData();next;}
$npubs = ${$t}{hits}{total};
PUBLOOP: for( my $n=0; $n < $npubs; $n++){
$npub = $n+1;
$id = ${$t}{hits}{hits}[$n]{id};
$clink = ${$t}{hits}{hits}[$n]{links}{citations};
$blink = ${$t}{hits}{hits}[$n]{links}{bibtex};
$bibid = ${$t}{hits}{hits}[$n]{metadata}{texkeys} [0];
$doi = ${$t}{hits}{hits}[$n]{metadata}{dois} [0]{value}; if( ! $doi ){$doi = "NoDOI";}
$type = ${$t}{hits}{hits}[$n]{metadata}{document_type} [0];
$title = ${$t}{hits}{hits}[$n]{metadata}{titles} [0]{title};
$abstract = ${$t}{hits}{hits}[$n]{metadata}{abstracts} [0]{value};
$cites = ${$t}{hits}{hits}[$n]{metadata}{citation_count};
$ccites = ${$t}{hits}{hits}[$n]{metadata}{citation_count_without_self_citations};
$nauth = ${$t}{hits}{hits}[$n]{metadata}{author_count};
$npages = ${$t}{hits}{hits}[$n]{metadata}{number_of_pages};
$arxiv = ${$t}{hits}{hits}[$n]{metadata}{arxiv_eprints} [0]{value}; if( ! $arxiv){$arxiv = "NoArXiv";}
$journal = ${$t}{hits}{hits}[$n]{metadata}{publication_info}[0]{journal_title};
$jvol = ${$t}{hits}{hits}[$n]{metadata}{publication_info}[0]{journal_volume};
$jyear = ${$t}{hits}{hits}[$n]{metadata}{publication_info}[0]{year}; if( ! $jyear){$jyear = ${$t}{hits}{hits}[$n]{metadata}{earliest_date};} # see also: preprint_date
$jpage = ${$t}{hits}{hits}[$n]{metadata}{publication_info}[0]{artid}; # same as: page_start (if it exists), prefer artid
$authorsb = ""; for( my $nn = 0; $nn < $nauth-1; $nn++){$authorsb .= ${$t}{hits}{hits}[$n]{metadata}{authors}[$nn ]{full_name}." and ";}; # BibTeX style
$authorsb .= ${$t}{hits}{hits}[$n]{metadata}{authors}[$nauth-1]{full_name};
$authors = ""; for( my $nn = 0; $nn < $nauth-1; $nn++){$authors .= ${$t}{hits}{hits}[$n]{metadata}{authors}[$nn ]{first_name}." ".${$t}{hits}{hits}[$n]{metadata}{authors}[$nn ]{last_name}.", ";}
$authors .= ${$t}{hits}{hits}[$n]{metadata}{authors}[$nauth-1]{first_name}." ".${$t}{hits}{hits}[$n]{metadata}{authors}[$nauth-1]{last_name};
@myrecids = (); for( my $nn = 0; $nn < $nauth ; $nn++){@myrecids = (@myrecids,${$t}{hits}{hits}[$n]{metadata}{authors}[$nn]{recid})};
#For authors, see also: first_name, last_name, full_name, recid
if($journal){$published = "$journal $jvol $jyear $jpage";}else{$published="NotPublished $jyear";}
$out = "$arxiv | $bibid | $id \n"; # print in columns using column -t -s "|"
if($showtype ){ $out = "$arxiv | $bibid | $id | $type \n" ;}
if($showtitle ){ $out = "$arxiv | $bibid | $id | $title \n" ;}
if($showauth ){ $out = "$arxiv | $bibid | $id | $nauth | $authors \n" ;}
if($showdoi ){ $out = "$arxiv | $bibid | $id | $doi \n" ;}
if($showjournal ){ $out = "$arxiv | $bibid | $id | $doi | $published \n" ;}
if($long ){ $out = "$arxiv | $bibid | $id | $doi | $type | $published \n";}
if($Long ){ $out = "$arxiv | $bibid | $id | $doi | $type | $published | $nauth | $authors | $title \n";}
if($makereport ){
$out = "-----------------|-------|--------|------------|---------|-----|------|----------\n";
$out .= " | npub | eprint | BibTex key | Slac ID | DOI | Type | No. Pages\n";
$out .= "NewRecord | $npub | $arxiv | $bibid | $id | $doi | $type | $npages\n";
$out .= "Published in | $published\n";
$out .= "DOI | $doi\n";
$out .= "Title | $title\n";
$out .= "Authors | $nauth | $authors\n";
$out .= "AuthorsBibTeX | $nauth | $authorsb\n";
if($showabstract){
$out .= "Abstract | $abstract\n";}
#$out .=
}
print "$out";
#if($npub > 1){last FILELOOP;} #Debugging
} # PUBLOOP: for( my $n=0; $n < $npubs; $n++){
} # FILELOOP: foreach my $fh (@filehandles){
#----------------------------------------------------------
sub dumpData {
#Make a shallower dump (you can change $Data::Dumper::Maxdepth to any depth you like)
#local $Data::Dumper::Purity = 1;
$Data::Dumper::Indent = 3;
$Data::Dumper::Deepcopy = 1; # avoid cross-refs
$Data::Dumper::Maxdepth = $dumpDepth; # no deeper than $dumpDepth refs down
print Dumper($t);
}
#----------------------------------------------------------
sub usage(){
if(@_){$message = shift(@_)."\n";}
$message .= << "EOF";
Usage: ${prog} [options] <files>
-t: Show title
-a: Show authors
-d: Show doi
-j: Show journal
-T: Show type of publications: ARTICLE, INPROCEEDINGS etc
-l: long listing
-L: longer listing
-r: make report: multiple line output per record
-A: make a report like -r, but print also the abstract
-D: dump data up to level \$dumpDepth (Default: $dumpDepth)
-B \$dumpDepth: Set level \$dumpDepth for a data dump.
Reads <files> or STDIN for BibTeX entries and print information in STDOUT. Each file (or STDIN) must contain only one JSON record.
Examples:
${prog} -r file1.json file2.json file3.json
cat file.json | ${prog} -d | column -t
cat file.json | ${prog} -a | cat -n | column -t -s"|"
cat file.json | ${prog} -l | grep article | column -t -s"|"
cat file.json | ${prog} -j | grep "Nucl. Phys. B" | column -t -s"|" | cat -n
cat file.json | ${prog} -l | grep JHEP | awk -F"|" '{print \$1,\$2,\$3}'
cat file.json | ${prog} -a | grep "^hep-th" | perl -nle '\@F=split /\\\|/;\@auth=split /\\,/,\$F[4];print \$F[0],\$\#auth+1, \$auth[0];'
(the last line counts the number of authors, and prints eprint, no. authors, and first author)
EOF
print STDERR $message;
exit(1);
}
sub main::HELP_MESSAGE(){ usage();} #for --help (does not work when default?)
# -------------------------------------------------------------------
# $f = /d/f.e => "e" = extension($f);"f"= filename($f);"/d"=dirname($f);"f.e"=basename($f);
sub extension(){($f,$d,$e)=fileparse(@_,qr/\.[^.]*$/);return $e}
sub filename (){($f,$d,$e)=fileparse(@_,qr/\.[^.]*$/);return $f}
# ! #####################################################################
# ! ---------------------------------------------------------------------
# ! Copyright by Konstantinos N. Anagnostopoulos (2023)
# ! Physics Dept., National Technical University,
# ! [email protected], www.physics.ntua.gr/konstant
# !
# ! 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, version 3 of the License.
# !
# ! 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 Liense along
# ! with this program. If not, see <http://www.gnu.org/licenses/>.
# ! ---------------------------------------------------------------------
# ! #####################################################################