Skip to content

Instantly share code, notes, and snippets.

@YuheiNakasaka
Created March 8, 2012 12:00
Show Gist options
  • Save YuheiNakasaka/2000713 to your computer and use it in GitHub Desktop.
Save YuheiNakasaka/2000713 to your computer and use it in GitHub Desktop.
oppai
use strict;
use warnings;
use LWP::UserAgent;
use URI;
use JSON qw/decode_json/;
use Digest::MD5 qw/md5_hex/;
use Path::Class qw/dir file/;
use Encode;
use utf8;
my $appid = '';
my $uri = URI->new('http://api.bing.net/json.aspx');
my $ua = LWP::UserAgent->new;
my $dir = dir('./data');
my $page_count = 0;
my $download_count = 0;
while(1){
my $offset = $page_count*50;
$uri->query_form(
AppId =>$appid,
Version =>'2.2',
Markert =>'ja-JP',
Sources => 'image',
'Image.Count' =>50,
'Image.Offset' =>$offset,
Adult => 'off',
Query => 'おっぱい'
);
my $res = $ua->get($uri);
die $res->status_line if $res->is_error;
my $ref = decode_json($res->content);
last unless defined @{$ref->{SearchResponse}{Image}{Results}};
for my $entry(@{$ref->{SearchResponse}{Image}{Results}}){
next unless $entry->{MediaUrl} =~ /\.jpg$/;
$download_count++;
my $filename = md5_hex(encode_utf8($entry->{MediaUrl})).'.jpg';
my $filepath = $dir->file($filename);
next if -f $filepath;
print encode_utf8("$download_count:Download...$entry->{MediaUrl}\n");
$res = $ua->get($entry->{MediaUrl},':content_file'=> $filepath->stringify);
unless ($res->content_type =~ m/^image/){
unlink $filepath;
}
}
$page_count++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment