« blib/htdocs/example_scripts/gbrowse_upload_data.pl
↑ Summary
blib/htdocs/tutorial/bin/expression_levels.pl »
| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use LWP::UserAgent; | ||
| 5 | use HTTP::Request::Common; | ||
| 6 | use File::Basename 'basename'; | ||
| 7 | use JSON; | ||
| 8 | |||
| 9 | @ARGV == 2 or die "usage: $0 <gbrowse_url> <file> [session_id]"; | ||
| 10 | my $gbrowse = shift; | ||
| 11 | my $file = shift; | ||
| 12 | my $id = shift; | ||
| 13 | |||
| 14 | -e $file && -r $file or die "Can't read file $file"; | ||
| 15 | my $filename = basename($file); | ||
| 16 | |||
| 17 | my $agent = LWP::UserAgent->new; | ||
| 18 | $agent->cookie_jar({file=>"$ENV{HOME}/.gbrowse_session", | ||
| 19 | autosave=>1 | ||
| 20 | }); | ||
| 21 | my $url = $gbrowse; | ||
| 22 | $url .= '/' unless $url =~ m!/$/!; | ||
| 23 | |||
| 24 | my @id = (id => $id) if $id; | ||
| 25 | my $request = POST($url, | ||
| 26 | Content_Type => 'form-data', | ||
| 27 | Content => [ | ||
| 28 | action => 'upload_file', | ||
| 29 | overwrite => 1, | ||
| 30 | file => [$file,$filename], | ||
| 31 | @id | ||
| 32 | ] | ||
| 33 | ); | ||
| 34 | my $response = $agent->request($request); | ||
| 35 | die $response->status_line unless $response->is_success; | ||
| 36 | my $content = $response->decoded_content; | ||
| 37 | my $cookie = $response->header('Set-cookie'); | ||
| 38 | my ($used_id) = $cookie =~ /gbrowse_sess=([a-f0-9]+)/; | ||
| 39 | |||
| 40 | my $struct = from_json($content); | ||
| 41 | |||
| 42 | if ($struct->{success}) { | ||
| 43 | print <<END; | ||
| 44 | Tracks : @{$struct->{tracks}} | ||
| 45 | uploadName : $struct->{uploadName} | ||
| 46 | sessionID : $used_id | ||
| 47 | END | ||
| 48 | } else { | ||
| 49 | die $struct->{error_msg}; | ||
| 50 | } |