Difference between revisions of "Batch Results Retrieval in Perl"
From Textserver wiki
(Created page with "<syntaxhighlight lang="perl" line="1" > </syntaxhighlight>") |
|||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
<syntaxhighlight lang="perl" line="1" > | <syntaxhighlight lang="perl" line="1" > | ||
+ | #! /usr/bin/perl | ||
+ | |||
+ | ######################################################################## | ||
+ | # | ||
+ | # Example client to retrieve the results of a batch request to TextServer | ||
+ | # | ||
+ | # 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> |
Latest revision as of 12:31, 21 January 2016
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";