-
Notifications
You must be signed in to change notification settings - Fork 1
/
comics.pl
224 lines (174 loc) · 5.03 KB
/
comics.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/perl
=pod
=head1 NAME
comics.pl - Download comics from the Internet
=head1 SYNOPSIS
comics.pl
=head1 DESCRIPTION
The I<comics.pl> reads the file I<comics.txt> for information on the location
of comic strips on the Internet. Any new ones are downloaded and stored
in the directory I<comics>.
The format of the I<comics.txt> file is:
<name> <top-url> <pattern>
(Each field is separated from the next by a tab.)
Where:
=over 4
=item I<name>
Is the name of the comic. This will be used as the root of the file name when the comic is written.
=item I<top-url>
The url of the web page containing the comic. (Not the image, the text page which contains the image.)
=item I<pattern>
A regular expression to use when searching the web page for images.
=back
The program writes out the comics to the directory I<comics>. It writes out information
as to which comics have been seen into the file I<comics.info>
=head1 AUTHOR
Steve Oualline, E<lt>[email protected]<gt>.
=head1 COPYRIGHT
Copyright 2005 Steve Oualline.
This program is distributed under the GPL.
=cut
use strict;
use warnings;
use LWP::Simple;
use HTML::SimpleLinkExtor;
use URI;
use POSIX;
# Information on the comics
my $in_file = "comics.txt";
# File with last download info
my $info_file = "comics.info";
my %file_info; # Information on the last download
#############################################################
# do_file($name, $page, $link, $index)
#
# Download the given link and store it in a file.
# If multiple file are present,
# $index should be different
# for each file.
#############################################################
sub do_file($$$$)
{
my $name = shift; # Name of the file
my $page = shift; # The base page
my $link = shift; # Link to grab
my $index = shift; # Index (if multiple files)
# Try and get the extension of the file from the link
$link =~ /(\.[^\$\.]*)$/;
# Define the extension of the file
my $ext;
if (defined($1)) {
$ext = $1;
} else {
$ext = ".jpg";
}
my $uri = URI->new($link);
my $abs_link = $uri->abs($page);
# Get the heading information of the link
# (and the modification time goes into $2);
my @head = head($abs_link->as_string());
if ($#head == -1) {
print "$name Broken link: ",
$abs_link->as_string(), "\n";
return;
}
if (defined($file_info{$name})) {
# If we've downloaded this one before
if ($head[2] == $file_info{$name}) {
print "Skipping $name\n";
return;
}
}
# Set the file information
$file_info{$name} = $head[2];
# Time of the last modification
my $time = asctime(localtime($head[2]));
chomp($time); # Stupid POSIX hack
print "Downloading $name (Last modified $time)\n";
# The raw data from the page
my $raw_data = get($abs_link->as_string());
if (not defined($raw_data)) {
print "Unable to download link $link\n";
return;
}
my $out_name; # Name of the output file
if (defined($index)) {
$out_name = "comics/$name.$index$ext";
} else {
$out_name = "comics/$name$ext";
}
if (not open(OUT_FILE, ">$out_name")) {
print "Unable to create $out_name\n";
return;
}
binmode OUT_FILE;
print OUT_FILE $raw_data;
close OUT_FILE;
}
#------------------------------------------------------------
open INFO_FILE, "<$info_file";
while (1) {
my $line = <INFO_FILE>; # Get line from info file
if (not defined($line)) {
last;
}
chomp($line);
# Get the name of the the and the last download
my ($name, $time) = split /\t/, $line;
$file_info{$name} = $time;
}
close INFO_FILE;
open IN_FILE, "<$in_file"
or die("Could not open $in_file");
while (1) {
my $line = <IN_FILE>; # Get line from the input
if (not defined($line)) {
last;
}
chomp($line);
# Parse the information from the config file
my ($name, $page, $pattern) = split /\t/, $line;
# If the input is bad, fuss and skip
if (not defined($pattern)) {
print "Illegal input $line\n";
next;
}
# Get the text page which points to the image page
my $text_page = get($page);
if (not defined($text_page)) {
print "Could not download $page\n";
next;
}
# Create a decoder for this page
my $decoder = HTML::SimpleLinkExtor->new();
$decoder->parse($text_page);
# Get the image links
my @links = $decoder->img();
my @matches = grep /$pattern/, @links;
if ($#matches == -1) {
print "Nothing matched pattern for $name\n";
print " Pattern: $pattern\n";
foreach my $cur_link (@links) {
print " $cur_link\n";
}
next;
}
if ($#matches != 0) {
print "Multiple matches\n";
my $index = 1;
foreach my $cur_link (@matches) {
print " $cur_link\n";
do_file($name, $page, $cur_link, $index);
++$index;
}
next;
}
# One match
do_file($name, $page, $matches[0], undef);
}
open INFO_FILE, ">$info_file" or
die("Could not create $info_file");
foreach my $cur_name (sort keys %file_info) {
print INFO_FILE "$cur_name $file_info{$cur_name}\n";
}
close (INFO_FILE);