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 $
17 get_one_manga - get_one_manga [manga]
24 --debug, -d debugging level (Default 0)
25 --help, -h display this help
26 --man, -m display manual
34 Debug verbosity. (Default 0)
38 Display brief usage information.
58 my %options = (debug => 0,
61 onemanga => 'http://www.onemanga.com',
65 'debug|d+','help|h|?','man|m');
67 pod2usage() if $options{help};
68 pod2usage({verbose=>2}) if $options{man};
70 $DEBUG = $options{debug};
74 # push @USAGE_ERRORS,"You must give the name of a manga";
77 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
80 my @manga_to_get = @ARGV;
83 my $d = IO::Dir->new('.') or die "Unable to open directory . for reading";
84 while (defined($_ = $d->read)) {
87 push @manga_to_get,$_;
91 exit 0 unless @manga_to_get;
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";
101 my $alpha_list = $m->clone();
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";
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 mm_get($m,$link->url_abs());
130 while ($m->uri() =~ m{\Q$chapter\E/(\d\d[^\/]*)/?$}) {
131 my $image = $m->find_image(alt_regex => qr{Loading\.+\s+(media|img)});
132 my $next_link = $m->find_link(url_regex => qr{\Q$manga_escaped\E/\Q$chapter\E/(\d\d[^\/]*)});
133 mm_get($m,$image->url_abs());
134 print "getting ".$image->url_abs()."\n";
135 my ($page) = $image->url_abs =~ m/([^\/]+)$/;
136 $m->save_content("$manga/$chapter_long/$page");
137 last if not defined $next_link;
138 mm_get($m,$next_link->url_abs());
139 print $m->uri()."\n";
156 $return = $m->get($url);
159 ($rerun-- > 0) and sleep 5);