| 1 | package Image::Imgur; | ||||||||||||||
| 2 | # Imgur.pm | ||||||||||||||
| 3 | # simple perl module for uploading pics to imgur.com | ||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use MIME::Base64; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use LWP; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use Moose; # a Mouse is fine too | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | |||||||||||||||
| 11 | 1 | has 'key' => (is => 'rw', isa => 'Str'); | |||||||||||||
| 12 | |||||||||||||||
| 13 | # errors: | ||||||||||||||
| 14 | # 0 -- no api key | ||||||||||||||
| 15 | # -1 -- problem with the file | ||||||||||||||
| 16 | 1 | ▶ | sub upload { | ||||||||||||
|
Subroutine
upload: called CC=4 SCAR=16.3
Pod
undocumented
| |||||||||||||||
| 17 | 1 | my $self = shift; | |||||||||||||
| 18 | 1 | my $image_path = shift; | |||||||||||||
| 19 | 1 | ▶ | return 0 unless($self->key); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 20 | 1 | my $res; | |||||||||||||
| 21 | 1 | ▶ | if($image_path =~ /http:\/\//) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 22 | 1 | $res = $image_path; | |||||||||||||
| 23 | } else { | ||||||||||||||
| 24 | 0 | my $fh; | |||||||||||||
| 25 | 0 | ▶ | open $fh,"<", $image_path or return -1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 0 | $res = _read_file($image_path); | |||||||||||||
| 27 | } | ||||||||||||||
| 28 | 1 | $res = $self->_upload($res); | |||||||||||||
| 29 | 1 | return $res; | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | |||||||||||||||
| 33 | # base64'd image | ||||||||||||||
| 34 | 0 | ▶ | sub _read_file { | ||||||||||||
|
Subroutine
_read_file: not called CC=2 SCAR=17.9
| |||||||||||||||
| 35 | 0 | my $fname = shift; | |||||||||||||
| 36 | 0 | my $fh; | |||||||||||||
| 37 | 0 | ▶ | open $fh, "<", $fname or return -1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 38 | 0 | binmode $fh; | |||||||||||||
| 39 | 0 | return encode_base64(join("" => <$fh>)); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | |||||||||||||||
| 42 | # errors: | ||||||||||||||
| 43 | # 1000 No image selected | ||||||||||||||
| 44 | # 1001 Image failed to upload | ||||||||||||||
| 45 | # 1002 Image larger than 10MB | ||||||||||||||
| 46 | # 1003 Invalid image type or URL | ||||||||||||||
| 47 | # 1004 Invalid API Key | ||||||||||||||
| 48 | # 1005 Upload failed during process | ||||||||||||||
| 49 | # 1006 Upload failed during the copy process | ||||||||||||||
| 50 | # 1007 Upload failed during thumbnail process | ||||||||||||||
| 51 | # 1008 Upload limit reached | ||||||||||||||
| 52 | # 1009 Animated GIF is larger than 2MB | ||||||||||||||
| 53 | # 1010 Animated PNG is larger than 2MB | ||||||||||||||
| 54 | # 1011 Invalid URL | ||||||||||||||
| 55 | # 1012 Could not download the image from that URL | ||||||||||||||
| 56 | # 9000 Invalid API request | ||||||||||||||
| 57 | # 9001 Invalid response format | ||||||||||||||
| 58 | # -3 Something is really wrong... | ||||||||||||||
| 59 | # else: image url | ||||||||||||||
| 60 | 1 | ▶ | sub _upload { | ||||||||||||
|
Subroutine
_upload: called CC=4 SCAR=15.2
| |||||||||||||||
| 61 | 1 | my $self = shift; | |||||||||||||
| 62 | 1 | my $image = shift; | |||||||||||||
| 63 | 1 | ▶ | return undef unless($image); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | 1 | my $user_a = LWP::UserAgent->new(agent => "Perl"); | |||||||||||||
| 65 | 1 | my $res = $user_a->post('http://imgur.com/api/upload.xml', ['image' => $image, 'key' => $self->key]); | |||||||||||||
| 66 | 1 | ▶ | if($res->content =~ /<original_image>(.*?)<\/original_image>/) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 67 | 0 | return $1; | |||||||||||||
| 68 | } elsif ($res->content =~ /<error_code>(\d+)<\/error_code>/) { | ||||||||||||||
| 69 | 0 | return $1; | |||||||||||||
| 70 | } else { | ||||||||||||||
| 71 | 1 | return -3; | |||||||||||||
| 72 | } | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 1 | 1; | |||||||||||||
| 76 | __END__ | ||||||||||||||