summaryrefslogtreecommitdiff
path: root/bin/comirror-setup
blob: 6bdbbe90f87ae240813d7cd410060bfc4609aaa1 (plain)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

use Getopt::Long;
use WWW::Mechanize;

my @mechs;
my @images;
my @unique_images;
my ($image_re, $cache) = (q{}) x 2;
my $length;
my $conf = {};
my $state;
my @opts = (
	[
		'batch',
		q{},
		undef,
		0,
	],
	[
		'link-to-image',
		q{!},
		'Try downloading links instead of images matching image_re first?',
		0,
	],
	[
		'version',
		q{},
		undef,
		0,
	],
);
my $version = '0.0-git';

local $| = 1;

binmode(STDOUT, ':utf8');

sub hash_to_file {
	my ($hash, $file) = @_;
	open(my $fh, '>', $file) or die("Can't open $file for writing: $!\n");
	binmode($fh, ':utf8');
	while (my ($key, $value) = each(%{$hash})) {
		$key =~ tr/-/_/;
		print {$fh} "$key\t$value\n";
	}
	close($fh);
	return;
}

sub parse_or_ask_options {

	if ($conf->{'batch'}) {
		delete $conf->{'batch'};
		return;
	}

	for my $ref (@opts) {
		my ($opt, $desc, $default) = @{$ref}[0, 2, 3];

		if (defined $desc and not exists $conf->{$opt}) {
			print "${desc} (default: ${default}) ";
			my $reply = <STDIN>;
			chomp $reply;
			$conf->{$opt} = $reply;
		}
	}
	return;
}

GetOptions($conf, map { $_->[0] . $_->[1] } @opts);

if ($conf->{'version'}) {
	say "comirror version $version";
	exit 0;
}

if (not -t STDOUT) {
	$conf->{'batch'} = 1;
}

if (@ARGV != 3 ) {
	die("Need three URLs to compare (first, second, last but one)\n");
}

print 'Fetching pages';

for my $url (@ARGV) {
	push(@mechs, WWW::Mechanize->new( stackdepth => 0 ));
	$mechs[-1]->get($url);
	print q{.};
}

print "\nComparing images";

for my $i ( 0 .. $#mechs ) {
	for my $image ($mechs[$i]->find_all_images()) {
		push(@{$images[$i]}, $image->url_abs());
	}
	print q{.};
}

print "\n";

for my $link ($mechs[0]->find_all_links()) {
	if ($link->url_abs eq $ARGV[1]) {
		$conf->{'next_link'} = $link->text;
	}
}

# A bit fragile so far. We assume that every site is exactly the same, except
# for the actual comic image. For this to work, we need to be sure that we are
# not comparing with a first or last site, because those may be missing a
# next/prev icon and therefore confuse us.

for my $i ( 0 .. $#{$images[1]} ) {
	if ($images[1]->[$i] ne $images[2]->[$i]) {
		push(@unique_images, [$images[1]->[$i], $images[2]->[$i]]);
	}
}

# XKCD has a weird robot detection image. So we just take the first
# @unique_images element for now. Again, this could use more elegance some
# time.

if (length($unique_images[0]->[0]) <= length($unique_images[0]->[1])) {
	$length = length($unique_images[0]->[0]);
}
else {
	$length = length($unique_images[0]->[1]);
}

for my $offset ( 0 .. $length ) {
	my $char1 = substr($unique_images[0]->[0], $offset, 1);
	my $char2 = substr($unique_images[0]->[1], $offset, 1);

	if ($char1 ne $char2) {
		$image_re .= q{.+};
		last;
	}

	$cache .= $char1;

	# Prevent using .../something.+ if we happen to have two images whose
	# names start with the same letter(s). Again, fragile.
	if ($char1 =~ / [^a-zA-Z0-9] /x) {
		$image_re .= $cache;
		$cache = q{};
	}
}

$state->{'uri'} = $ARGV[0];

$conf->{'image_re'} = $image_re;

say "\nimage_re: $conf->{image_re}";
say "\"next\" link text: $conf->{next_link}";

parse_or_ask_options();

hash_to_file($conf , 'comirror.conf' );
hash_to_file($state, 'comirror.state');

say "\nIf everything is correct, type 'comirror' to start mirroring";

__END__

=head1 NAME

B<comirror-setup> - Set up a directory to be used by B<comirror>

=head1 SYNOPSIS

B<comirror-setup> I<comis urls...>

=head1 DESCRIPTION

B<comirror-setup> takes three URL arguments: The very first page of the web
comic, the second page, and the last but one page.

Based on these arguments, it tries to set up the current working directory so
that you only need to call B<comirror> to mirror the webcomic you were
pointing to.  It does this by comparing the last two URLs to determine a
correct regular expression for the comic images and comparing the first two to
figure out the text on the "next" link.

It then creates F<comirror.conf> and F<comirror.state> so that you only need
to call comirror(1) to start downloading.

Unless all options (or B<--batch>) were specified on the commandline,
B<comirror> will interactively ask the user for further options.

=head1 OPTIONS

=over

=item B<--batch>

Do not ask questions interactively.  Take default values or those specified
via options on the commandline.

=item B<--link-to-image> I<bool>

First try downloading a link matching image_re, and if that fails look for an
image matching image_re. Note that this does not (yet) affect image_re
detection.

=back

=head1 EXIT STATUS

Non-zero on grave errors, zero otherwise.

=head1 CONFIGURATION

B<comirror-setup> is not configurable.

=head1 DEPENDENCIES

This script requires the perl module WWW::Mechanize.

=head1 BUGS AND LIMITATIONS

As with B<comirror> itself: This script has no brain.
So far, this script is very dumb and fragile.  It's recommended to take a
quick glance at the image_re before actually calling comirror to make sure you
get what you want.

=head1 AUTHOR

Copyright (C) 2010 by Daniel Friesel E<lt>derf@chaosdorf.deE<gt>

=head1 LICENSE

  0. You just DO WHAT THE FUCK YOU WANT TO.