source: main/trunk/model-cols/waikato-campus-view/perllib/plugins/PhotafMetadataJSONPlugin.pm@ 26888

Last change on this file since 26888 was 26888, checked in by davidb, 11 years ago

Panorama based collection of the University of Waikato campus

File size: 5.8 KB
Line 
1###########################################################################
2#
3# PhotafMetadataJSONPlugin.pm -- A plugin for audio structured JSON files
4#
5# A component of the Greenstone digital library software
6# from the New Zealand Digital Library Project at the
7# University of Waikato, New Zealand.
8#
9# Copyright 2013 New Zealand Digital Library Project
10#
11# This program is free software; you can redistribute it and/or modify
12# it under the terms of the GNU General Public License as published by
13# the Free Software Foundation; either version 2 of the License, or
14# (at your option) any later version.
15#
16# This program is distributed in the hope that it will be useful,
17# but WITHOUT ANY WARRANTY; without even the implied warranty of
18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19# GNU General Public License for more details.
20#
21# You should have received a copy of the GNU General Public License
22# along with this program; if not, write to the Free Software
23# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24#
25###########################################################################
26
27package PhotafMetadataJSONPlugin;
28
29
30use BasePlugin;
31use MetadataRead;
32
33use strict;
34no strict 'refs';
35use multiread;
36
37use Encode;
38use JSON;
39
40# methods with identical signatures take precedence in the order given in the ISA list.
41sub BEGIN {
42 @PhotafMetadataJSONPlugin::ISA = ('MetadataRead', 'BasePlugin');
43}
44
45
46my $arguments = [
47 { 'name' => "process_exp",
48 'desc' => "{BasePlugin.process_exp}",
49 'type' => "regexp",
50 'reqd' => "no",
51 'deft' => &get_default_process_exp() }
52
53];
54
55
56my $options = { 'name' => "PhotafMetadataJSONPlugin",
57 'desc' => "{PhotafMetadataJSONPlugin.desc}",
58 'abstract' => "no",
59 'inherits' => "yes",
60 'args' => $arguments };
61
62
63sub new
64{
65 my ($class) = shift (@_);
66 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
67 push(@$pluginlist, $class);
68
69 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
70 push(@{$hashArgOptLists->{"OptList"}},$options);
71
72 my $self = new BasePlugin($pluginlist, $inputargs, $hashArgOptLists);
73
74 return bless $self, $class;
75}
76
77
78sub get_default_process_exp
79{
80 return q^(?i)PhotafPanoramaConfig\.cfg$^;
81}
82
83sub file_block_read {
84 my $self = shift (@_);
85 my ($pluginfo, $base_dir, $file, $block_hash, $metadata, $gli) = @_;
86
87 my ($filename_full_path, $filename_no_path) = &util::get_full_filenames($base_dir, $file);
88
89 if (!-f $filename_full_path || !$self->can_process_this_file($filename_full_path)) {
90 return undef; # can't recognise
91 }
92
93 # set this so we know this is a metadata file - needed for incremental
94 # build
95 # if this file changes, then we need to reimport everything
96 $block_hash->{'metadata_files'}->{$filename_full_path} = 1;
97
98 return 1;
99}
100
101sub metadata_read
102{
103 my $self = shift (@_);
104 my ($pluginfo, $base_dir, $file, $block_hash,
105 $extrametakeys, $extrametadata, $extrametafile,
106 $processor, $gli, $aux) = @_;
107
108 # Read metadata from JSON files
109 my $filename = &util::filename_cat($base_dir, $file);
110
111# if ($filename !~ /\.js$/ || !-f $filename) {
112
113 if (!-f $filename || !$self->can_process_this_file($filename)) {
114
115 return undef;
116 }
117 print STDERR "\n<Processing n='$file' p='PhotafMetadataJSONPlugin'>\n" if ($gli);
118 print STDERR "PhotafMetadataJSONPlugin: 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 &util::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('PhotafMetadataJSONPlugin::JSON_FILE');
132 $json_file_reader->read_file(\$json_file_content);
133
134 # Would be nice if PhotafMetadataJSONPlugin 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 $json_file_content = decode("utf8",$json_file_content);
138
139 close(JSON_FILE);
140
141 # Split the file into lines and read the first line (contains the metadata names)
142 $json_file_content =~ s/\r/\n/g; # Handle non-Unix line endings
143 $json_file_content =~ s/\n+/\n/g;
144# my @json_file_lines = split(/;/s, $json_file_content);
145
146# my $matching_file = $file;
147# $matching_filename =~ s/\.js/\.{ogg,mp3,wav}/;
148
149
150 my $json_metadata = {};
151
152 $json_file_content =~ s/^\{//;
153 $json_file_content =~ s/\}$//;
154
155 my @json_pairs_conjoined = split(/,/,$json_file_content);
156
157
158 # Read each line of the file and assign the metadata appropriately
159 foreach my $json_pair_str (@json_pairs_conjoined) {
160
161
162 my ($metaname,$metaval) = ($json_pair_str =~ m/\"(.+?)\":\"?(.+?)\"?$/);
163
164 $metaname =~ s/^m_//;
165 $json_metadata->{$metaname} = [$metaval];
166 $json_metadata->{"photaf.".$metaname} = [$metaval];
167
168 }
169
170 # Associate the metadata with the companion jpg image
171 my $json_filename = "PhotafPanoramaPicHD.jpg";
172 $json_filename = &util::filename_to_regex($json_filename);
173
174 $extrametadata->{$json_filename} = $json_metadata;
175 push(@$extrametakeys, $json_filename);
176
177 # record which file the metadata came from
178 if (!defined $extrametafile->{$json_filename}) {
179 $extrametafile->{$json_filename} = {};
180 }
181 # maps the file to full path
182 $extrametafile->{$json_filename}->{$file} = $filename;
183
184 return 1;
185}
186
187sub print_error
188{
189 my $self = shift(@_);
190 my ($outhandle, $failhandle, $gli, $file, $error) = @_;
191
192 print $outhandle "PhotafMetadataJSONPlugin Error: $file: $error\n";
193 print $failhandle "PhotafMetadataJSONPlugin Error: $file: $error\n";
194 print STDERR "<ProcessingError n='$file' r='$error'/>\n" if ($gli);
195}
1961;
Note: See TracBrowser for help on using the repository browser.