| 1 | package POE::Component::IRC::Plugin::ImageMirror; | ||
| 2 | 2 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | $POE::Component::IRC::Plugin::ImageMirror::AUTHORITY = 'cpan:HINRIK'; | |
| 4 | } | ||
| 5 | 2 | ▶ | BEGIN { |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 2 | $POE::Component::IRC::Plugin::ImageMirror::VERSION = '0.15'; | |
| 7 | } | ||
| 8 | |||
| 9 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 2 | ▶ | use HTTP::Cookies; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 2 | ▶ | use HTTP::Headers; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use Encode qw(is_utf8); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 2 | ▶ | use List::Util qw(first); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 2 | ▶ | use POE; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 2 | ▶ | use POE::Component::IRC::Common qw(irc_to_utf8); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | 2 | ▶ | use POE::Component::IRC::Plugin qw(PCI_EAT_NONE PCI_EAT_PLUGIN); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 2 | ▶ | use POE::Component::IRC::Plugin::URI::Find; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 2 | ▶ | use POE::Quickie; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 2 | ▶ | use URI::Title qw(title); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 0 | ▶ | use Image::ImageShack; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 22 | use Try::Tiny; | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my ($package, %args) = @_; | ||
| 26 | my $self = bless \%args, $package; | ||
| 27 | |||
| 28 | # defaults | ||
| 29 | $self->{useragent} = | ||
| 30 | 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b3pre) Gecko/2008020108' | ||
| 31 | if !defined $self->{useragent}; | ||
| 32 | |||
| 33 | $self->_text_to_regex(); | ||
| 34 | $self->{URI_match} = [qr/(?i:jpe?g|gif|png)$/] if !$self->{URI_match}; | ||
| 35 | $self->{URI_title} = 1 if !defined $self->{URI_title}; | ||
| 36 | $self->{Method} = 'notice' if !defined $self->{Method}; | ||
| 37 | $self->{req} = []; | ||
| 38 | |||
| 39 | return $self; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub _text_to_regex { | ||
| 43 | my ($self) = @_; | ||
| 44 | |||
| 45 | no re 'eval'; | ||
| 46 | if ($self->{URI_match}) { | ||
| 47 | for my $elem (@{ $self->{URI_match} }) { | ||
| 48 | $elem = qr/$elem/ if !ref $elem; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | while (my ($key, $value) = each %{ $self->{URI_subst} }) { | ||
| 53 | if (!ref $key) { | ||
| 54 | delete $self->{URI_subst}{$key}; | ||
| 55 | $self->{URI_subst}{qr/$key/} = $value; | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | sub PCI_register { | ||
| 63 | my ($self, $irc) = @_; | ||
| 64 | |||
| 65 | if ( !grep { $_->isa('POE::Component::IRC::Plugin::URI::Find') } values %{ $irc->plugin_list() } ) { | ||
| 66 | $irc->plugin_add('URIFind', POE::Component::IRC::Plugin::URI::Find->new()); | ||
| 67 | } | ||
| 68 | |||
| 69 | $self->{irc} = $irc; | ||
| 70 | POE::Session->create( | ||
| 71 | object_states => [ | ||
| 72 | $self => [qw( | ||
| 73 | _start | ||
| 74 | _sig_DIE | ||
| 75 | _process_uri | ||
| 76 | _mirror_imgur | ||
| 77 | _mirror_imgshack | ||
| 78 | _mirrored | ||
| 79 | _post_uri | ||
| 80 | )], | ||
| 81 | ], | ||
| 82 | ); | ||
| 83 | |||
| 84 | $irc->plugin_register($self, 'SERVER', qw(urifind_uri)); | ||
| 85 | return 1; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub PCI_unregister { | ||
| 89 | my ($self, $irc) = @_; | ||
| 90 | $poe_kernel->refcount_decrement($self->{session_id}, __PACKAGE__); | ||
| 91 | return 1; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub _start { | ||
| 95 | my ($kernel, $self, $session) = @_[KERNEL, OBJECT, SESSION]; | ||
| 96 | $self->{session_id} = $session->ID(); | ||
| 97 | $kernel->sig(DIE => '_sig_DIE'); | ||
| 98 | $kernel->refcount_increment($self->{session_id}, __PACKAGE__); | ||
| 99 | return; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub _sig_DIE { | ||
| 103 | my ($kernel, $self, $ex) = @_[KERNEL, OBJECT, ARG1]; | ||
| 104 | chomp $ex->{error_str}; | ||
| 105 | warn "Error: Event $ex->{event} in $ex->{dest_session} raised exception:\n"; | ||
| 106 | warn " $ex->{error_str}\n"; | ||
| 107 | $kernel->sig_handled(); | ||
| 108 | return; | ||
| 109 | } | ||
| 110 | |||
| 111 | sub S_urifind_uri { | ||
| 112 | my ($self, $irc) = splice @_, 0, 2; | ||
| 113 | my $where = ${ $_[1] }; | ||
| 114 | my $uri = ${ $_[2] }; | ||
| 115 | |||
| 116 | return PCI_EAT_NONE if $self->_ignoring_channel($where); | ||
| 117 | |||
| 118 | my $matched; | ||
| 119 | for my $match (@{ $self->{URI_match} }) { | ||
| 120 | $matched = 1 if $uri =~ $match; | ||
| 121 | last if $matched; | ||
| 122 | } | ||
| 123 | return PCI_EAT_NONE if !$matched; | ||
| 124 | |||
| 125 | if ($self->{URI_subst}) { | ||
| 126 | while (my ($regex, $subst) = each %{ $self->{URI_subst} }) { | ||
| 127 | $uri =~ s/$regex/$subst/; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | $poe_kernel->call($self->{session_id}, '_process_uri', $where, $uri); | ||
| 132 | |||
| 133 | return $self->{Eat} | ||
| 134 | ? PCI_EAT_PLUGIN | ||
| 135 | : PCI_EAT_NONE; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub _ignoring_channel { | ||
| 139 | my ($self, $chan) = @_; | ||
| 140 | |||
| 141 | if ($self->{Channels}) { | ||
| 142 | return 1 if !first { | ||
| 143 | my $c = $chan; | ||
| 144 | $c = irc_to_utf8($c) if is_utf8($_); | ||
| 145 | $_ eq $c | ||
| 146 | } @{ $self->{Channels} }; | ||
| 147 | } | ||
| 148 | return; | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _process_uri { | ||
| 152 | my ($kernel, $self, $sender, $where, $uri) = | ||
| 153 | @_[KERNEL, OBJECT, SENDER, ARG0, ARG1]; | ||
| 154 | |||
| 155 | $kernel->refcount_increment($sender->ID, __PACKAGE__); | ||
| 156 | |||
| 157 | my $req = { | ||
| 158 | sender => $sender->ID, | ||
| 159 | where => $where, | ||
| 160 | orig_uri => $uri, | ||
| 161 | }; | ||
| 162 | push @{ $self->{req} }, $req; | ||
| 163 | |||
| 164 | |||
| 165 | if ($self->{URI_title}) { | ||
| 166 | my $title; | ||
| 167 | for (1..3) { | ||
| 168 | ($title) = quickie(sub { print title($uri), "\n" }); | ||
| 169 | chomp $title; | ||
| 170 | last if length $title; | ||
| 171 | } | ||
| 172 | $req->{title} = $title; | ||
| 173 | } | ||
| 174 | |||
| 175 | POE::Quickie->run( | ||
| 176 | Program => sub { | ||
| 177 | _mirror_imgur($self->{Imgur_user}, $self->{Imgur_pass}, $uri); | ||
| 178 | }, | ||
| 179 | Context => [$req, '0_imgur'], | ||
| 180 | StdoutEvent => '_mirrored', | ||
| 181 | ); | ||
| 182 | |||
| 183 | POE::Quickie->run( | ||
| 184 | Program => sub { _mirror_imgshack($self->{useragent}, $uri) }, | ||
| 185 | Context => [$req, '1_imgshack'], | ||
| 186 | StdoutEvent => '_mirrored', | ||
| 187 | ); | ||
| 188 | |||
| 189 | return; | ||
| 190 | } | ||
| 191 | |||
| 192 | sub _mirror_imgur { | ||
| 193 | my ($imgur_user, $imgur_pass, $orig_uri) = @_; | ||
| 194 | |||
| 195 | my $ua = LWP::UserAgent->new( | ||
| 196 | cookie_jar => HTTP::Cookies->new, | ||
| 197 | requests_redirectable => [qw(GET HEAD POST)], | ||
| 198 | ); | ||
| 199 | |||
| 200 | $ua->post( | ||
| 201 | 'http://imgur.com/signin', | ||
| 202 | { | ||
| 203 | username => $imgur_user, | ||
| 204 | password => $imgur_pass, | ||
| 205 | submit => '', | ||
| 206 | }, | ||
| 207 | ); | ||
| 208 | |||
| 209 | my $imgur = ''; | ||
| 210 | |||
| 211 | TRY: for (1..3) { | ||
| 212 | my $res = $ua->get("http://imgur.com/api/upload/?url=$orig_uri"); | ||
| 213 | |||
| 214 | if ($res->is_success) { | ||
| 215 | if (my ($uri) = $res->content =~ m{id="direct"\s+value="(.*?)"}) { | ||
| 216 | $imgur = $uri; | ||
| 217 | last TRY; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | } | ||
| 221 | |||
| 222 | print $imgur, "\n"; | ||
| 223 | return; | ||
| 224 | |||
| 225 | } | ||
| 226 | |||
| 227 | sub _mirror_imgshack { | ||
| 228 | my ($useragent, $orig_uri) = @_; | ||
| 229 | |||
| 230 | my $ua = LWP::UserAgent->new( | ||
| 231 | cookie_jar => HTTP::Cookies->new, | ||
| 232 | requests_redirectable => [qw(GET HEAD POST)], | ||
| 233 | ua => $useragent, | ||
| 234 | default_header => HTTP::Headers->new( | ||
| 235 | Referer => 'http://imageshack.us/', | ||
| 236 | ), | ||
| 237 | ); | ||
| 238 | my $ishack = Image::ImageShack->new; | ||
| 239 | |||
| 240 | my $imgshack = ''; | ||
| 241 | |||
| 242 | TRY: for (1..3) { | ||
| 243 | try { | ||
| 244 | my $url = $ishack->host($orig_uri); | ||
| 245 | |||
| 246 | # Try to get the big version | ||
| 247 | my $res = $ua->get($url); | ||
| 248 | if ($res->is_success) { | ||
| 249 | if (my ($big_url) = $res->content =~ m{<meta property="og:image" content="(.*?)"}) { | ||
| 250 | $imgshack = $big_url; | ||
| 251 | last TRY; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | }; | ||
| 255 | } | ||
| 256 | |||
| 257 | print $imgshack, "\n"; | ||
| 258 | return; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub _mirrored { | ||
| 262 | my ($kernel, $self, $uri, $context) = @_[KERNEL, OBJECT, ARG0, ARG2]; | ||
| 263 | |||
| 264 | my ($req, $mirror) = @$context; | ||
| 265 | $req->{mirrored}{$mirror} = $uri; | ||
| 266 | |||
| 267 | while (@{ $self->{req} } | ||
| 268 | && $self->{req}[0]{mirrored} | ||
| 269 | && keys %{ $self->{req}[0]{mirrored} } == 2) { | ||
| 270 | my $request = shift @{ $self->{req} }; | ||
| 271 | $kernel->yield(_post_uri => $request); | ||
| 272 | } | ||
| 273 | return; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub _post_uri { | ||
| 277 | my ($kernel, $self, $req) = @_[KERNEL, OBJECT, ARG0]; | ||
| 278 | |||
| 279 | my $title = $self->{URI_title} ? "$req->{title} - " : ''; | ||
| 280 | my $mirrors = join ' / ', | ||
| 281 | map { $req->{mirrored}{$_} } | ||
| 282 | sort keys %{ $req->{mirrored} }; | ||
| 283 | |||
| 284 | $self->{irc}->yield($self->{Method}, $req->{where}, "$title$mirrors"); | ||
| 285 | |||
| 286 | $kernel->refcount_decrement($req->{sender}, __PACKAGE__); | ||
| 287 | return; | ||
| 288 | } | ||
| 289 | |||
| 290 | 1; | ||
| 291 | |||
| 292 | =encoding utf8 | ||
| 293 | |||
| 294 | =head1 NAME | ||
| 295 | |||
| 296 | POE::Component::IRC::Plugin::ImageMirror - A PoCo-IRC plugin which uploads select images to a mirror service | ||
| 297 | |||
| 298 | =head1 SYNOPSIS | ||
| 299 | |||
| 300 | To quickly get an IRC bot with this plugin up and running, you can use | ||
| 301 | L<App::Pocoirc|App::Pocoirc>: | ||
| 302 | |||
| 303 | $ pocoirc -s irc.perl.org -j '#bots' -a ImageMirror | ||
| 304 | |||
| 305 | Or use it in your code: | ||
| 306 | |||
| 307 | use POE::Component::IRC::Plugin::ImageMirror; | ||
| 308 | |||
| 309 | # mirror all images from 4chan.org | ||
| 310 | $irc->plugin_add(ImageMirror => POE::Component::IRC::Plugin::ImageMirror->new( | ||
| 311 | URI_match => [ | ||
| 312 | qr{4chan\.org/\w+/src/.*(?i:jpe?g|gif|png)$}, | ||
| 313 | ], | ||
| 314 | )); | ||
| 315 | |||
| 316 | =head1 DESCRIPTION | ||
| 317 | |||
| 318 | POE::Component::IRC::Plugin::ImageMirror is a | ||
| 319 | L<POE::Component::IRC|POE::Component::IRC> plugin. It looks for image URLs in | ||
| 320 | the channel log and uploads the images to Imageshack and Imgur, then prints a | ||
| 321 | short description of the image along with the new URLs. | ||
| 322 | |||
| 323 | <avar> http://images.4chan.org/b/src/1267339589262.gif | ||
| 324 | -MyBot:#channel- gif (318 x 241) - http://imgur.com/RWcSE.gif - http://img535.imageshack.us/img535/9685/1267339589262.gif | ||
| 325 | |||
| 326 | This plugin makes use of | ||
| 327 | L<POE::Component::IRC::Plugin::URI::Find|POE::Component::IRC::URI::Find>. An | ||
| 328 | instance will be added to the plugin pipeline if it is not already present. | ||
| 329 | |||
| 330 | =head1 METHODS | ||
| 331 | |||
| 332 | =head2 C<new> | ||
| 333 | |||
| 334 | Takes the following optional arguments: | ||
| 335 | |||
| 336 | B<'Channels'>, an array reference of channels names. If you don't supply | ||
| 337 | this, images will be mirrored in all channels. | ||
| 338 | |||
| 339 | B<'URI_match'>, an array reference of regex objects. Any url found must match | ||
| 340 | at least one of these regexes if it is to be uploaded. If you don't supply | ||
| 341 | this parameter, a default regex of C<qr/(?i:jpe?g|gif|png)$/> is used. | ||
| 342 | |||
| 343 | B<'URI_subst'>, an hash reference of regex/string pairs. These | ||
| 344 | substitutions will be done on the accepted URIs before they are processed | ||
| 345 | further. | ||
| 346 | |||
| 347 | Example: | ||
| 348 | |||
| 349 | # always fetch 7chan images via http, not https | ||
| 350 | URI_subst => [ | ||
| 351 | qr{(?<=^)https(?=://(?:www\.)?7chan\.org)} => 'http', | ||
| 352 | ] | ||
| 353 | |||
| 354 | B<'URI_title'>, whether or not to include a title produced by | ||
| 355 | L<URI::Title|URI::Title>. Defaults to true. | ||
| 356 | |||
| 357 | B<'Imgur_user'>, an Imgur username. If provided, the images uploaded to Imgur | ||
| 358 | will be under this account rather than anonymous. | ||
| 359 | |||
| 360 | B<'Imgur_pass'>, an Imgur account password to go with B<'ImgurUser'>. | ||
| 361 | |||
| 362 | B<'Method'>, how you want messages to be delivered. Valid options are | ||
| 363 | 'notice' (the default) and 'privmsg'. | ||
| 364 | |||
| 365 | B<'Eat'>, when enabled, will prevent further processing of C<irc_urifind_uri> | ||
| 366 | events by other plugins for URIs which this plugin mirrors. False by default. | ||
| 367 | |||
| 368 | Returns a plugin object suitable for feeding to | ||
| 369 | L<POE::Component::IRC|POE::Component::IRC>'s C<plugin_add> method. | ||
| 370 | |||
| 371 | =head1 AUTHOR | ||
| 372 | |||
| 373 | Hinrik E<Ouml>rn SigurE<eth>sson, hinrik.sig@gmail.com | ||
| 374 | |||
| 375 | Imageshack-related code provided by E<AElig>var ArnfjE<ouml>rE<eth> | ||
| 376 | Bjarmason <avar@cpan.org>. | ||
| 377 | |||
| 378 | =head1 LICENSE AND COPYRIGHT | ||
| 379 | |||
| 380 | Copyright 2010 Hinrik E<Ouml>rn SigurE<eth>sson | ||
| 381 | |||
| 382 | This program is free software, you can redistribute it and/or modify | ||
| 383 | it under the same terms as Perl itself. | ||
| 384 | |||
| 385 | =cut | ||