Skip to content
Snippets Groups Projects
Commit d2bf2b5e authored by Gerhard Gonter's avatar Gerhard Gonter :speech_balloon:
Browse files

make it more versatile

parent c82e5c02
Branches
No related tags found
No related merge requests found
#!/usr/bin/perl
#!/usr/local/bin/perl
# $Id: script.pl,v 1.19 2025/03/22 13:54:11 gonter Exp $
=head1 NAME
dummy script doing nothing
=cut
use strict;
......@@ -6,29 +13,144 @@ use Data::Dumper;
$Data::Dumper::Indent= 1;
$Data::Dumper::Sortkeys= 1;
use FileHandle;
binmode( STDOUT, ':utf8' ); autoflush STDOUT 1;
binmode( STDERR, ':utf8' ); autoflush STDERR 1;
binmode( STDIN, ':utf8' );
use Util::Simple_CSV;
my $fnm= shift(@ARGV);
my $doit= 1;
my $mode= 'wait';
my $sleep_time= 8;
my @PARS;
my $arg;
my @tickets;
while (defined ($arg= shift (@ARGV)))
{
utf8::decode($arg); # needed to process utf8 characters in commandline arguments
if ($arg eq '-') { push (@PARS, '-'); }
elsif ($arg eq '--') { push (@PARS, @ARGV); @ARGV= (); }
elsif ($arg =~ /^--(.+)/)
{
my ($opt, $val)= split ('=', $1, 2);
if ($opt eq 'help') { usage(); }
elsif ($opt eq 'dry-run') { $doit= 0 }
elsif ($opt eq 'doit') { $doit= 1 }
elsif ($opt eq 'sleep') { $mode= 'sleep'; my $t= $val || shift (@ARGV) || 8; $sleep_time= $t; }
elsif ($opt eq 'ticket' || $opt eq 'tickets')
{
$val= shift (@ARGV) unless (defined ($val));
my @t= split(',', $val);
foreach my $t (@t) { push (@tickets, $1) if ($t =~ m/^#?(\d+)$/); } # else complain
}
else { usage(); }
}
elsif ($arg =~ /^-(.+)/)
{
foreach my $opt (split ('', $1))
{
if ($opt eq 'h') { usage(); exit (0); }
elsif ($opt eq 'n') { $doit= 0; }
elsif ($opt eq '$') { $doit= 1; }
else { usage(); }
}
}
else
{
if ($arg =~ /^\d+$/ && !-f $arg) { push (@tickets, $arg) }
elsif (-f $arg) { push (@PARS, $arg); }
else { die "what? arg=[$arg]\n"; }
}
}
push(@PARS, 'identifiers.tsv') unless (@PARS);
foreach my $arg (@PARS)
{
my $r= retrieve_data($arg, \@tickets);
show_results($r);
}
exit(0);
sub retrieve_data
{
my $fnm= shift;
my $tickets= shift;
my %tickets;
if (defined ($tickets))
{
foreach my $t (@$tickets) { $tickets{$t}++ }
}
my @columns= qw(url canonical_url doi identifier);
my @columns= qw(url canonical_url doi identifier ticket);
my $csv= new Util::Simple_CSV (load => $fnm, separator => "\t");
my $data= $csv->{data};
my @results;
foreach my $row (@$data)
{
print __LINE__, " row: ", Dumper($row);
my ($url, $canonical_url, $doi, $identifier)= map { $row->{$_} } @columns;
# print __LINE__, " row: ", Dumper($row);
my ($url, $canonical_url, $doi, $identifier, $ticket)= map { $row->{$_} } @columns;
$doi= $identifier unless ($doi);
$url= $canonical_url unless ($url);
# filter by ticket number
my $use= 0;
if (@tickets)
{
my @t= split(',', $ticket);
foreach my $t (@t) { $use++ if (exists ($tickets{$t})) }
}
else { $use= 1 }
if ($use)
{
my $doi_link= 'https://doi.org/'. $doi;
push (@results, { doi => $doi, url => $url, doi_link => $doi_link } );
}
}
(wantarray) ? @results : \@results;
}
sub show_results
{
my @x= @_;
my @d;
foreach my $x (@x)
{
if (ref($x) eq 'ARRAY') { push (@d, @$x) }
elsif (ref($x) eq '') { push (@d, $x) }
# else ...
}
while (my $d= shift (@d))
{
my ($doi, $url, $doi_link)= map { $d->{$_} } qw(doi url doi_link);
my $msg= "doi=[$doi] url=[$url] doi_link=[$doi_link]";
print __LINE__, ' ', $msg, "\n";
if ($doit)
{
system ('notify-send', $msg);
system ('xdg-open', $doi_link);
<STDIN>;
if (@d)
{
if ($mode eq 'sleep') { sleep($sleep_time) }
elsif ($mode eq 'wait') { <STDIN> }
else { } # complain?
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment