Skip to content

Instantly share code, notes, and snippets.

@NeoCat
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NeoCat/11020320 to your computer and use it in GitHub Desktop.
Save NeoCat/11020320 to your computer and use it in GitHub Desktop.
Submit a source code in the specified language to ideone.com (in private mode) and get executed results
#!/usr/bin/perl
use strict;
use warnings;
use SOAP::Lite;
my $user = '******';
my $pass = '******';
my $client = SOAP::Lite->new(proxy => 'http://ideone.com/api/1/service');
my $arg = shift || 'list';
my $lang_ids;
my $lang_id;
sub lang_list {
my $i = 0;
for (sort {$a <=> $b} keys %$lang_ids) {
printf "%3d: %s\n", $_, $lang_ids->{$_};
}
exit 1;
}
sub get_langs {
my $langs = $client->getLanguages($user, $pass)->result;
die $langs->{'error'} if $langs->{'error'} ne 'OK';
$lang_ids = $langs->{'languages'};
}
sub search_lang {
my $arg = shift;
my $lang = $arg;
$lang =~ s/\(\)//g;
$lang =~ s/([+*?$@%&()\[\]])/\\$1/g;
for my $more (0,1) {
for (sort {$a<=>$b} keys %$lang_ids) {
my $name = $lang_ids->{$_};
if (!$more and ($_ eq $lang or $name eq $arg or
$name =~ /^$lang\s+\(/i or $name =~ /\($lang\)/i) or
$more and ($name =~ /^$lang\s/i or $name =~ /\($lang[\s-]/i)) {
print STDERR "Language: $name\n";
return $_;
}
}
}
return 0;
}
sub read_data {
my $file = shift;
my $f;
if ($file ne '-') {
open $f, "<", $file or die "cannot open file '$file'";
} else {
print STDERR "reading from stdin:\n";
$f = *STDIN;
}
my $data = join '', <$f>;
close $f if $file ne '-';
return $data;
}
sub submit {
my $result = $client->createSubmission(
$user, $pass, $_[1], $_[0], $_[2], 1, 1
)->result;
die $result->{'error'} if $result->{'error'} ne 'OK';
return $result->{'link'};
}
sub check_status {
my $result = $client->getSubmissionStatus($user, $pass, $_[0])->result;
die $result->{'error'} if $result->{'error'} ne 'OK';
my $status = $result->{'status'};
my $msg = '???';
if ($status < 0) {
$msg = "waiting ...";
} elsif ($status == 0) {
$msg = "done!";
} elsif ($status == 1) {
$msg = "compiling ...";
} elsif ($status == 3) {
$msg = "running ...";
}
return $status, $msg;
}
sub get_result {
my $result = $client->getSubmissionDetails(
$user, $pass, $_[0], 0, 0, 1, 1, 1
)->result;
die $result->{'error'} if $result->{'error'} ne 'OK';
return $result;
}
sub result_str {
my $result = shift;
my $code = $result->{'result'};
my %result_str = (
0 => 'OK',
11 => 'compilation error',
12 => 'runtime error',
13 => 'time limit exceeded',
15 => 'success',
17 => 'memory limit exceeded',
19 => 'illegal system call',
20 => 'internal error',
);
return $result_str{$code} || 'unknown result';
}
sub print_result {
my $result = shift;
print STDERR "--- output ---\n";
print $result->{'output'};
$|=1; $|=0; #flush
print STDERR "--- stderr ---\n";
print STDERR $result->{'stderr'};
print STDERR "--- cmpinfo ---\n";
print STDERR $result->{'cmpinfo'} || '';
}
print STDERR "Getting languages ...";
get_langs();
print STDERR "\r";
lang_list() if $arg eq 'list';
$lang_id = search_lang($arg);
lang_list() unless $lang_id;
my $file = shift or die "no source file is specified\n";
my $input_file = shift;
my $source = read_data($file);
my $input = $input_file && read_data($input_file);
print STDERR "Submitting ...";
my $link = submit($lang_id, $source, $input);
my $status = -1;
my $msg;
do {
sleep 3;
print STDERR "\rGetting status ... ";
($status, $msg) = check_status($link);
printf STDERR "\r%-20s", $msg;
} while $status;
my $result = get_result($link);
my $code = $result->{'result'};
my $result_str = result_str($result);
print STDERR "[$code: $result_str]\n";
print_result($result);
exit($code == 0 || $code == 15 ? 0 : $code);
@NeoCat
Copy link
Author

NeoCat commented Apr 18, 2014

Usage:

ideone.pl <language> <source file> [<input file>]

If or is "-", read from stdin.

Example

% echo 123$'\n'456 | ideone.pl ruby add.rb -
Language: Ruby (ruby-1.9.3)
reading from stdin:
done!               [15: success]
--- output ---
579
--- stderr  ---
--- cmpinfo ---

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment