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.
59 my %options = (debug => 0,
62 onemanga => 'http://www.onemanga.com',
66 'debug|d+','help|h|?','man|m');
68 pod2usage() if $options{help};
69 pod2usage({verbose=>2}) if $options{man};
71 $DEBUG = $options{debug};
75 # push @USAGE_ERRORS,"You must give the name of a manga";
78 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
81 my @manga_to_get = @ARGV;
84 my $d = IO::Dir->new('.') or die "Unable to open directory . for reading";
85 while (defined($_ = $d->read)) {
88 push @manga_to_get,$_;
94 my $m = WWW::Mechanize->new();
95 $m->cookie_jar(HTTP::Cookies->new());
96 $m->cookie_jar()->set_cookie(1,'age_verified','42','/','www.1000manga.com');
98 # print STDERR Dumper($m->cookie_jar());
99 for my $manga (@manga_to_get) {
100 # see if the manga exists
101 mm_get($m,$options{onemanga}.'/'.$manga);
102 if ($m->status() != 200) {
103 print STDERR "Manga $manga doesn't exist\n";
110 # figure out where to start getting stuff
111 # we need to escape ! apparently; there are probably other characters as well
112 my $manga_escaped = uri_escape($manga,'!');
113 my @chapter_links = $m->find_all_links(url_abs_regex => qr{\Q$manga\E\/\d+});
114 for my $chapter_link (reverse @chapter_links) {
115 my ($chapter) = $chapter_link->url() =~ m/([\d.-]+)\/?$/;
116 my $chapter_long = $chapter =~ /\./ ? join('.',map {sprintf'%04d',$_} split /\./,$chapter) : sprintf('%04d',$chapter);
117 if (! -d "$manga/$chapter_long") {
118 print $chapter_link->url(),qq(\n);
119 mm_get($m,$chapter_link->url_abs());
120 my $link = $m->find_link(text_regex => qr{Begin [Rr]eading});
121 if (not defined $link) {
122 #print $m->content();
123 my $temp = $m->find_link(text_regex => qr{Read.*at.*1000manga\.com});
124 mm_get($m,$temp->url_abs());
125 #print $m->content();
126 $link = $m->find_link(text_regex => qr{Begin [Rr]eading});
128 mm_get($m,$link->url_abs());
129 # print $link->url_abs();
130 # print $m->content();
131 mkdir("$manga/$chapter_long");
132 while ($m->uri() =~ m{\Q$chapter\E/(\d\d[^\/]*)/?$}) {
133 my $image = $m->find_image(alt_regex => qr{Loading\.+\s+(media|img)});
134 my $next_link = $m->find_link(url_regex => qr{\Q$manga_escaped\E/\Q$chapter\E/(\d\d[^\/]*)});
135 mm_get($m,$image->url_abs());
136 print "getting ".$image->url_abs()."\n";
137 my ($page) = $image->url_abs =~ m/([^\/]+)$/;
138 $m->save_content("$manga/$chapter_long/$page");
139 last if not defined $next_link;
140 mm_get($m,$next_link->url_abs());
141 print $m->uri()."\n";
154 $return = $m->get($url);
157 ($rerun-- > 0) and sleep 5);