-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoch-perl.pl
executable file
·33 lines (31 loc) · 1.06 KB
/
soch-perl.pl
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
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.014;
use LWP::Simple;
use XML::XPath;
use open qw(:utf8 :std);
# Search K-samsök using Perl XML tools:
say "Enter search term:";
my $query = readline();
say "Number of results (max 500)?";
my $number = readline();
chomp for ($query, $number);
die "Not a number!\n" unless ($number =~ /^\d+$/);
die "Max 500 results!\n" unless ($number <= 500);
my $xml = get("http://kulturarvsdata.se/ksamsok/api?method=search&version=1.1&hitsPerPage=$number&x-api=test&query=text=$query");
die "Failed to fetch results.\n" unless (defined $xml);
my $xp = XML::XPath->new($xml);
my $objects = $xp->find('/result/records/record/rdf:RDF/rdf:Description/ns5:presentation/pres:item');
my @results;
for my $node ($objects->get_nodelist()) {
my @fields;
for my $field (qw{organization id type entityUri}) {
push @fields, $xp->find("./pres:$field/text()", $node);
}
next unless @fields;
push @results, join(' 'x12, map {s!/(object|media|fmi)/!/$1/html/!g; $_} @fields);
}
say 'No results returned.' unless @results;
say for sort @results;