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.
57 my %options = (debug => 0,
60 onemanga => 'http://www.onemanga.com',
64 'debug|d+','help|h|?','man|m');
66 pod2usage() if $options{help};
67 pod2usage({verbose=>2}) if $options{man};
69 $DEBUG = $options{debug};
73 # push @USAGE_ERRORS,"You must give the name of a manga";
76 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
79 my @manga_to_get = @ARGV;
82 my $d = IO::Dir->new('.') or die "Unable to open directory . for reading";
83 while (defined($_ = $d->read)) {
86 push @manga_to_get,$_;
92 my $m = WWW::Mechanize->new();
93 for my $manga (@manga_to_get) {
94 # see if the manga exists
95 mm_get($options{onemanga}.'/'.$manga);
96 if ($m->status() != 200) {
97 print STDERR "Manga $manga doesn't exist\n";
104 # figure out where to start getting stuff
105 my @chapter_links = $m->find_all_links(url_abs_regex => qr{\Q$manga\E\/\d+});
106 for my $chapter_link (reverse @chapter_links) {
107 my ($chapter) = $chapter_link->url() =~ m/([\d.-]+)\/?$/;
108 my $chapter_long = $chapter =~ /\./ ? join('.',map {sprintf'%04d',$_} split /\./,$chapter) : sprintf('%04d',$chapter);
109 if (! -d "$manga/$chapter_long") {
110 print $chapter_link->url(),qq(\n);
111 mkdir("$manga/$chapter_long");
112 mm_get($m,$chapter_link->url_abs());
113 my $link = $m->find_link(text_regex => qr{Begin reading});
114 mm_get($m,$link->url_abs());
115 while ($m->uri() =~ m{\Q$chapter\E/(\d\d[^\/]*)/?$}) {
116 my $image = $m->find_image(alt_regex => qr{Loading\.+\s+media});
117 my $next_link = $m->find_link(url_regex => qr{\Q$manga\E/\Q$chapter\E/(\d\d[^\/]*)});
118 mm_get($m,$image->url_abs());
119 print "getting ".$image->url_abs()."\n";
120 my ($page) = $image->url_abs =~ m/([^\/]+)$/;
121 $m->save_content("$manga/$chapter_long/$page");
122 last if not defined $next_link;
123 mm_get($m,$next_link->url_abs());
124 print $m->uri()."\n";
137 $return = $m->get($url);
140 $rerun-- > 0 and sleep 5);