]> git.donarmstrong.com Git - bin.git/blob - get_mangareader
add a VERBOSE flag to getmail
[bin.git] / get_mangareader
1 #! /usr/bin/perl
2 # , and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2009 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1432 2009-04-21 02:42:41Z don $
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 get_one_manga - get_one_manga [manga] 
18
19 =head1 SYNOPSIS
20
21  [options]
22
23  Options:
24   --debug, -d debugging level (Default 0)
25   --help, -h display this help
26   --man, -m display manual
27
28 =head1 OPTIONS
29
30 =over
31
32 =item B<--debug, -d>
33
34 Debug verbosity. (Default 0)
35
36 =item B<--help, -h>
37
38 Display brief usage information.
39
40 =item B<--man, -m>
41
42 Display this manual.
43
44 =back
45
46 =head1 EXAMPLES
47
48
49 =cut
50
51
52 use URI::Escape;
53 use WWW::Mechanize;
54 use IO::Dir;
55 use IO::File;
56 use vars qw($DEBUG);
57
58 my %options = (debug           => 0,
59                help            => 0,
60                man             => 0,
61                onemanga        => 'http://www.onemanga.com',
62                );
63
64 GetOptions(\%options,
65            'debug|d+','help|h|?','man|m');
66
67 pod2usage() if $options{help};
68 pod2usage({verbose=>2}) if $options{man};
69
70 $DEBUG = $options{debug};
71
72 my @USAGE_ERRORS;
73 # if (1) {
74 #      push @USAGE_ERRORS,"You must give the name of a manga";
75 # }
76
77 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
78
79
80 my @manga_to_get = @ARGV;
81
82 if (not @ARGV) {
83     my $d = IO::Dir->new('.') or die "Unable to open directory . for reading";
84     while (defined($_ = $d->read)) {
85         next if /^\./;
86         next unless -d $_;
87         push @manga_to_get,$_;
88     }
89 }
90
91 exit 0 unless @manga_to_get;
92
93
94 my $failure = 0;
95 my $m = WWW::Mechanize->new();
96 mm_get($m,"http://www.mangareader.net/alphabetical");
97 if ($m->status() != 200) {
98     print STDERR "Unable to get alphabetical listing of manga";
99     exit 1;
100 }
101 my $alpha_list = $m->clone();
102
103 for my $manga (@manga_to_get) {
104     # see if the manga exists
105     my $manga_regex = $manga;
106     $manga_regex =~ s/\W+/[_-]/g;
107     my $manga_url = $alpha_list->find_link(url_abs_regex=>qr{$manga_regex});
108     mm_get($m,$manga_url->url_abs());
109     if ($m->status() != 200) {
110         print STDERR "Manga $manga doesn't exist\n";
111         $failure ||= 1;
112         next;
113     }
114     if (! -d $manga) {
115         mkdir($manga);
116     }
117     # figure out where to start getting stuff
118     # we need to escape ! apparently; there are probably other characters as well
119     my $manga_escaped = uri_escape($manga,'!');
120     my @chapter_links = $m->find_all_links(url_abs_regex => qr{\Q$manga\E\/\d+});
121     for my $chapter_link (reverse @chapter_links) {
122         my ($chapter) = $chapter_link->url() =~ m/([\d.-]+)\/?$/;
123         my $chapter_long = $chapter =~ /\./ ? join('.',map {sprintf'%04d',$_} split /\./,$chapter) : sprintf('%04d',$chapter);
124         if (! -d "$manga/$chapter_long") {
125             print $chapter_link->url(),qq(\n);
126             mkdir("$manga/$chapter_long");
127             mm_get($m,$chapter_link->url_abs());
128             # my $link = $m->find_link(text_regex => qr{Begin reading});
129             # print $m->content();
130             # exit 0;
131             # mm_get($m,$link->url_abs());
132             print $m->uri()."\n";
133             while ($m->uri() =~ m{\Q$chapter\E/?(\d\d[^\/]*)?/?$}) {
134                 my $image = $m->find_image(alt_regex => qr{ - Page \d+});
135                 print $image->url_abs()."\n";
136                 my $next_link = $m->find_link(url_regex => qr{\Q$manga_escaped\E/\Q$chapter\E/(\d\d[^\/]*)});
137                 print "getting ".$image->url_abs()."\n";
138                 mm_get($m,$image->url_abs());
139                 my ($page) = $image->url_abs =~ m/([^\/]+)$/;
140                 $m->save_content("$manga/$chapter_long/$page");
141                 last if not defined $next_link;
142                 mm_get($m,$next_link->url_abs());
143                 print $m->uri()."\n";
144                 sleep 3;
145             }
146         }
147     }
148 }
149
150 sub manga_url{
151     my ($m,$manga) = @_;
152 }
153
154 sub mm_get{
155     my ($m,$url) = @_;
156     my $rerun = 8;
157     my $return;
158     do {
159         eval {
160             $return = $m->get($url);
161         };
162     } while ($@ and
163              ($rerun-- > 0) and sleep 5);
164     return $return;
165 }
166
167
168 __END__