]> git.donarmstrong.com Git - bin.git/blob - get_one_manga
handle new style of manga
[bin.git] / get_one_manga
1 #! /usr/bin/perl
2 # , and is released
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 $
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 get_one_manga - get_one_manga [manga] 
18
19 =head1 SYNOPSIS
20
21  [options]
22
23  Options:
24   --debug, -d debugging level (Default 0)
25   --help, -h display this help
26   --man, -m display manual
27
28 =head1 OPTIONS
29
30 =over
31
32 =item B<--debug, -d>
33
34 Debug verbosity. (Default 0)
35
36 =item B<--help, -h>
37
38 Display brief usage information.
39
40 =item B<--man, -m>
41
42 Display this manual.
43
44 =back
45
46 =head1 EXAMPLES
47
48
49 =cut
50
51
52 use URI::Escape;
53 use WWW::Mechanize;
54 use IO::Dir;
55 use IO::File;
56 use vars qw($DEBUG);
57
58 my %options = (debug           => 0,
59                help            => 0,
60                man             => 0,
61                onemanga        => 'http://www.onemanga.com',
62                );
63
64 GetOptions(\%options,
65            'debug|d+','help|h|?','man|m');
66
67 pod2usage() if $options{help};
68 pod2usage({verbose=>2}) if $options{man};
69
70 $DEBUG = $options{debug};
71
72 my @USAGE_ERRORS;
73 # if (1) {
74 #      push @USAGE_ERRORS,"You must give the name of a manga";
75 # }
76
77 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
78
79
80 my @manga_to_get = @ARGV;
81
82 if (not @ARGV) {
83     my $d = IO::Dir->new('.') or die "Unable to open directory . for reading";
84     while (defined($_ = $d->read)) {
85         next if /^\./;
86         next unless -d $_;
87         push @manga_to_get,$_;
88     }
89 }
90
91
92 my $failure = 0;
93 my $m = WWW::Mechanize->new();
94 for my $manga (@manga_to_get) {
95     # see if the manga exists
96     mm_get($m,$options{onemanga}.'/'.$manga);
97     if ($m->status() != 200) {
98         print STDERR "Manga $manga doesn't exist\n";
99         $failure ||= 1;
100         next;
101     }
102     if (! -d $manga) {
103         mkdir($manga);
104     }
105     # figure out where to start getting stuff
106     # we need to escape ! apparently; there are probably other characters as well
107     my $manga_escaped = uri_escape($manga,'!');
108     my @chapter_links = $m->find_all_links(url_abs_regex => qr{\Q$manga\E\/\d+});
109     for my $chapter_link (reverse @chapter_links) {
110         my ($chapter) = $chapter_link->url() =~ m/([\d.-]+)\/?$/;
111         my $chapter_long = $chapter =~ /\./ ? join('.',map {sprintf'%04d',$_} split /\./,$chapter) : sprintf('%04d',$chapter);
112         if (! -d "$manga/$chapter_long") {
113             print $chapter_link->url(),qq(\n);
114             mkdir("$manga/$chapter_long");
115             mm_get($m,$chapter_link->url_abs());
116             my $link = $m->find_link(text_regex => qr{Begin reading});
117             mm_get($m,$link->url_abs());
118             while ($m->uri() =~ m{\Q$chapter\E/(\d\d[^\/]*)/?$}) {
119                 my $image = $m->find_image(alt_regex => qr{Loading\.+\s+(media|img)});
120                 my $next_link = $m->find_link(url_regex => qr{\Q$manga_escaped\E/\Q$chapter\E/(\d\d[^\/]*)});
121                 mm_get($m,$image->url_abs());
122                 print "getting ".$image->url_abs()."\n";
123                 my ($page) = $image->url_abs =~ m/([^\/]+)$/;
124                 $m->save_content("$manga/$chapter_long/$page");
125                 last if not defined $next_link;
126                 mm_get($m,$next_link->url_abs());
127                 print $m->uri()."\n";
128                 sleep 3;
129             }
130         }
131     }
132 }
133
134 sub mm_get{
135     my ($m,$url) = @_;
136     my $rerun = 8;
137     my $return;
138     do {
139         eval {
140             $return = $m->get($url);
141         };
142     } while ($@ and
143              ($rerun-- > 0) and sleep 5);
144     return $return;
145 }
146
147
148 __END__