Skip to content

Instantly share code, notes, and snippets.

@ngs
Created October 8, 2010 21:45
Show Gist options
  • Save ngs/617628 to your computer and use it in GitHub Desktop.
Save ngs/617628 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl -w
use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/lib";
use Getopt::Compact;
use Erockr;
use URI::Escape;
use encoding "utf8";
my $getopt = Getopt::Compact->new(
name => 'erockr.pl',
args => 'NAME',
version => '0.1',
modes => [],
struct => [
[[qw|o out|], 'Directory to save photos.','=s']
],
);
my $name;
unless($name=shift @ARGV) {
print STDERR $getopt->usage;
exit 1;
}
my $opts = $getopt->opts;
utf8::encode($name);
my $e = Erockr->new(
name => $name,
dir => ( $opts->{out} || $FindBin::Bin ) . "/dl/".$name
);
1 while($e->next);
package Erockr;
use strict;
use warnings;
use URI;
use URI::Escape;
use Data::Dumper;
use Web::Scraper;
use File::Path qw/mkpath/;
use LWP::Simple;
use constant URL_FORMAT => 'http://erockr.com/archive/%s/%d/200/s';
sub new {
my $class = shift;
my $self = bless {
page => 1,
@_
}, $class;
mkpath $self->dir;
$self;
}
sub dir { $_[0]->{dir} }
sub name { $_[0]->{name} }
sub next {
my $self = shift;
my $url = sprintf(URL_FORMAT,uri_escape($self->name),$self->{page}++);
print $url,"\n";
my $page = scraper {
process '#searchResultList a', 'results[]' => '@href',
process 'a[rel="next"]', next_page => '@href',
};
my $res = $page->scrape( URI->new($url) );
$self->get_image($_) foreach @{ $res->{results} };
defined $res->{next_page}
}
sub get_image {
my $self = shift;
my $url = shift;
my $page = scraper {
process '#image img', 'src' => '@src',
};
my $res = $page->scrape( URI->new($url) );
$self->download($res->{src});
}
sub download {
my $self = shift;
my $url = shift;
if( $url =~ m|.*/(.*)$|i ) {
my $file = $self->dir."/$1";
print "$url -> $file\n";
getstore($url,$file);
return 1;
}
0;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment