source: gs3-extensions/mars-src/trunk/perllib/plugins/AMCMetadataJSONPlugin.pm@ 34367

Last change on this file since 34367 was 34367, checked in by davidb, 4 years ago

Now supports https URLs as well

File size: 6.6 KB
Line 
1###########################################################################
2#
3# AMCMetadataJSONPlugin.pm -- A plugin for JSON files resulting from page
4# scrape of artists on the Australian Music Centre
5# website
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Copyright 2016 New Zealand Digital Library Project
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26#
27###########################################################################
28
29package AMCMetadataJSONPlugin;
30
31
32use BaseImporter;
33use MetadataRead;
34
35use strict;
36no strict 'refs';
37use multiread;
38
39use Encode;
40use JSON;
41
42# methods with identical signatures take precedence in the order given in the ISA list.
43sub BEGIN {
44 @AMCMetadataJSONPlugin::ISA = ('MetadataRead', 'BaseImporter');
45}
46
47
48my $arguments = [
49 { 'name' => "process_exp",
50 'desc' => "{BaseImporter.process_exp}",
51 'type' => "regexp",
52 'reqd' => "no",
53 'deft' => &get_default_process_exp() }
54
55];
56
57
58my $options = { 'name' => "AMCMetadataJSONPlugin",
59 'desc' => "{AMCMetadataJSONPlugin.desc}",
60 'abstract' => "no",
61 'inherits' => "yes",
62 'args' => $arguments };
63
64
65sub new
66{
67 my ($class) = shift (@_);
68 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
69 push(@$pluginlist, $class);
70
71 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
72 push(@{$hashArgOptLists->{"OptList"}},$options);
73
74 my $self = new BaseImporter($pluginlist, $inputargs, $hashArgOptLists);
75
76 return bless $self, $class;
77}
78
79
80sub get_default_process_exp
81{
82 return q^(?i)\.json$^;
83}
84
85sub file_block_read {
86 my $self = shift (@_);
87 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
88
89 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
90
91 if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
92 return undef; # can't recognise
93 }
94
95 # set this so we know this is a metadata file - needed for incremental
96 # build
97 # if this file changes, then we need to reimport everything
98 $block_hash->{'metadata_files'}->{$filename_full_path} = 1;
99
100 return 1;
101}
102
103sub metadata_read
104{
105 my $self = shift (@_);
106 my ($pluginfo, $base_dir, $file, $block_hash,
107 $extrametakeys, $extrametadata, $extrametafile,
108 $processor, $gli, $aux) = @_;
109
110 # Read metadata from JSON files
111 my $filename = &util::filename_cat($base_dir, $file);
112
113 if (!-f $filename || !$self->can_process_this_file($filename)) {
114
115 return undef;
116 }
117 print STDERR "\n<Processing n='$file' p='AMCMetadataJSONPlugin'>\n" if ($gli);
118 print STDERR "AMCMetadataJSONPlugin: processing $file\n" if ($self->{'verbosity'}) > 1;
119
120 my $outhandle = $self->{'outhandle'};
121 my $failhandle = $self->{'failhandle'};
122
123 # add the file to the block list so that it won't be processed in read, as we will do all we can with it here
124 $self->block_filename($block_hash,$filename);
125
126
127 # Read the JSON file to get the metadata
128 my $json_file_content;
129 open(JSON_FILE, "$filename");
130 my $json_file_reader = new multiread();
131 $json_file_reader->set_handle('AMCMetadataJSONPlugin::JSON_FILE');
132 $json_file_reader->read_file(\$json_file_content);
133
134 # Would be nice if AMCMetadataJSONPlugin was extended to support a minus
135 # option to choose the character encoding the JSON file is in
136 # For now we will assume it is always in UTF8
137
138 my $json_file_content_bytes = encode('UTF-8', $json_file_content);
139
140 $json_file_content = decode("utf8",$json_file_content_bytes);
141
142 close(JSON_FILE);
143
144 # Split the file into lines and read the first line (contains the metadata names)
145# $json_file_content =~ s/\r/\n/g; # Handle non-Unix line endings
146# $json_file_content =~ s/\n+/\n/g;
147# my @json_file_lines = split(/;/s, $json_file_content);
148
149# my $matching_file = $file;
150# $matching_filename =~ s/\.js/\.{ogg,mp3,wav}/;
151
152
153# my $json_metadata = {};
154
155 print STDERR "**** json_metadata = $json_file_content\n";
156 # my $json_metadata = decode_json $json_file_content;
157 my $json_metadata = JSON->new->utf8->decode($json_file_content);
158
159 # We can't associate any metadata without knowing the file to associate it with
160 my $audio_url = $json_metadata->{"audio_url"};
161 if (!defined $audio_url) {
162 $self->print_error($outhandle, $failhandle, $gli, $filename, "No audio_url metadata in JSON file: $file");
163
164 return -1;
165 }
166
167 my $local_audio_file = $audio_url;
168 $local_audio_file =~ s/^http(s?):\/\/.*\///;
169 my ($amc_id) = ($local_audio_file =~ m/^(.+)(?:\..+?)$/);
170
171 ## print STDERR "***** local audio file = $local_audio_file\n";
172
173 # Set up metadata table based on JSON file read in
174 foreach my $k (keys %$json_metadata) {
175 $json_metadata->{'amc.'.$k} = [ $json_metadata->{$k} ];
176 delete $json_metadata->{$k};
177 }
178 $json_metadata->{'amc.id'} = [ $amc_id ];
179
180# my $json_filename = shift(@$json_trackurl_array);
181# $json_filename =~ s/\"//g; # strip off quotes
182# delete $json_metadata->{"track_url"};
183
184 # Associate the metadata now
185 $local_audio_file = &util::filename_to_regex($local_audio_file);
186
187### print STDERR "**** setting up mapping for: $json_filename\n";
188
189 # Set up mapping from local_audio_file to the created JSON metadata table
190 # so it 'sticks' to the file
191 $extrametadata->{$local_audio_file} = $json_metadata;
192 push(@$extrametakeys, $local_audio_file);
193 # record which file the metadata came from
194 if (!defined $extrametafile->{$local_audio_file}) {
195 $extrametafile->{$local_audio_file} = {};
196 }
197 # maps the file to full path
198 $extrametafile->{$local_audio_file}->{$file} = $filename;
199
200 return 1;
201}
202
203sub print_error
204{
205
206 my $self = shift(@_);
207 my ($outhandle, $failhandle, $gli, $file, $error) = @_;
208
209 print $outhandle "AMCMetadataJSONPlugin Error: $file: $error\n";
210 print $failhandle "AMCMetadataJSONPlugin Error: $file: $error\n";
211 print STDERR "<ProcessingError n='$file' r='$error'/>\n" if ($gli);
212}
2131;
Note: See TracBrowser for help on using the repository browser.