]> git.donarmstrong.com Git - bin.git/blob - get_bleachexile
add reset usb bus command
[bin.git] / get_bleachexile
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_bleachexile - get_bleachexile [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                bleachexile        => 'http://manga.bleachexile.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{bleachexile}.'/'.lc($manga).'.html');
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     # Find out how many chapters there are
111
112     my @chapters = $m->content() =~ m{<option\s+value="(\d+)"\s*(?:selected="selected"\s*)?>\s*Chapter\s+\#?\d+\s*</option>}gi;
113
114     for my $chapter (uniq(@chapters)) {
115         my ($chapter) = +$chapter;
116         my $chapter_long = $chapter =~ /\./ ? join('.',map {sprintf'%04d',$_} split /\./,$chapter) : sprintf('%04d',$chapter);
117         my $chapter_url = $options{bleachexile}.'/'.lc($manga).'-chapter-'.$chapter.'.html';
118         if (! -d "$manga/$chapter_long") {
119             print $chapter_url,qq(\n);
120             mm_get($m,$chapter_url);
121             # Find out how many pages there are
122             my @pages = $m->content() =~ m{<option\s+value="(\d+)"\s*(?:selected="selected"\s*)?>\s*Page\s+\#?[\d\.\-]+\s*</option>}gi;
123             mkdir("$manga/$chapter_long");
124             @pages = uniq(map {+$_} @pages);
125             for my $page (@pages) {
126                 my $page_url = $options{bleachexile}.'/'.lc($manga).'-chapter-'.$chapter.'-page-'.$page.'.html';
127                 print $page_url.qq(\n);
128                 mm_get($m,$page_url);
129                 my $image = $m->find_image(url_abs_regex => qr{static\.bleachexile\.com/manga/});
130                 mm_get($m,$image->url_abs());
131                 print "getting ".$image->url_abs()."\n";
132                 my ($page_long) = $image->url_abs =~ m/([^\/]+)$/;
133                 $page_long = sprintf('%04d',$page).'_'.$page_long;
134                 $page_long =~ s/(?:\%20)+/_/g;
135                 $page_long =~ s/[\s_-]+/_/;
136                 $m->save_content("$manga/$chapter_long/${page_long}");
137                 sleep 3;
138             }
139         }
140     }
141 }
142
143 sub mm_get{
144     my ($m,$url) = @_;
145     my $rerun = 8;
146     my $return;
147     do {
148         eval {
149             $return = $m->get($url);
150         };
151     } while ($@ and
152              ($rerun-- > 0) and sleep 5);
153     return $return;
154 }
155
156 sub uniq {
157     my @return;
158     my %existing;
159     for (@_) {
160         push @return,$_ unless exists $existing{$_};
161         $existing{$_} = 1;
162     }
163     return @return;
164 }
165
166
167 __END__