Fixed bug at fetching page >10
[onemanga_down.git] / onemanga_down.pl
blobeac915b2844ae9721debe6cc571c4ac3387d5c2b
1 #!/usr/bin/perl
2 # onemanga_down.pl : A script to download manga from onemanga
4 # Released under ISC license.
6 # Copyright (c) 2008, Abel Camarillo <00z@the00z.org>
8 # Permission to use, copy, modify, and/or distribute this software for any
9 # purpose with or without fee is hereby granted, provided that the above
10 # copyright notice and this permission notice appear in all copies.
12 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 use WWW::Mechanize;
22 use Getopt::Long ;
24 Getopt::Long::Configure('bundling');
26 # default options
27 my ($debug, $url, $base, $dst_dir) = (0, '', '', '');
29 GetOptions('debug|d' => \$debug
30 , 'url|u=s' =>\$url
31 , 'dest|D=s' =>\$dst_dir
34 # Initial url
35 #$url='http://www.onemanga.com/BLAME/1/01/';
38 my $mech = WWW::Mechanize->new(stack_depth => 2);
40 while (1)
42 print STDERR "#" x 72, "\n" ;
43 $mech->get($url);
45 break unless $mech->success();
47 my ($base_url) = $url =~ m{(http://[^/]+)/+};
49 my ($next_url) = $mech->content =~
50 m{<[[:space:]]*input[[:space:]]+
51 type="button"[[:space:]]+
52 value="next[[:space:]]+page"[[:space:]]
53 onclick="javascript:window.location='([^']+)';"[[:space:]]*/*>}sx;
55 $next_url = "$base_url/$next_url";
57 $url =~ s/^$base_url//;
59 my ($name, $chapter, $page) = (split qq{/}, $url)[-3..-1] ;
61 # add leading zeros to $chapter and $page
63 ($chapter) = ('000' . $chapter) =~ m/(.{3})$/;
65 # to enable that odd `double' pages
66 $page =~ s/^[[:digit:]]+[^[:digit:]]//;
68 ($page) = ('000' . $page) =~ m/(.{3})$/;
70 my @dst_file = ($dst_dir,$name,$chapter,$page) ;
72 shift @dst_file unless $dst_dir;
74 # create directories
75 for (my $i=0; $i<$#dst_file;$i++)
77 my $dir =join('/',@dst_file[0..$i]);
78 unless (-d $dir)
80 print STDERR "mkdir $dir\n";
81 mkdir $dir or die "$!" ;
85 my %data =
87 'URI: ' => $mech->uri
88 , 'URL: ' => $url
89 , 'Base URI: ' => $mech->base
90 , 'Content : ' => $mech->ct
91 , 'title : ' => $mech->title
92 , 'Images : ' => join (qq/\n/, map { $_->url() } $mech->images )
93 , 'Destination: ' => join ('/', @dst_file)
94 , 'Chapter : ' => $chapter
95 , 'Page : ' => $page
96 , 'Name : ' => $name
97 , 'Next : ' => $next_url
101 print STDERR map { ($_ , $data{$_}, "\n") } sort {$a cmp $b} keys %data
102 if $debug;
104 for ($mech->images)
106 next unless ( $_->url =~ m{/mangas/.*(?:jpe?g|png|gif)$} );
108 my ($extension) = $_->url =~ /(\.[^.]+)$/;
109 $dst_file[$#dst_file] .= $extension;
111 my $dst_file =join ('/', @dst_file);
113 $mech->get($_->url) ;
114 print STDERR qq/Downloading Image: `/, $_->url, qq/' to `$dst_file' .../;
116 open DST , ">$dst_file" or die "\n$!";
117 print DST $mech->content;
118 close DST;
120 print STDERR qq/successfull!\n/ if $mech->success;
124 $url = $next_url;