]> git.donarmstrong.com Git - bin.git/blob - get_one_manga
add get_one_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 WWW::Mechanize;
53 use IO::Dir;
54 use IO::File;
55 use vars qw($DEBUG);
56
57 my %options = (debug           => 0,
58                help            => 0,
59                man             => 0,
60                onemanga        => 'http://www.onemanga.com',
61                );
62
63 GetOptions(\%options,
64            'debug|d+','help|h|?','man|m');
65
66 pod2usage() if $options{help};
67 pod2usage({verbose=>2}) if $options{man};
68
69 $DEBUG = $options{debug};
70
71 my @USAGE_ERRORS;
72 # if (1) {
73 #      push @USAGE_ERRORS,"You must give the name of a manga";
74 # }
75
76 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
77
78
79 my @manga_to_get = @ARGV;
80
81 if (not @ARGV) {
82     my $d = IO::Dir->new('.') or die "Unable to open directory . for reading";
83     while (defined($_ = $d->read)) {
84         next if /^./;
85         next unless -d $_;
86         push @manga_to_get,$_;
87     }
88 }
89
90
91 my $failure = 0;
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";
98         $failure ||= 1;
99         next;
100     }
101     if (! -d $manga) {
102         #mkdir($manga);
103     }
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         print $chapter_link->url(),qq(\n);
108         my ($chapter) = $chapter_link->url() =~ m/(\d+)\/?$/;
109         if (! -d "$manga/$chapter_link") {
110             #mkdir("$manga/$chapter");
111             my $page = 0;
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+/?$}) {
115                 $page++;
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+});
118                 $m->get($image->url_abs());
119                 print "getting ".$image->url_abs()."\n";
120                 # $m->save_content("$manga/$chapter/".sprintf('%04d',$page).".jpg");
121                 $m->get($next_link->url_abs());
122                 print $m->uri();
123                 sleep 3;
124             }
125             exit 0;
126         }
127     }
128 }
129
130
131 __END__