Skip to content

Commit

Permalink
Tidied
Browse files Browse the repository at this point in the history
  • Loading branch information
kthakore committed Sep 9, 2010
1 parent c91394b commit b5cf58e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion t/sdlx_app.t
Expand Up @@ -27,7 +27,7 @@ can_ok(
warp warp
fullscreen fullscreen
iconify iconify
grab_input grab_input
sync sync
attribute attribute
/ /
Expand Down
80 changes: 40 additions & 40 deletions tools/SDLBot.pl
@@ -1,4 +1,5 @@
package MouseImgur; package MouseImgur;

# Imgur.pm # Imgur.pm
# simple perl module for uploading pics to imgur.com # simple perl module for uploading pics to imgur.com


Expand All @@ -8,21 +9,21 @@ package MouseImgur;
use warnings; use warnings;
use Mouse; # a use Mouse instead use Mouse; # a use Mouse instead


has 'key' => (is => 'rw', isa => 'Str'); has 'key' => ( is => 'rw', isa => 'Str' );


# errors: # errors:
# 0 -- no api key # 0 -- no api key
# -1 -- problem with the file # -1 -- problem with the file
sub upload { sub upload {
my $self = shift; my $self = shift;
my $image_path = shift; my $image_path = shift;
return 0 unless($self->key); return 0 unless ( $self->key );
my $res; my $res;
if($image_path =~ /http:\/\//) { if ( $image_path =~ /http:\/\// ) {
$res = $image_path; $res = $image_path;
} else { } else {
my $fh; my $fh;
open $fh,"<", $image_path or return -1; open $fh, "<", $image_path or return -1;
$res = _read_file($image_path); $res = _read_file($image_path);
} }
$res = $self->_upload($res); $res = $self->_upload($res);
Expand All @@ -36,10 +37,10 @@ sub _read_file {
my $fh; my $fh;
open $fh, "<", $fname or return -1; open $fh, "<", $fname or return -1;
binmode $fh; binmode $fh;
return encode_base64(join("" => <$fh>)); return encode_base64( join( "" => <$fh> ) );
} }


# errors: # errors:
# 1000 No image selected # 1000 No image selected
# 1001 Image failed to upload # 1001 Image failed to upload
# 1002 Image larger than 10MB # 1002 Image larger than 10MB
Expand All @@ -49,14 +50,14 @@ sub _read_file {
# 1006 Upload failed during the copy process # 1006 Upload failed during the copy process
# 1007 Upload failed during thumbnail process # 1007 Upload failed during thumbnail process
sub _upload { sub _upload {
my $self = shift; my $self = shift;
my $image = shift; my $image = shift;
return undef unless($image); return undef unless ($image);
my $user_a = LWP::UserAgent->new(agent => "Perl"); my $user_a = LWP::UserAgent->new( agent => "Perl" );
my $res = $user_a->post('http://imgur.com/api/upload.xml', ['image' => $image, 'key' => $self->key]); my $res = $user_a->post( 'http://imgur.com/api/upload.xml', [ 'image' => $image, 'key' => $self->key ] );
if($res->content =~ /<original_image>(.*?)<\/original_image>/) { if ( $res->content =~ /<original_image>(.*?)<\/original_image>/ ) {
return $1; return $1;
} elsif ($res->content =~ /<error_code>(\d+)<\/error_code>/) { } elsif ( $res->content =~ /<error_code>(\d+)<\/error_code>/ ) {
return $1; return $1;
} else { } else {
return -3; return -3;
Expand All @@ -83,15 +84,15 @@ package SDLBot;
my $quite = 0; my $quite = 0;


warn Dumper qx(sh -c "ulimit -a"); warn Dumper qx(sh -c "ulimit -a");

sub said { sub said {
my ( $self, $message ) = @_; my ( $self, $message ) = @_;
if ( $message->{body} =~ /^eval:\s*/ ) if ( $message->{body} =~ /^eval:\s*/ ) {
{

$message->{body} =~ s/^.+?eval://; $message->{body} =~ s/^.+?eval://;
warn 'Got '.$message->{body}; warn 'Got ' . $message->{body};
return eval_imgur( $message->{body} ); return eval_imgur( $message->{body} );

} }
my $return = ticket($message); my $return = ticket($message);
return $return if $return; return $return if $return;
Expand All @@ -118,44 +119,43 @@ sub ticket {


sub eval_imgur { sub eval_imgur {


warn 'eval'; warn 'eval';


return "Can't run $_[0]" if $_[0] =~ /fork|unlink|threads|while/; return "Can't run $_[0]" if $_[0] =~ /fork|unlink|threads|while/;


my $videodriver = $ENV{SDL_VIDEODRIVER}; my $videodriver = $ENV{SDL_VIDEODRIVER};
$ENV{SDL_VIDEODRIVER} = 'dummy'; $ENV{SDL_VIDEODRIVER} = 'dummy';


my $key = '26447813009ded6a7e83986738085949'; my $key = '26447813009ded6a7e83986738085949';


my $imgur = MouseImgur->new(key=>$key); my $imgur = MouseImgur->new( key => $key );


my $app = SDLx::Surface->new( width => 200, height => 200); my $app = SDLx::Surface->new( width => 200, height => 200 );


my $e = eval $_[0]; my $e = eval $_[0];


warn 'Got eval: '.$@; warn 'Got eval: ' . $@;


if( $@ ) if ($@) {
{


$e = $@; $e = $@;
$e .= SDL::get_error(); $e .= SDL::get_error();
return $e; return $e;
} }
my $image = 'foo'.rand(1000).'.bmp'; my $image = 'foo' . rand(1000) . '.bmp';


SDL::Video::save_BMP( $app, $image); SDL::Video::save_BMP( $app, $image );


my $result = $imgur->upload($image); my $result = $imgur->upload($image);


my $r = "Imgur Upload: $result\n"; my $r = "Imgur Upload: $result\n";


unlink $image; unlink $image;


$ENV{SDL_VIDEODRIVER} = $videodriver; $ENV{SDL_VIDEODRIVER} = $videodriver;




return $r; return $r;


} }


Expand Down

0 comments on commit b5cf58e

Please sign in to comment.