-
Notifications
You must be signed in to change notification settings - Fork 1
/
Webcomic.pm
77 lines (67 loc) · 1.94 KB
/
Webcomic.pm
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
package Webcomic;
use Moo;
use MooX::Types::MooseLike::Base qw/Str HashRef CodeRef/;
use Webcomic::Tag;
use Cwd;
use LWP::UserAgent;
use HTML::TokeParser;
has 'url' => ( is => 'ro',
isa => Str,
required => 1 );
has 'tags' => ( is => 'rw',
isa => HashRef[CodeRef] );
has 'end' => ( is => 'ro',
isa => CodeRef,
default => sub { \&_file_exists } );
sub _download {
my ($info, $ua, $exitcode) = @_;
$| = 1;
print "fetching $info->{'filename'}: ";
my $res = $ua->mirror($info->{'image'}, $info->{'filename'});
if ($res->is_success()) {
print "ok\n";
${$exitcode} = 0;
} else {
print "nok\n";
}
}
sub update {
my $self = shift;
my $next = $self->url();
my $exitcode = 2;
my $ua = LWP::UserAgent->new('agent' => 'Firefox');
$ua->default_header('Referer' => $self->url());
my $tags = $self->tags();
while (1) {
my %info = ();
my $res = $ua->get($next);
unless ($res->is_success) {
die "Could not download URL $next: ", $res->status_line, ".\n";
}
my $parser = HTML::TokeParser->new(\$res->decoded_content());
while (my $tag = $parser->get_tag(keys %$tags)) {
$self->tags()->{$tag->[0]}->(Webcomic::Tag->new(tag => $tag, parser => $parser), \%info);
last if ($info{'end'});
}
unless (defined $info{'filename'}) {
$info{'filename'} = $self->basename($info{'image'});
}
if ($self->end()->(%info)) {
print "Finished.\n";
exit $exitcode;
}
_download(\%info, $ua, \$exitcode);
$next = $info{'next'};
}
}
sub _file_exists {
my %info = @_;
die cwd unless exists $info{'filename'};
return (-e $info{'filename'});
}
sub basename {
my ($self, $image) = @_;
return (split(/.*\//, $image))[1];
}
no Moo;
__PACKAGE__->meta->make_immutable;