Difference between revisions of "Batch Results Retrieval in Perl"
From Textserver wiki
(Created page with "<syntaxhighlight lang="perl" line="1" > </syntaxhighlight>") |
|||
Line 1: | Line 1: | ||
<syntaxhighlight lang="perl" line="1" > | <syntaxhighlight lang="perl" line="1" > | ||
+ | #! /usr/bin/perl | ||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # Example client to submit a batch request to TextServer | ||
+ | # SERVICENAME service, and wait for the job to be finished. | ||
+ | # | ||
+ | # Input must be a ZIP file containing one or more text files to analyze | ||
+ | # Output will be a ZIP file containing results for each input file in | ||
+ | # the requested format (XML, json, conll) | ||
+ | # | ||
+ | ######################################################################## | ||
+ | |||
+ | use strict; | ||
+ | |||
+ | # import needeed libraries | ||
+ | use HTTP::Request::Common; | ||
+ | use LWP::UserAgent; | ||
+ | |||
+ | print "TextServer username: "; | ||
+ | my $user=<>; chomp $user; | ||
+ | print "TextServer Job Token ID: "; | ||
+ | my $tkid=<>; chomp $tkid; | ||
+ | print "Output ZIP file: "; | ||
+ | my $outfname=<>; chomp $outfname; | ||
+ | |||
+ | # URL for the requested service | ||
+ | my $TextServer_URL = "http://frodo.lsi.upc.edu:8080/TextWS/textservlet/ws"; | ||
+ | |||
+ | # create user agent | ||
+ | my $ua = LWP::UserAgent->new; | ||
+ | |||
+ | # send request to check for job termination | ||
+ | print "Polling server for job completion...\n"; | ||
+ | my $resp = $ua->post($TextServer_URL."/resultRetrieve", | ||
+ | Content_Type => 'form-data', | ||
+ | Content => [username => $user, | ||
+ | tokenID => $tkid | ||
+ | ] | ||
+ | ); | ||
+ | |||
+ | if ($resp->code==503 && $resp->decoded_content =~ /^\[TS\-125\]/ ) { | ||
+ | # http status 503, textserver code TS-125 means the job is not finished yet. | ||
+ | print "Job not finished yet\n"; | ||
+ | exit(); | ||
+ | } | ||
+ | |||
+ | elsif (!$resp->is_success) { | ||
+ | # some other error happened, report it. | ||
+ | die $resp->status_line." - ".$resp->decoded_content."\n"; | ||
+ | } | ||
+ | |||
+ | |||
+ | # if status=200, we a got ZIP response, the job is done. | ||
+ | # print zipped respose to output file | ||
+ | open(my $outf, '>', $outfname); | ||
+ | print $outf $resp->decoded_content; | ||
+ | close $outf; | ||
+ | print "Job finished. Results saved to ".$outfname."\n"; | ||
</syntaxhighlight> | </syntaxhighlight> |
Revision as of 12:28, 21 January 2016
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";