| 1 | package Net::Amazon::MechanicalTurk::Command::UpdateHITs; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use IO::File; | ||
| 6 | use Net::Amazon::MechanicalTurk::BulkSupport; | ||
| 7 | use Net::Amazon::MechanicalTurk::DataStructure; | ||
| 8 | use Net::Amazon::MechanicalTurk::RowData; | ||
| 9 | use Net::Amazon::MechanicalTurk::Properties; | ||
| 10 | use Net::Amazon::MechanicalTurk::Template; | ||
| 11 | use Net::Amazon::MechanicalTurk::Template::ReplacementTemplate; | ||
| 12 | use Net::Amazon::MechanicalTurk::DelimitedWriter; | ||
| 13 | |||
| 14 | our $VERSION = '1.00'; | ||
| 15 | |||
| 16 | =head1 NAME | ||
| 17 | |||
| 18 | Net::Amazon::MechanicalTurk::Command::UpdateHITs - Bulk HIT update support for Amazon Mechancial Turk. | ||
| 19 | |||
| 20 | This module adds the updateHITs method to the Net::Amazon::MechanicalTurk class. | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | my $properties = { | ||
| 25 | Title => 'UpdateHITs Perl sample 2', | ||
| 26 | Description => 'This is a test of the bulk updating API.', | ||
| 27 | Keywords => 'UpdateHITs, bulkload, perl', | ||
| 28 | Reward => { | ||
| 29 | CurrencyCode => 'USD', | ||
| 30 | Amount => 0.02 | ||
| 31 | }, | ||
| 32 | RequesterAnnotation => 'test', | ||
| 33 | AssignmentDurationInSeconds => 60 * 60, | ||
| 34 | AutoApprovalDelayInSeconds => 60 * 60 * 10, | ||
| 35 | }; | ||
| 36 | |||
| 37 | my $mturk = Net::Amazon::MechanicalTurk->new; | ||
| 38 | |||
| 39 | $mturk->updateHITs( | ||
| 40 | properties => $properties, | ||
| 41 | input => "updatehits-input.csv", | ||
| 42 | progress => \*STDOUT, | ||
| 43 | success => "updatehits-success.csv", | ||
| 44 | fail => "updatehits-failure.csv" | ||
| 45 | ); | ||
| 46 | |||
| 47 | =head1 C<updateHITs> | ||
| 48 | |||
| 49 | updateHITs | ||
| 50 | |||
| 51 | Bulk updates many hits to have a new hit type in Mechanical Turk. | ||
| 52 | The method takes a set of properties used to create a HITType and its | ||
| 53 | associated HITs. HITIds are pulled from an input source. For each row | ||
| 54 | in the input source, the HIT it identifies is modified to have the | ||
| 55 | properties specified. | ||
| 56 | |||
| 57 | updateHITs takes a hash reference or a hash with the following parameters: | ||
| 58 | |||
| 59 | |||
| 60 | properties - (required) Either a hash reference or the name of a file, | ||
| 61 | containing the properties to use for generating a HITType | ||
| 62 | and the associated HITs. When the properties are read from | ||
| 63 | a file, the method | ||
| 64 | Net::Amazon::MechanicalTurk::Properties->readNestedData is | ||
| 65 | used. | ||
| 66 | |||
| 67 | input - (required) The input source for row data. | ||
| 68 | This parameter may be of the following types: | ||
| 69 | - Net::Amazon::MechanicalTurk::RowData | ||
| 70 | - An array of hashes. | ||
| 71 | (This is internally converted into an object of type: | ||
| 72 | Net::Amazon::MechanicalTurk::RowData::ArrayHashRowData) | ||
| 73 | - A reference to a subroutine. When the updateHITs method | ||
| 74 | asks for row data, the subroutine will be called and | ||
| 75 | passed a subroutine reference, which should be called | ||
| 76 | for every row generated by the input. The generated row | ||
| 77 | should be a hash reference. | ||
| 78 | (This is internally converted into an object of type | ||
| 79 | Net::Amazon::MechanicalTurk::RowData::SubroutineRowData) | ||
| 80 | - The name of a file. The file should be either a CSV or | ||
| 81 | tab delimited file. If the file name ends with '.csv', | ||
| 82 | it will read as a CSV, otherwise it is assumed to be | ||
| 83 | tab delimited. The first row in the file should contain | ||
| 84 | the column names. Each subsequence row becomes a hash | ||
| 85 | reference based on the column names. | ||
| 86 | (This is internally converted into an object of type | ||
| 87 | Net::Amazon::MechanicalTurk::RowData::DelimitedRowData) | ||
| 88 | |||
| 89 | progress - (optional) Used to display progress messages. This | ||
| 90 | parameter may be of the following types: | ||
| 91 | - A subroutine. The subroutine is called with 1 parameter, | ||
| 92 | a message to be displayed. | ||
| 93 | - An IO::Handle. The progress message is written to the | ||
| 94 | handle. | ||
| 95 | |||
| 96 | success - (optional) Used to handle a successfully created hit. This | ||
| 97 | parameter may be of the following types: | ||
| 98 | - A filename. HITId's and HITTypeId's will be written to | ||
| 99 | this file. The file will be in a delimited format, | ||
| 100 | with the first row containing column headers. If the | ||
| 101 | filename ends in ".csv" the file format will be CSV, | ||
| 102 | otherwise it will be tab delimited. | ||
| 103 | - A subroutine. The subroutine is called when a hit is | ||
| 104 | created and passed a hash with the following parameters: | ||
| 105 | - mturk - A handle to the mturk client. | ||
| 106 | - row - The input row the hit was created | ||
| 107 | from. | ||
| 108 | - HITId - The HITId of the updated HIT. | ||
| 109 | - HITTypeId - The HITTypeId the updated HIT was | ||
| 110 | assigned to. | ||
| 111 | |||
| 112 | fail - (optional) Used to handle a hit which failed creation. If | ||
| 113 | this value is not specified and a hit fails creation, an | ||
| 114 | error will be raised. This value may be of the following | ||
| 115 | types: | ||
| 116 | - A filename. The input row will be written back to the | ||
| 117 | file in a delimited format. If the file name ends with | ||
| 118 | ".csv", then the file will be in CSV format, otherwise | ||
| 119 | it will be in a tab delimited format. | ||
| 120 | - A subroutine. The subroutine will be called back with | ||
| 121 | a hash containing the following values: | ||
| 122 | - mturk - A handle to the mturk client. | ||
| 123 | - row - The input row the hit was created | ||
| 124 | from. | ||
| 125 | - HITId - The HITId that was to be updated | ||
| 126 | - HITTypeId - The HITTypeId that the HIT was to | ||
| 127 | be assigned to. | ||
| 128 | - error - The error message associated with | ||
| 129 | the failure. | ||
| 130 | |||
| 131 | maxHits - (optional) If this value is greater than or equal to 0, than | ||
| 132 | at most maxHits will be created. | ||
| 133 | |||
| 134 | =cut | ||
| 135 | |||
| 136 | |||
| 137 | sub updateHITs { | ||
| 138 | my $mturk = shift; | ||
| 139 | my %params = @_; | ||
| 140 | |||
| 141 | foreach my $required (qw{ properties input }) { | ||
| 142 | if (!exists $params{$required}) { | ||
| 143 | Carp::croak("Missing required parameter $required."); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | my $progress = Net::Amazon::MechanicalTurk::BulkSupport::progressBlock($params{progress}); | ||
| 148 | my $success = Net::Amazon::MechanicalTurk::BulkSupport::successBlock($params{success}); | ||
| 149 | my $fail = Net::Amazon::MechanicalTurk::BulkSupport::failBlock($params{fail}); | ||
| 150 | my $maxHits = (exists $params{maxHits}) ? $params{maxHits} : -1; | ||
| 151 | |||
| 152 | if ($progress) { | ||
| 153 | $progress->("--[Initializing] " . scalar localtime() . " ---"); | ||
| 154 | $progress->(" URL: " . $mturk->serviceUrl); | ||
| 155 | $progress->(" Properties: $params{properties}"); | ||
| 156 | $progress->(" Input: $params{input}"); | ||
| 157 | } | ||
| 158 | |||
| 159 | my $properties = Net::Amazon::MechanicalTurk::Properties->toProperties($params{properties}); | ||
| 160 | my $input = Net::Amazon::MechanicalTurk::RowData->toRowData($params{input}); | ||
| 161 | |||
| 162 | my $createHITTypeProperties = Net::Amazon::MechanicalTurk::BulkSupport::getCreateHITTypeProperties($properties); | ||
| 163 | |||
| 164 | my $hitTypeId = -1; | ||
| 165 | my $exitedEach = 0; | ||
| 166 | my $rowNumber = 0; | ||
| 167 | my $hitsLoaded = 0; | ||
| 168 | my $failures = 0; | ||
| 169 | my $start = time(); | ||
| 170 | my $hitIdKey; | ||
| 171 | my $lastHITId; | ||
| 172 | |||
| 173 | eval { | ||
| 174 | |||
| 175 | # Register HITType | ||
| 176 | $progress->("--[Registering HITType] " . scalar localtime() . " ---") if ($progress); | ||
| 177 | $hitTypeId = Net::Amazon::MechanicalTurk::BulkSupport::createHITType($mturk, $createHITTypeProperties, $properties, $progress); | ||
| 178 | $progress->(" Created HITType (HITTypeId: $hitTypeId)") if ($progress); | ||
| 179 | |||
| 180 | $progress->("--[Updating HITs] " . scalar localtime() . " ---") if ($progress); | ||
| 181 | $input->each(sub { | ||
| 182 | my ($_self, $row) = @_; | ||
| 183 | $rowNumber++; | ||
| 184 | |||
| 185 | if ($maxHits >= 0 and $rowNumber > $maxHits) { | ||
| 186 | $exitedEach = 1; | ||
| 187 | die "Exiting each loop"; | ||
| 188 | } | ||
| 189 | |||
| 190 | # Need to find the column name of the HITId key | ||
| 191 | # The loop is for case insensitive matching and ignoring white space. | ||
| 192 | if (!defined $hitIdKey) { | ||
| 193 | while (my ($key,$value) = each %$row) { | ||
| 194 | if ($key =~ /^\s*HITId\s*$/i) { | ||
| 195 | $hitIdKey = $key; | ||
| 196 | last; | ||
| 197 | } | ||
| 198 | } | ||
| 199 | if (!defined $hitIdKey) { | ||
| 200 | Carp::croak("Couldn't find HITId column. Row has the following columns: " . | ||
| 201 | join(", ", sort keys (%$row))); | ||
| 202 | } | ||
| 203 | } | ||
| 204 | |||
| 205 | if (!exists $row->{$hitIdKey}) { | ||
| 206 | Carp::croak("Couldn't find HITId column on row $rowNumber."); | ||
| 207 | } | ||
| 208 | my $hitId = $row->{$hitIdKey}; | ||
| 209 | $hitId =~ s/^\s+//; | ||
| 210 | $hitId =~ s/\s+$//; | ||
| 211 | |||
| 212 | |||
| 213 | eval { | ||
| 214 | my $changeProps = { HITId => $hitId, HITTypeId => $hitTypeId }; | ||
| 215 | $mturk->ChangeHITTypeOfHIT(HITId => $hitId, HITTypeId => $hitTypeId); | ||
| 216 | $progress->(" Updated HITType of HIT $rowNumber (HITId: $lastHITId) to $hitTypeId.") if ($progress); | ||
| 217 | $hitsLoaded++; | ||
| 218 | $success->( | ||
| 219 | mturk => $mturk, | ||
| 220 | row => $row, | ||
| 221 | HITId => $lastHITId, | ||
| 222 | HITTypeId => $hitTypeId | ||
| 223 | ); | ||
| 224 | }; | ||
| 225 | if ($@) { | ||
| 226 | $failures++; | ||
| 227 | $progress->(" $@") if $progress; | ||
| 228 | $fail->( | ||
| 229 | mturk => $mturk, | ||
| 230 | row => $row, | ||
| 231 | HITId => $hitId, | ||
| 232 | HITTypeId => $hitTypeId, | ||
| 233 | error => $@ | ||
| 234 | ); | ||
| 235 | } | ||
| 236 | }); # End each | ||
| 237 | }; | ||
| 238 | if ($@ and !$exitedEach) { | ||
| 239 | my $message = "\nAn error occurred while updating a HIT.\n"; | ||
| 240 | $message .= "\n$@\n"; | ||
| 241 | if ($mturk->response) { | ||
| 242 | if ($mturk->response->errorCode) { | ||
| 243 | $message .= "\nError Code: " . $mturk->response->errorCode . "\n"; | ||
| 244 | $message .= "Error Message: " . $mturk->response->errorMessage . "\n"; | ||
| 245 | } | ||
| 246 | } | ||
| 247 | if ($rowNumber > 0) { | ||
| 248 | $message .= "\nFailed on row $rowNumber in input $params{input}.\n"; | ||
| 249 | } | ||
| 250 | if ($mturk->request) { | ||
| 251 | $message .= "\nLast operation called " . $mturk->request->{Operation} . ".\n"; | ||
| 252 | $message .= "\nDump of call parameters:\n" . | ||
| 253 | Net::Amazon::MechanicalTurk::BulkSupport::formatDataStructure($mturk->request, 4) . "\n"; | ||
| 254 | } | ||
| 255 | if ($mturk->response) { | ||
| 256 | $message .= "\nDump of response:\n" . | ||
| 257 | Net::Amazon::MechanicalTurk::BulkSupport::formatDataStructure($mturk->response->fullResult, 4) . "\n"; | ||
| 258 | } | ||
| 259 | Carp::croak($message); | ||
| 260 | } | ||
| 261 | |||
| 262 | if ($progress) { | ||
| 263 | $progress->(" Failed to update $failures HITs."); | ||
| 264 | $progress->(" Updated $hitsLoaded HITs."); | ||
| 265 | $progress->("--[Done Updating HITs] " . scalar localtime() . " ---"); | ||
| 266 | $progress->(" Total update time: " . (time() - $start) . " seconds."); | ||
| 267 | $progress->(" You may see your HITs here: " . $mturk->getHITTypeURL($hitTypeId)); | ||
| 268 | } | ||
| 269 | |||
| 270 | return { updated => $hitsLoaded, failed => $failures, HITTypeId => $hitTypeId }; | ||
| 271 | } | ||
| 272 | |||
| 273 | return 1; |