-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwipple.pl
81 lines (65 loc) · 2.01 KB
/
twipple.pl
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
75
76
77
78
79
80
81
#!/usr/bin/env perl
use strict;
use warnings;
use Path::Tiny;
use Furl;
use WWW::Mechanize;
use WWW::Mechanize::AutoPager;
use Web::Query;
use DateTime::Format::Strptime;
use Log::Minimal;
my $screen_name = shift or die "usage: perl $0 twipleid";
my $f = Furl->new;
my $base = 'http://p.twpl.jp/show/orig/';
my $dir = path('twipple_photo', $screen_name);
$dir->mkpath unless -d $dir;
my $mech = WWW::Mechanize->new;
$mech->autopager->add_site(
url => 'http://p.twipple.jp/user/.+',
nextLink => 'id("nextFoot")//a[contains(text(),"next")]',
pageElement => 'id("mypage")',
);
my $fmt = '%b %d, %Y at %I:%M %p %Z';
my $strp = DateTime::Format::Strptime->new(
pattern => $fmt,
locale => 'en_US',
time_zone => 'Asia/Tokyo',
on_error => 'croak',
);
my $res = $mech->get("http://p.twipple.jp/user/$screen_name/detail");
store($res);
while (my $link = $mech->next_link()) {
sleep 5;
my ($n) = ($link =~ m!/(\d+)!);
infof $link;
my $res = $mech->get($link);
store($res, $n);
}
sub store {
my ($res, $n) = @_;
$n //= 1;
my $content = $res->content;
wq($content)
->find('.photoList')
->each(sub {
my $i = shift;
my ($photo_id) = ($_->attr('id') =~ /\Apic_thumb_(\w+)\z/);
my $date = $_->find('.photoDetail > span:nth-child(2)')->text();
$date =~ s/\A\s*|\s*\z//g;
my $dt = $strp->parse_datetime($date);
my $uri = $base . $photo_id;
my $file = $dir->child($dt->ymd('-') . '-' . $dt->hms('-') . '_' . $photo_id . '.jpg');
if (-e $file) {
infof 'exist %s, skip', $file;
} else {
infof '%s %s -> %s', $dt->datetime, $uri, $file->basename;
my $fres = $f->get($uri);
if ($fres->is_success) {
my $fh = $file->openw;
binmode $fh;
print $fh $fres->body;
close $fh;
}
}
});
}