Skip to content

Instantly share code, notes, and snippets.

@xaicron
Created July 23, 2010 10:53
Show Gist options
  • Save xaicron/487283 to your computer and use it in GitHub Desktop.
Save xaicron/487283 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use strict;
use warnings;
use 5.0100;
use LWP::UserAgent;
use JSON qw/decode_json/;
use URI::Escape qw/uri_escape/;
use File::Basename qw/basename/;
use Getopt::Long qw/GetOptions/;
my $page = 1;
my $per_page = 50;
my $base = "http://search.naver.jp/image?q=%s&vt=aj&start=%d&display=$per_page";
GetOptions(
'p|page=i' => \$page,
) or usage();
my $keyword = shift || usage();
my $save_dir = shift || '.';
my $res = LWP::UserAgent->new->get(sprintf $base, uri_escape($keyword), start_index($page));
die "Oops!: ", $res->status_line unless $res->is_success;
my $data = decode_json $res->content;
my $entries = $data->{entry};
for my $entry (@$entries) {
if (my $link = $entry->{link}) {
say my $url = $link->{href};
store($url);
}
}
sub store {
my $url = shift;
return !system 'lwp-download', $url, $save_dir;
}
sub start_index {
my $page = shift;
return ($page - 1) * $per_page + 1;
}
sub usage {
print <<USAGE;
Usage: naver.pl [options] keyword [save_dir]
Options:
--page, -p default 1
USAGE
exit 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment