| 1 | package WWW::Pastebin::Sprunge::Retrieve; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | # ABSTRACT: retrieves pastes from the sprunge.us pastebin | ||
| 5 | our $VERSION = '0.009'; # VERSION | ||
| 6 | use URI; | ||
| 7 | use Carp; | ||
| 8 | use LWP::UserAgent; | ||
| 9 | use Encode; | ||
| 10 | use base 'Class::Data::Accessor'; | ||
| 11 | __PACKAGE__->mk_classaccessors(qw( | ||
| 12 | ua | ||
| 13 | uri | ||
| 14 | id | ||
| 15 | content | ||
| 16 | error | ||
| 17 | results | ||
| 18 | )); | ||
| 19 | |||
| 20 | use overload q|""| => sub { shift->content }; | ||
| 21 | |||
| 22 | |||
| 23 | |||
| 24 | sub new { | ||
| 25 | my $class = shift; | ||
| 26 | croak 'new() takes an even number of arguments' if @_ & 1; | ||
| 27 | |||
| 28 | my %args = @_; | ||
| 29 | $args{ +lc } = delete $args{ $_ } for keys %args; | ||
| 30 | |||
| 31 | $args{timeout} ||= 30; | ||
| 32 | $args{ua} ||= LWP::UserAgent->new( | ||
| 33 | timeout => $args{timeout}, | ||
| 34 | agent => 'WWW::Pastebin::Sprunge (+http://p3rl.org/WWW::Pastebin::Sprunge)', | ||
| 35 | ); | ||
| 36 | |||
| 37 | my $self = bless {}, $class; | ||
| 38 | $self->ua( $args{ua} ); | ||
| 39 | |||
| 40 | return $self; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | sub retrieve { | ||
| 45 | my $self = shift; | ||
| 46 | my $id = shift; | ||
| 47 | |||
| 48 | $self->$_(undef) for qw( error uri id results ); | ||
| 49 | |||
| 50 | return $self->_set_error('Missing or empty paste ID/URL') | ||
| 51 | unless $id; | ||
| 52 | |||
| 53 | (my $uri, $id) = $self->_make_uri_and_id($id, @_) or return; | ||
| 54 | |||
| 55 | $self->uri($uri); | ||
| 56 | $self->id($id); | ||
| 57 | |||
| 58 | my $ua = $self->ua; | ||
| 59 | my $response = $ua->get($uri); | ||
| 60 | if ($response->is_success) { | ||
| 61 | return $self->_get_was_successful($response->content); | ||
| 62 | } | ||
| 63 | else { | ||
| 64 | return $self->_set_error('Network error: ' . $response->status_line); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _get_was_successful { | ||
| 69 | my $self = shift; | ||
| 70 | my $content = shift; | ||
| 71 | |||
| 72 | return $self->results( $self->_parse($content) ); | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _set_error { | ||
| 76 | my $self = shift; | ||
| 77 | my $err_or_res = shift; | ||
| 78 | my $is_net_error = shift; | ||
| 79 | |||
| 80 | if (defined $is_net_error) { | ||
| 81 | $self->error('Network error: ' . $err_or_res->status_line); | ||
| 82 | } | ||
| 83 | else { | ||
| 84 | $self->error($err_or_res); | ||
| 85 | } | ||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub _make_uri_and_id { | ||
| 90 | my $self = shift; | ||
| 91 | my $in = shift; | ||
| 92 | |||
| 93 | my $id; | ||
| 94 | if ( $in =~ m{ (?:http://)? (?:www\.)? sprunge.us/ (\S+?) (?:\?\w+)? $}ix ) { | ||
| 95 | $id = $1; | ||
| 96 | } | ||
| 97 | $id = $in unless defined $id; | ||
| 98 | |||
| 99 | return ( URI->new("http://sprunge.us/$id"), $id ); | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _parse { | ||
| 103 | my $self = shift; | ||
| 104 | my $content = shift; | ||
| 105 | my $id = $self->id; | ||
| 106 | |||
| 107 | if (!defined($content) or !length($content)) { | ||
| 108 | return $self->_set_error('Nothing to parse (empty document retrieved)'); | ||
| 109 | } | ||
| 110 | elsif ($content eq "$id not found") { | ||
| 111 | return $self->_set_error('No such paste'); | ||
| 112 | } | ||
| 113 | else { | ||
| 114 | $self->results(decode_utf8($content)); | ||
| 115 | return $self->content(decode_utf8($content)); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | sub content { | ||
| 122 | my $self = shift; | ||
| 123 | |||
| 124 | return $self->results; | ||
| 125 | } | ||
| 126 | |||
| 127 | 1; | ||
| 128 | |||
| 129 | __END__ |