Batch Results Retrieval in Perl

From Textserver wiki
Revision as of 12:28, 21 January 2016 by Padro (Talk | contribs)

Jump to: navigation, search
 1 #! /usr/bin/perl
 2 
 3 ########################################################################
 4 #
 5 #  Example client to submit a batch request to TextServer 
 6 # SERVICENAME service, and wait for the job to be finished.
 7 #
 8 #  Input must be a ZIP file containing one or more text files to analyze
 9 #  Output will be a ZIP file containing results for each input file in
10 # the requested format (XML, json, conll)
11 #
12 ########################################################################
13 
14 use strict;
15 
16 # import needeed libraries
17 use HTTP::Request::Common;
18 use LWP::UserAgent;
19 
20 print "TextServer username: "; 
21 my $user=<>; chomp $user;
22 print "TextServer Job Token ID: "; 
23 my $tkid=<>; chomp $tkid;
24 print "Output ZIP file: ";
25 my $outfname=<>; chomp $outfname;
26 
27 # URL for the requested service
28 my $TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws";
29 
30 # create user agent
31 my $ua = LWP::UserAgent->new;
32 
33 # send request to check for job termination
34 print "Polling server for job completion...\n";
35 my $resp = $ua->post($TextServer_URL."/resultRetrieve",
36                   Content_Type => 'form-data',
37                   Content  => [username => $user, 
38                                tokenID => $tkid
39                               ] 
40                  );
41     
42 if ($resp->code==503 && $resp->decoded_content =~ /^\[TS\-125\]/ ) {
43    # http status 503, textserver code TS-125 means the job is not finished yet.
44    print "Job not finished yet\n"; 
45    exit();
46 }
47 
48 elsif (!$resp->is_success) { 
49    # some other error happened, report it.
50     die $resp->status_line." - ".$resp->decoded_content."\n";
51 }
52 
53 
54 # if status=200, we a got ZIP response, the job is done.
55 # print zipped respose to output file 
56 open(my $outf, '>', $outfname);
57 print $outf $resp->decoded_content;
58 close $outf;
59 print "Job finished. Results saved to ".$outfname."\n";