Skip to content

Instantly share code, notes, and snippets.

@ishiduca
Created November 3, 2011 14:21
Show Gist options
  • Save ishiduca/1336597 to your computer and use it in GitHub Desktop.
Save ishiduca/1336597 to your computer and use it in GitHub Desktop.
AnyEvent上でdlのテスト
#!/usr/bin/env perl
use strict;
use warnings;
use Data::Dumper;
use AnyEvent;
use AnyEvent::HTTP;
use Config::Pit;
use Web::Scraper;
use File::Basename;
my $illust_id = $ARGV[0] || die qq(! failed: "illust_id" not found\n);
my $pixiv_net = 'http://www.pixiv.net';
my $login_php = "${pixiv_net}/login.php";
my $illust_top = "${pixiv_net}/member_illust.php?mode=medium&illust_id=";
my $config = pit_get('www.pixiv.net', require => {
'pixiv_id' => '', 'pass' => '',
});
my $jar = {};
my $data = {};
my $cv = AE::cv;
http_request('POST' => $login_php,
body => "mode=login&pixiv_id=$config->{pixiv_id}&pass=$config->{pass}",
headers => { 'Content-Type' => 'application/x-www-form-urlencoded' },
recurse => 0,
sub {
my($body, $headers) = @_;
warn Dumper $headers;
$jar = parse_cookie($headers->{'set-cookie'});
warn Dumper $jar;
http_request('GET' => $headers->{location},
cookie_jar => $jar,
sub {
my($body, $headers) = @_;
warn Dumper $headers;
http_request('GET' => "${illust_top}${illust_id}",
cookie_jar => $jar,
sub {
my($body, $headers) = @_;
warn Dumper $headers;
$data = scrape_illust_top($body);
my $contents_url = $data->{contents_url};
http_request('GET' => $contents_url,
cookie_jar => $jar,
headers => {
referer => "${illust_top}${illust_id}",
},
sub {
my($body, $headers) = @_;
warn Dumper $headers;
if ($contents_url =~ /mode=manga/){
while ($body =~ m!(http://img\d\d\.pixiv\.net/img/[^']+?)'!g) {
my $uri = $1;
warn $uri;
$cv->begin;
download($uri, $jar, {
referer => "${illust_top}${illust_id}",
}, sub { $cv->end; });
}
} else {
# /mode=big/
my $scraper = scraper {
process '//div/a/img[1]', 'img_src' => '@src';
};
my $data = $scraper->scrape($body);
warn $data->{img_src};
$cv->begin;
download($data->{img_src}, $jar, {
referer => "${illust_top}${illust_id}",
}, sub { $cv->end; });
}
}
);
},
);
},
);
},
);
$cv->recv;
sub download {
my $cb = pop;
my($uri, $jar, $headers) = @_;
my $filename = basename $uri;
open my $fh, '>', $filename or die qq(! failed: "${filename}" $!);
binmode $fh;
http_request('GET' => $uri,
cookie_jar => $jar,
headers => $headers,
on_body => sub {
my($partial_body, $header) = @_;
if ($header->{Status} =~ /^2/) {
print $fh $partial_body;
}
1;
},
$cb,
);
}
sub scrape_illust_top {
my $body = shift;
my $scraper = scraper {
process '//div[@class="works_display"]/a[1]', 'contents_url' => [ '@href', sub {
return join '/', $pixiv_net, $_;
} ];
process '//div[@class="works_display"]/a[1]/img[1]', 'img_src' => '@src';
};
return $scraper->scrape($body);
}
sub parse_cookie {
local $_ = shift || return ;
my %cookie = ();
my $phpsessid = 'PHPSESSID';
map{
my($key, $value) = split /=/;
$cookie{$key} = $value;
}(split /; /);
die qq(! failed: "${phpsessid}" not found) unless $cookie{$phpsessid};
return {
$cookie{domain} => {
$cookie{path} => {
$phpsessid => {
_expires => AnyEvent::HTTP::parse_date $cookie{expires},
#_expires => $cookie{expires},
value => $cookie{$phpsessid},
},
},
},
version => 1,
};
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment