Batch Results Retrieval in Perl

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

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