File Coverage

blib/lib/WWW/FetchStory.pm
Criterion Covered Total %
statement 12 53 22.6
branch 0 12 0.0
condition 0 12 0.0
subroutine 4 7 57.1
pod 3 3 100.0
total 19 87 21.8


line stmt bran cond sub pod time code
1 1     1   234830 use strict;
  1         3  
  1         48  
2 1     1   6 use warnings;
  1         3  
  1         135  
3              
4             package WWW::FetchStory;
5             $WWW::FetchStory::VERSION = '0.2602';
6             =head1 NAME
7              
8             WWW::FetchStory - Fetch a story from a fiction website
9              
10             =head1 VERSION
11              
12             version 0.2602
13              
14             =head1 SYNOPSIS
15              
16             use WWW::FetchStory;
17              
18             my $obj = WWW::FetchStory->new(%args);
19              
20             my %story_info = $obj->fetch_story(urls=>\@urls);
21              
22             =head1 DESCRIPTION
23              
24             This will fetch a story from a fiction website, intelligently
25             dealing with the formats from various different fiction websites
26             such as fanfiction.net; it deals with multi-file stories,
27             and strips all the extras from the HTML (such as navbars and javascript)
28             so that all you get is the story text and its formatting.
29              
30             =head2 Fetcher Plugins
31              
32             In order to tidy the HTML and parse the pages for data about the story,
33             site-specific "Fetcher" plugins are required.
34              
35             These are in the namespace 'WWW::FetchStory::Fetcher'; a fetcher for the Foo site
36             would be called 'WWW::FetchStory::Fetcher::Foo'.
37              
38             =cut
39              
40 1     1   3532 use WWW::FetchStory::Fetcher;
  1         3  
  1         59  
41 1         7 use Module::Pluggable instantiate => 'new',
42             search_path => ['WWW::FetchStory::Fetcher'],
43 1     1   2108 sub_name => 'fetchers';
  1         14423  
44              
45             =head1 METHODS
46              
47             =head2 new
48              
49             Create a new object, setting global values for the object.
50              
51             my $obj = WWW::FetchStory->new();
52              
53             =cut
54              
55             sub new {
56 0     0 1   my $class = shift;
57 0           my %parameters = (@_);
58 0   0       my $self = bless ({%parameters}, ref ($class) || $class);
59              
60             # ---------------------------------------
61             # Fetchers
62             # find out what fetchers are available, and group them by priority
63 0           $self->{fetch_pri} = {};
64 0           my @fetchers = $self->fetchers();
65 0           foreach my $fe (@fetchers)
66             {
67 0           my $priority = $fe->priority();
68 0           my $name = $fe->name();
69 0 0         if ($self->{debug})
70             {
71 0           print STDERR "fetcher=$name($priority)\n";
72             }
73 0 0         if (!exists $self->{fetch_pri}->{$priority})
74             {
75 0           $self->{fetch_pri}->{$priority} = [];
76             }
77 0           push @{$self->{fetch_pri}->{$priority}}, $fe;
  0            
78             }
79              
80 0           return ($self);
81             } # new
82              
83             =head2 fetch_story
84              
85             my %story_info = fetch_story(
86             urls=>\@urls,
87             verbose=>0,
88             toc=>0);
89              
90             =cut
91             sub fetch_story ($%) {
92 0     0 1   my $self = shift;
93 0           my %args = (
94             urls=>undef,
95             verbose=>0,
96             toc=>0,
97             @_
98             );
99              
100 0           my $fetcher;
101 0           my $first_url = $args{urls}[0];
102 0           foreach my $pri (reverse sort keys %{$self->{fetch_pri}})
  0            
103             {
104 0           foreach my $fe (@{$self->{fetch_pri}->{$pri}})
  0            
105             {
106 0 0 0       if ($fe->allow($first_url)
      0        
      0        
107             # the URL might be a file, check rurl
108             or (-f $first_url and $args{rurl} and $fe->allow($args{rurl}))
109             )
110             {
111 0           $fetcher = $fe;
112 0 0         warn "Fetcher($pri): ", $fe->name(), "\n" if $args{verbose};
113 0           last;
114             }
115             }
116 0 0         if (defined $fetcher)
117             {
118 0           last;
119             }
120             }
121 0 0         if (defined $fetcher)
122             {
123 0           $fetcher->init(%{$self});
  0            
124 0           return $fetcher->fetch(%args);
125             }
126              
127 0           return undef;
128             } # fetch_story
129              
130             =head2 list_fetchers
131              
132             my %fetchers = list_fetchers();
133              
134             =cut
135             sub list_fetchers ($%) {
136 0     0 1   my $self = shift;
137 0           my %args = (
138             verbose=>0,
139             @_
140             );
141              
142 0           my %fetchers = ();
143 0           my @all_fetchers = $self->fetchers();
144 0           foreach my $fe (@all_fetchers)
145             {
146 0           $fetchers{$fe->name()} = $fe->info();
147             }
148 0           return %fetchers;
149              
150             } # list_fetchers
151              
152             =head1 BUGS
153              
154             Please report any bugs or feature requests to the author.
155              
156             =cut
157              
158             1; # End of Text::ParseStory
159             __END__