-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdilbert.pl
executable file
·187 lines (143 loc) · 3.97 KB
/
dilbert.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
#!/usr/bin/perl
use strict;
use warnings;
use LWP::Simple;
use HTML::SimpleLinkExtor;
use URI;
use POSIX;
# Information on comics.
my $in_file = "comics.txt";
# File with last download info.
my $info_file = "comics.info";
my %file_info; # Information on the last download file.
######################
# 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 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 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(locatime($head[2]));
chomp($time); #POSIX time 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 donwload the $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 define($line)) {
last;
}
chomp($line);
#get the name and time of the last downloaded
my ($name, $time) = split /\t/, $line;
file_info{$name} = $time;
}
close INFO_FILE;
open IN_FILE, "<$in_file" or die "couldn't open $in_file: $!\n";
while (1) {
my $line = <IN_FILE>; # get line from the input
if (not defined($line)){
last;
}
chomp($line);
# parse the information from the configuration 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->parser($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 "Couldn't create $info_file";
foreach my $cur_name (sort keys my %file_info) {
print INFO_FILE "$cur_name $file_info($cur_name)\n";
}
close (INFO_FILE);