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 $m->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 = sprintf('%04d',$chapter);
109 if (! -d "$manga/$chapter_long") {
110 print $chapter_link->url(),qq(\n);
111 mkdir("$manga/$chapter_long");
112 $m->get($chapter_link->url_abs());
113 $m->follow_link(text_regex => qr{Begin reading});
114 while ($m->uri() =~ m{\Q$chapter\E/(\d\d[^\/]*)/?$}) {
115 my $image = $m->find_image(alt_regex => qr{Loading\.+\s+media});
116 my $next_link = $m->find_link(url_regex => qr{\Q$manga\E/\Q$chapter\E/(\d\d[^\/]*)});
117 $m->get($image->url_abs());
118 print "getting ".$image->url_abs()."\n";
119 my ($page) = $image->url_abs =~ m/([^\/]+)$/;
120 $m->save_content("$manga/$chapter_long/$page");
121 last if not defined $next_link;
122 $m->get($next_link->url_abs());
123 print $m->uri()."\n";