lib/Novel/Robot/Parser/tieba.pm untested

filter: Statementstmt 0.0% 0 / 51 Branchbran 0.0% 0 / 4 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 75 CC 3 SCAR 14.2
CC
3
Cov
0%
parse_novel_item
12.0
base_url
2.0
charset
2.0
CRAP 4.1
1 # ABSTRACT: http:://tieba.baidu.com
2 package Novel::Robot::Parser::tieba;
3 use strict;
4 use warnings;
5 use utf8;
6
7 use base 'Novel::Robot::Parser';
8
9 use HTML::Entities;
10 use Web::Scraper;
11
12 sub base_url { 'http://tieba.baidu.com' }
13
14 sub charset { 'utf8' }
15
16 sub site_type { 'tiezi' }
17
18 sub scrape_novel {
19 { title => { path => '//h3' },
20 writer => { path => '//li[@class="d_name"]' },
21 };
22 }
23
24 sub parse_novel_item {
25 my ( $self, $h ) = @_;
26
27 my $parse_query = scraper {
28 process '//div[contains(@class,"l_post ")]',
29 'items[]' => scraper {
30 process '.', 'info' => '@data-field';
31 process_first '//h1[@class="core_title_txt"]', 'title' => 'TEXT';
32 process_first '//li[@class="d_name"]', 'writer' => 'TEXT';
33 process_first '//div[contains(@class,"d_post_content ")]', content => 'HTML';
34 };
35 };
36 my $ref = $parse_query->scrape( $h );
37
38 my @items;
39 for my $f ( @{ $ref->{items} } ) {
40 next unless ( $f->{content} );
41 $f->{writer} ||= 'unknown';
42 if ( $f->{info} ) {
43 my $x = decode_entities( $f->{info} );
44 ( $f->{id} ) = $x =~ /"post_no":(\d+),/s;
45 ( $f->{time} ) = $x =~ /"date":"(.+?)",/s;
46 delete( $f->{info} );
47 }
48 push @items, $f;
49 }
50 return \@items;
51 } ## end sub parse_novel_item
52
53 sub parse_item_list {
54 my ( $self, $h ) = @_;
55 my $parse_item = scraper {
56 process_first '//link[@rel="canonical"]', 'base' => '@href';
57 process_first '//li[@class="l_reply_num"]', 'page' => 'TEXT';
58 };
59 my $ref = $parse_item->scrape( $h );
60 my ( $page ) = $ref->{page} =~ /共(\d+)页/s;
61 my @urls = map { "$ref->{base}?pn=$_" } ( 2 .. $page );
62 return \@urls;
63 }
64
65 1;