script/get_novel.pl untested

filter: Statementstmt 0.0% 0 / 82 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 53 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 186 CC 9 SCAR 32.6
CC
9
Cov
0%
read_option
56.0
send_novel
12.0
BEGIN
2.0
CRAP 26.0
1 #!/usr/local/bin/perl
2 use strict;
3 use warnings;
4 use utf8;
5  
6 use Encode::Locale;
7 use Encode;
8 use Getopt::Long qw(:config no_ignore_case);
9 use Novel::Robot;
10 #use Data::Dumper;
11  
12 $| = 1;
13 #binmode( STDIN, ":encoding(console_in)" );
14 #binmode( STDOUT, ":encoding(console_out)" );
15 #binmode( STDERR, ":encoding(console_out)" );
16  
17 my %opt;
18 GetOptions(
19 \%opt,
20  
21 'site|s=s', 'url|u=s', 'file|f=s', 'writer|w=s', 'book|b=s',
22 'type|t=s', 'output|o=s',
23 'item|i=s', 'back_index=s',
24 'page|j=s',
25 'cookie|c=s',
26 'not_download|D', 'single_page', 'verbose|v',
27 'progress',
28  
29 'use_chrome',
30 'with_toc', 'grep_content=s', 'filter_content=s', 'only_poster', 'min_content_word_num=i',
31 'max_process_num=i',
32 'chapter_regex=s',
33 'content_path=s', 'writer_path=s', 'book_path=s', 'item_list_path=s',
34 'content_regex=s', 'writer_regex=s', 'book_regex=s',
35  
36 'query_type|q=s', 'query_keyword|k=s', 'board|B=s',
37  
38 # remote ansible host
39 #'remote|R=s',
40  
41 # mail
42 'mail_msg|m=s',
43 'mail_server|M=s',
44 'mail_port|p=s',
45 'mail_usr|U=s',
46 'mail_pwd|P=s',
47 'mail_from|F=s', 'mail_to|T=s',
48 );
49  
50 %opt = read_option( %opt );
51  
52 our $xs = Novel::Robot->new( %opt );
53 if ( $opt{cookie} ) {
54 my ( $dom, $base_dom ) = $xs->{parser}->detect_domain( $xs->{parser}->domain() || $opt{url} );
55 $xs->{browser}->read_moz_cookie( $opt{cookie}, $base_dom );
56 }
57  
58 my ( $outf, $bookr );
59 if ( $opt{file} ) {
60 if ( $opt{site} eq 'txt' ) {
61 my @path = split ',', $opt{file};
62 ( $outf, $bookr ) = $xs->get_novel( \@path, %opt );
63 } else {
64 $outf = $xs->{packer}->convert_novel( $opt{file}, $opt{output}, \%opt );
65 }
66 } elsif ( $opt{url} ) {
67 if ( $opt{not_download} ) {
68 my $r = $xs->{parser}->get_novel_info( $opt{url}, %opt, max_page_num => 1 );
69 print join( ",", $r->{writer} || '', $r->{book} || $r->{title} || '', $r->{url} || '', $r->{item_num} || '' ), "\n";
70 } elsif($opt{single_page}){
71 ($outf, $bookr) = $xs->get_page($opt{url}, %opt);
72 }else{
73 ( $outf, $bookr ) = $xs->get_novel( $opt{url}, %opt );
74 }
75 } elsif ( $opt{site} and $opt{writer} and $opt{book} ) {
76 ( $outf, $bookr ) = $xs->get_novel( "$opt{writer}:$opt{book}", %opt );
77 } elsif ( defined $opt{query_keyword} or defined $opt{board}) {
78 my $info_ref;
79 if(defined $opt{query_keyword}){
80 $info_ref = $xs->{parser}->get_query_ref( $opt{query_keyword}, %opt );
81 }else{
82 $info_ref = $xs->{parser}->get_board_ref($opt{board}, %opt);
83 }
84  
85 my $items_ref = $info_ref->{item_list};
86  
87 for my $r ( @$items_ref ) {
88 my $u = $r->{url};
89 next unless($u);
90 if ( $opt{not_download} ) {
91 print encode(locale=>join( ",", $r->{writer} // $info_ref->{writer} // 'unknown', $r->{book} // $r->{title}, $r->{url} )), "\n";
92 } else {
93 $xs->get_novel( $u, %opt );
94 }
95 }
96 }
97  
98 if($opt{mail_to} and -f $outf){
99 send_novel( $outf, $bookr, %opt );
100 }
101  
102 sub send_novel {
103 my ( $outf, $bookr, %o ) = @_;
104  
105 my $outfname= decode(locale=>$outf);
106 my $msg = $bookr->{writer} ? qq[$bookr->{writer}-$bookr->{book}-$bookr->{item_num}] : $outfname;
107 $msg=~s#^.*/##;
108  
109 my $cmd = qq[calibre-smtp -a "$outfname" -s "$msg" --relay $o{mail_server} --port $o{mail_port} -u "$o{mail_usr}" -p "$o{mail_pwd}" "$o{mail_from}" "$o{mail_to}" "$msg"];
110 print encode(locale=>$cmd), "\n";
111  
112 system( $cmd );
113  
114 unlink($outf) if($o{url});
115  
116 }
117  
118 sub read_option {
119 my ( %opt ) = @_;
120 $opt{site} ||= $opt{url} || $opt{file} || $opt{board};
121 $opt{type} ||= 'html';
122 $opt{with_toc} //= 1;
123 $opt{progress} //= 0;
124 $opt{max_process_num} //= 1;
125 $opt{verbose} //= 0;
126  
127 for my $k (
128 qw/writer writer_path writer_regex book book_path book_regex content_path content_regex item_list_path
129 chapter_regex
130 grep_content filter_content
131 query_keyword query_type
132 /
133 ) {
134 next unless ( exists $opt{$k} );
135 $opt{$k} = decode( locale => $opt{$k} );
136 }
137  
138 if ( $opt{page} ) {
139 @opt{qw/min_page_num max_page_num/} = Novel::Robot::split_index( $opt{page} );
140 }
141  
142 if ( $opt{item} ) {
143 @opt{qw/min_item_num max_item_num/} = Novel::Robot::split_index( $opt{item} );
144 }
145  
146 if ( $opt{file} ) {
147 my $tf = decode( locale => $opt{file} );
148 my ( $tw, $tb, $suffix ) = $tf =~ m#([^\/\\]+)-([^\/\\]+)\.([^.]+)$#;
149 $opt{writer} = $tw unless ( defined $opt{writer} );
150 $opt{book} = $tb unless ( defined $opt{book} );
151 }
152  
153 return %opt;
154 } ## end sub read_option