Lines 25-46
use C4::Context;
Link Here
|
25 |
use C4::Output qw( output_with_http_headers ); |
25 |
use C4::Output qw( output_with_http_headers ); |
26 |
use Search::Elasticsearch; |
26 |
use Search::Elasticsearch; |
27 |
|
27 |
|
28 |
|
|
|
29 |
my $cgi = CGI->new; |
28 |
my $cgi = CGI->new; |
30 |
|
29 |
|
31 |
# See http://documentation.abes.fr/aideidrefdeveloppeur/index.html#MicroWebBiblio |
30 |
# See http://documentation.abes.fr/aideidrefdeveloppeur/index.html#MicroWebBiblio |
32 |
my $base = 'http://www.idref.fr/services/biblio/'; |
31 |
my $base = 'http://www.idref.fr/services/biblio/'; |
33 |
my $unimarc3 = $cgi->param('ppn'); |
32 |
my $unimarc3 = $cgi->param('ppn'); |
34 |
|
33 |
|
35 |
my $publications = { name => '', ppn => $unimarc3, roles => [] }; |
34 |
my $publications = { name => '', ppn => $unimarc3, roles => [] }; |
36 |
my $send_result = sub { |
35 |
my $send_result = sub { |
37 |
my $json = to_json($publications); |
36 |
my $json = to_json($publications); |
38 |
utf8::encode($json); |
37 |
utf8::encode($json); |
39 |
output_with_http_headers($cgi, undef, $json, 'json'); |
38 |
output_with_http_headers( $cgi, undef, $json, 'json' ); |
40 |
exit; |
39 |
exit; |
41 |
}; |
40 |
}; |
42 |
|
41 |
|
43 |
my $ua = LWP::UserAgent->new; |
42 |
my $ua = LWP::UserAgent->new; |
44 |
my $request = HTTP::Request->new( |
43 |
my $request = HTTP::Request->new( |
45 |
'GET', |
44 |
'GET', |
46 |
$base . $unimarc3 . ".json", |
45 |
$base . $unimarc3 . ".json", |
Lines 49-105
$request->protocol('HTTP/1.1');
Link Here
|
49 |
my $response = $ua->request($request); |
48 |
my $response = $ua->request($request); |
50 |
$send_result->() if not $response->is_success; |
49 |
$send_result->() if not $response->is_success; |
51 |
|
50 |
|
52 |
my $content = Encode::decode("utf8", $response->content); |
51 |
my $content = Encode::decode( "utf8", $response->content ); |
53 |
my $json = from_json($content); |
52 |
my $json = from_json($content); |
54 |
my $result = $json->{sudoc}->{result}; |
53 |
my $result = $json->{sudoc}->{result}; |
55 |
$send_result->() if $result->{countRoles} == 0; |
54 |
$send_result->() if $result->{countRoles} == 0; |
56 |
|
55 |
|
57 |
$publications->{name} = $result->{name}; |
56 |
$publications->{name} = $result->{name}; |
58 |
$result->{role} = [ $result->{role} ] if ref($result->{role}) ne 'ARRAY'; |
57 |
$result->{role} = [ $result->{role} ] if ref( $result->{role} ) ne 'ARRAY'; |
59 |
my $ppn; |
58 |
my $ppn; |
60 |
for my $r (@{$result->{role}}) { |
59 |
for my $r ( @{ $result->{role} } ) { |
61 |
my $role = { |
60 |
my $role = { |
62 |
code => $r->{unimarcCode}, |
61 |
code => $r->{unimarcCode}, |
63 |
label => $r->{roleName}, |
62 |
label => $r->{roleName}, |
64 |
docs => [], |
63 |
docs => [], |
65 |
}; |
64 |
}; |
66 |
$r->{doc} = [ $r->{doc} ] if ref $r->{doc} ne 'ARRAY'; |
65 |
$r->{doc} = [ $r->{doc} ] if ref $r->{doc} ne 'ARRAY'; |
67 |
for my $doc ( @{$r->{doc}} ) { |
66 |
for my $doc ( @{ $r->{doc} } ) { |
68 |
push @{$role->{docs}}, { |
67 |
push @{ $role->{docs} }, { |
69 |
ppn => $doc->{ppn}, |
68 |
ppn => $doc->{ppn}, |
70 |
citation => $doc->{citation}, |
69 |
citation => $doc->{citation}, |
71 |
}; |
70 |
}; |
72 |
push @$ppn, $doc->{ppn}; |
71 |
push @$ppn, $doc->{ppn}; |
73 |
} |
72 |
} |
74 |
push @{$publications->{roles}}, $role; |
73 |
push @{ $publications->{roles} }, $role; |
75 |
} |
74 |
} |
76 |
|
75 |
|
77 |
my $ec = C4::Context->config('elasticsearch'); |
76 |
my $ec = C4::Context->config('elasticsearch'); |
78 |
my $e = Search::Elasticsearch->new( nodes => $ec->{server} ); |
77 |
my $e = Search::Elasticsearch->new( nodes => $ec->{server} ); |
79 |
my $query = { |
78 |
my $query = { |
80 |
index => $ec->{index_name} . '_biblios', |
79 |
index => $ec->{index_name} . '_biblios', |
81 |
body => { |
80 |
body => { |
82 |
_source => ["ppn"], |
81 |
_source => ["ppn"], |
83 |
size => '10000', |
82 |
size => '10000', |
84 |
query => { terms => { ppn => $ppn } } |
83 |
query => { terms => { ppn => $ppn } } |
85 |
} |
84 |
} |
86 |
}; |
85 |
}; |
87 |
my $res = $e->search($query); |
86 |
my $res = $e->search($query); |
88 |
my $hits = $res->{hits}->{hits}; |
87 |
my $hits = $res->{hits}->{hits}; |
89 |
my $ppn_to_bib; |
88 |
my $ppn_to_bib; |
90 |
for my $hit (@$hits) { |
89 |
for my $hit (@$hits) { |
91 |
my $ppn = $hit->{_source}->{ppn}->[0]; |
90 |
my $ppn = $hit->{_source}->{ppn}->[0]; |
92 |
$ppn_to_bib->{$ppn} = $hit->{_id}; |
91 |
$ppn_to_bib->{$ppn} = $hit->{_id}; |
93 |
} |
92 |
} |
94 |
for my $role (@{$publications->{roles}}) { |
93 |
for my $role ( @{ $publications->{roles} } ) { |
95 |
my @docs = @{$role->{docs}}; |
94 |
my @docs = @{ $role->{docs} }; |
96 |
for my $d (@docs) { |
95 |
for my $d (@docs) { |
97 |
my $bib = $ppn_to_bib->{ $d->{ppn} }; |
96 |
my $bib = $ppn_to_bib->{ $d->{ppn} }; |
98 |
$d->{bib} = $bib if $bib; |
97 |
$d->{bib} = $bib if $bib; |
99 |
} |
98 |
} |
100 |
my $key = sub { |
99 |
my $key = sub { |
101 |
my $doc = shift; |
100 |
my $doc = shift; |
102 |
($doc->{bib} ? 'a' : 'b') . $doc->{citation}; |
101 |
( $doc->{bib} ? 'a' : 'b' ) . $doc->{citation}; |
103 |
}; |
102 |
}; |
104 |
@docs = sort { $key->($a) cmp $key->($b) } @docs; |
103 |
@docs = sort { $key->($a) cmp $key->($b) } @docs; |
105 |
$role->{docs} = \@docs; |
104 |
$role->{docs} = \@docs; |
106 |
- |
|
|