1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#!/usr/bin/env perl
#utf8
use utf8;
use 5.12.1;
use WWW::Mixi::Scraper;
use lib 'lib';
use Digest::MD5 qw( md5_hex );
use Encode;
use Config::Pit;
my $config = pit_get(
'nqounet@mixi.jp',
require => {
email => 'username@domain.com',
password => 'password',
},
);
use Log::Handler;
my $log = Log::Handler->new(
file => {
filename => 'log/app.log',
utf8 => 1,
maxlevel => 'info',# 'debug',
timeformat => '%Y/%m/%d %H:%M:%S',
message_layout => '[%T][%L] %m (%C)',
},
);
my $mixi = WWW::Mixi::Scraper->new(
mode => 'TEXT',
%{$config},
);
my $topic = shift(@ARGV);
$log->die('Usage: ./get_mixi_photo.pl topicURL') unless $topic;
my @list = $mixi->parse( $topic );
$log->dump( debug => \@list );
# トピックのタイトル
my $dir = md5_hex( Encode::encode_utf8( $list[0]{subject} ) );
mkdir $dir;
open my $fh, '>:utf8', qq{$dir/info.txt} or $log->die( qq{Can not open $dir/info.txt.} );
print $fh $list[0]{subject};
close $fh;
# トピックのトップ画像
for my $item ( @{$list[0]{images}} ) {
$log->dump( debug => $item->{link} );
my @urls = $mixi->parse( $item->{link} );
for my $url ( @urls ) {
save_url_image( $url, $dir );
}
}
# 各コメントの画像
for my $comment ( @{$list[0]{comments}} ) {
for my $item ( @{$comment->{images}} ) {
$log->dump( debug => $item->{link} );
my @urls = $mixi->parse( $item->{link} );
for my $url ( @urls ) {
save_url_image( $url, $dir );
}
}
}
# 保存一式
sub save_url_image {
my ($url, $dir) = @_;
my $link = $url->{link}->as_string;
$log->dump( debug => $url->{link} );
my ($ext) = $link =~ /(\.[a-zA-Z]+)$/ms;
$log->dump( debug => $ext );
$ext = '.jpg' unless $ext;
my $filename = md5_hex( $link ) . $ext;
return if -f qq{$dir/$filename};
my $content = $mixi->{mech}->get_content( $url->{link} );
$log->dump( debug => $content );
open my $fh, ">:raw", qq{$dir/$filename} or $log->die( qq{Can not open $dir/$filename.} );
print $fh $content;
close $fh;
}
|