| 1 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | package WWW::FetchStory; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 1 | $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 | ▶ | use WWW::FetchStory::Fetcher; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 1 | ▶ | use Module::Pluggable instantiate => 'new', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | search_path => ['WWW::FetchStory::Fetcher'], | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 1 | sub_name => 'fetchers'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub new { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | my %parameters = (@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $self = bless ({%parameters}, ref ($class) || $class); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table0%0/3
A || B
A= ref $classB= $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 | ▶ | if ($self->{debug}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | print STDERR "fetcher=$name($priority)\n"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (!exists $self->{fetch_pri}->{$priority}) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 0 | $self->{fetch_pri}->{$priority} = []; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | push @{$self->{fetch_pri}->{$priority}}, $fe; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub fetch_story ($%) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fetch_story: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | 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}}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | foreach my $fe (@{$self->{fetch_pri}->{$pri}}) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if ($fe->allow($first_url) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/9
$fe->allow($first_url) or -f $first_url and $args{"rurl"} and $fe->allow($args{"rurl"})
-f $first_url and $args{"rurl"} and $fe->allow($args{"rurl"})
-f $first_url and $args{"rurl"}
MC/DC0%0/4
$fe->allow($first_url) or -f $first_url and $args{"rurl"} and $fe->allow($args{"rurl"})
$fe->allow($first_url)
-f $first_url
$args{"rurl"}
$fe->allow($args{"rurl"})
Truth table0%0/5
A or B and C and D
A= $fe->allow($first_url)B= -f $first_urlC= $args{"rurl"}D= $fe->allow($args{"rurl"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | ▶ | warn "Fetcher($pri): ", $fe->name(), "\n" if $args{verbose}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if (defined $fetcher) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | if (defined $fetcher) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | $fetcher->init(%{$self}); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub list_fetchers ($%) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_fetchers: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | 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 | 1; # End of Text::ParseStory | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||