Robot/Parser/lofter.pm untested

filter: Statementstmt 0.0% 0 / 79 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 136 CC 8 SCAR 29.7
CC
8
Cov
0%
extract_content
56.0
get_tiezi_ref
6.0
charset
2.0
CRAP 19.5
1 # ABSTRACT: http:://www.lofter.com
2 package Novel::Robot::Parser::lofter;
3 use strict;
4 use warnings;
5 use utf8;
6
7 use base 'Novel::Robot::Parser';
8
9 use Encode;
10 use Web::Scraper;
11 use JSON;
12
13 sub charset { 'utf8' }
14
15 sub class { 'tiezi' }
16
17 sub domain { 'lofter.com' }
18
19 sub extract_content {
20 my ( $self, $book, $h ) = @_;
21 my $r = scraper {
22 process '//ul[@class="m-list"]//li',
23 'artical[]' => { url => 'HTML', };
24 process '//h2//a',
25 'chapter[]' => {
26 title => 'TEXT',
27 url => '@href'
28 };
29 process '//a[@class="title"] | //a[@class="readall"]',
30 'chap[]' => {
31 title => 'TEXT',
32 url => '@href'
33 };
34 };
35 my $res_r = $r->scrape( $h );
36 my $chap_r =
37 ( $res_r->{artical} and @{ $res_r->{artical} } ) ? $res_r->{artical}
38 : ( $res_r->{chapter} and @{ $res_r->{chapter} } ) ? $res_r->{chapter}
39 : ( $res_r->{chap} and @{ $res_r->{chap} } ) ? $res_r->{chap}
40 : undef;
41 return unless ( $chap_r and @$chap_r );
42 if ( $res_r->{artical} ) {
43 ( $_->{title} ) = $_->{url} =~ m#<strong>(.+?)</strong>#s for @$chap_r;
44 ( $_->{url} ) = $_->{url} =~ m#<a href="([^"]+)">#s for @$chap_r;
45 }
46 my @chap_t = grep { $_->{url} =~ m#/post/# } @$chap_r;
47
48 return unless ( @chap_t );
49 my @chap_tidy = grep { $_->{title} =~ /$book/i } @chap_t;
50 return \@chap_tidy;
51 } ## end sub extract_content
52
53 sub gen_next_search_url {
54 my ( $self, $start_u, $i, $h ) = @_;
55 return "$start_u&page=$i";
56 }
57
58 sub gen_next_tag_url {
59 my ( $self, $start_u, $i, $h ) = @_;
60 return "$start_u?page=$i";
61 }
62
63 sub extract_item {
64 my ( $self, $c ) = @_;
65 #my $c = $self->{browser}->request_url( $r->{url} );
66 my $r = {};
67 #$r->{content} = $self->scrape_element_try($c, [
68 #{ path => '//div[starts-with(@class,"m-post")]', 'extract' => 'HTML' },
69 #{ path => '//div[@class="txtcont"]', 'extract' => 'HTML' },
70 #{ path => '//div[@class="content"]', 'extract' => 'HTML' },
71 #{ path => '//div[@class="postdesc"]', 'extract' => 'HTML' },
72 #{ path => '//div[@class="article"]', 'extract' => 'HTML' },
73 #{ path => '//div[@class="post-ctc"]', 'extract' => 'HTML' },
74 #]);
75
76 my ($js) = $c=~m#<script>window\.__initialize_data__ =(.+?)<\/script>#s;
77 my $js_r = decode_json(encode("utf8", $js));
78 my $post = $js_r->{postData}{data}{postData}{postView};
79 $r->{title} = $post->{title};
80 $r->{content} = $post->{textPostView}{content};
81
82 return $r;
83 }
84
85 sub get_tiezi_ref {
86 my ( $self, $w_b, %opt ) = @_;
87
88 my $base_url = "http://$opt{writer}.lofter.com";
89 my $b = uc( unpack( "H*", encode( "utf8", $opt{book} ) ) );
90 $b =~ s/(..)/%$1/g;
91
92 my %iter_opt = (
93 #verbose => 1,
94 %opt,
95 reverse_item_list => 1,
96 info_sub => sub { { writer => $opt{writer}, book => $opt{book}, title => $opt{book} } },
97 item_list_sub => sub { $self->extract_content( $opt{book}, @_ ) },
98 stop_sub => sub { return; },
99 #item_sub => sub { $self->extract_item( @_ ) },
100 );
101
102 my $url = "$base_url/search/?q=$b";
103 my $next_search_sub = sub { $self->gen_next_search_url( @_ ) };
104 my ( $info, $item_list ) = $self->{browser}->request_url_whole(
105 $url,
106 %iter_opt,
107 next_page_sub => $next_search_sub,
108 );
109
110 my $tag_url = "$base_url/tag/$b";
111 my $next_tag_sub = sub { $self->gen_next_tag_url( @_ ) };
112 my ( $tag_info, $tag_item_list ) = $self->{browser}->request_url_whole(
113 $tag_url,
114 %iter_opt,
115 next_page_sub => $next_tag_sub,
116 );
117
118 my ($final_url, $next_page_sub, $final_item_list) = $#$tag_item_list>$#$item_list ?
119 ($tag_url, $next_tag_sub, $tag_item_list) : ($url, $next_search_sub, $item_list);
120
121 my ( $final_info, $dst_item_list ) = $self->{browser}->request_url_whole(
122 $final_url,
123 %iter_opt,
124 item_list => $final_item_list,
125 next_page_sub => $next_page_sub,
126 item_sub => sub { $self->extract_item( @_ ) },
127 reverse_item_list => 0,
128 );
129
130 $final_info->{url} = $final_url;
131 $final_info->{item_list} = $dst_item_list;
132 $self->filter_item_list($final_info);
133 #print "last_chapter_id : $info->{item_list}[-1]{id}\n";
134 return $final_info;
135 } ## end sub get_tiezi_ref
136
137 1;