]> git.donarmstrong.com Git - bin.git/blob - get_one_manga
add reset usb bus command
[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 HTTP::Cookies;
55 use IO::Dir;
56 use IO::File;
57 use vars qw($DEBUG);
58
59 my %options = (debug           => 0,
60                help            => 0,
61                man             => 0,
62                onemanga        => 'http://www.onemanga.com',
63                );
64
65 GetOptions(\%options,
66            'debug|d+','help|h|?','man|m');
67
68 pod2usage() if $options{help};
69 pod2usage({verbose=>2}) if $options{man};
70
71 $DEBUG = $options{debug};
72
73 my @USAGE_ERRORS;
74 # if (1) {
75 #      push @USAGE_ERRORS,"You must give the name of a manga";
76 # }
77
78 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
79
80
81 my @manga_to_get = @ARGV;
82
83 if (not @ARGV) {
84     my $d = IO::Dir->new('.') or die "Unable to open directory . for reading";
85     while (defined($_ = $d->read)) {
86         next if /^\./;
87         next unless -d $_;
88         push @manga_to_get,$_;
89     }
90 }
91
92
93 my $failure = 0;
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');
97 # use Data::Dumper;
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";
104         $failure ||= 1;
105         next;
106     }
107     if (! -d $manga) {
108         mkdir($manga);
109     }
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});
127             }
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";
142                 sleep 3;
143             }
144         }
145     }
146 }
147
148 sub mm_get{
149     my ($m,$url) = @_;
150     my $rerun = 8;
151     my $return;
152     do {
153         eval {
154             $return = $m->get($url);
155         };
156     } while ($@ and
157              ($rerun-- > 0) and sleep 5);
158     return $return;
159 }
160
161
162 __END__