File Coverage

blib/lib/WWW/FetchStory/Fetcher/PetulantPoetess.pm
Criterion Covered Total %
statement 9 62 14.5
branch 0 12 0.0
condition n/a
subroutine 3 9 33.3
pod 6 6 100.0
total 18 89 20.2


line stmt bran cond sub pod time code
1             package WWW::FetchStory::Fetcher::PetulantPoetess;
2             $WWW::FetchStory::Fetcher::PetulantPoetess::VERSION = '0.2602';
3 1     1   210969 use strict;
  1         2  
  1         31  
4 1     1   4 use warnings;
  1         1  
  1         61  
5             =head1 NAME
6              
7             WWW::FetchStory::Fetcher::PetulantPoetess - fetching module for WWW::FetchStory
8              
9             =head1 VERSION
10              
11             version 0.2602
12              
13             =head1 DESCRIPTION
14              
15             This is the PetulantPoetess story-fetching plugin for WWW::FetchStory.
16              
17             =cut
18              
19 1     1   5 use parent qw(WWW::FetchStory::Fetcher);
  1         1  
  1         7  
20              
21             =head2 info
22              
23             Information about the fetcher.
24              
25             $info = $self->info();
26              
27             =cut
28              
29             sub info {
30 0     0 1   my $self = shift;
31            
32 0           my $info = "(http://www.thepetulantpoetess.com/) A Harry Potter fiction archive.";
33              
34 0           return $info;
35             } # info
36              
37             =head2 priority
38              
39             The priority of this fetcher. Fetchers with higher priority
40             get tried first. This is useful where there may be a generic
41             fetcher for a particular site, and then a more specialized fetcher
42             for particular sections of a site. For example, there may be a
43             generic PetulantPoetess fetcher, and then refinements for particular
44             PetulantPoetess community, such as the sshg_exchange community.
45             This works as either a class function or a method.
46              
47             This must be overridden by the specific fetcher class.
48              
49             $priority = $self->priority();
50              
51             $priority = WWW::FetchStory::Fetcher::priority($class);
52              
53             =cut
54              
55             sub priority {
56 0     0 1   my $class = shift;
57              
58 0           return 1;
59             } # priority
60              
61             =head2 allow
62              
63             If this fetcher can be used for the given URL, then this returns
64             true.
65             This must be overridden by the specific fetcher class.
66              
67             if ($obj->allow($url))
68             {
69             ....
70             }
71              
72             =cut
73              
74             sub allow {
75 0     0 1   my $self = shift;
76 0           my $url = shift;
77              
78 0           return ($url =~ /thepetulantpoetess\.com/);
79             } # allow
80              
81             =head1 Private Methods
82              
83             =head2 parse_toc
84              
85             Parse the table-of-contents file.
86              
87             %info = $self->parse_toc(content=>$content,
88             url=>$url,
89             urls=>\@urls);
90              
91             This should return a hash containing:
92              
93             =over
94              
95             =item chapters
96              
97             An array of URLs for the chapters of the story. In the case where the
98             story only takes one page, that will be the chapter.
99             In the case where multiple URLs have been passed in, it will be those URLs.
100              
101             =item title
102              
103             The title of the story.
104              
105             =back
106              
107             It may also return additional information, such as Summary.
108              
109             =cut
110              
111             sub parse_toc {
112 0     0 1   my $self = shift;
113 0           my %args = (
114             url=>'',
115             content=>'',
116             @_
117             );
118              
119 0           my %info = ();
120 0           my $content = $args{content};
121 0           my @chapters = ();
122 0           $info{url} = $args{url};
123 0           my $sid='';
124 0 0         if ($args{url} =~ m#sid=(\d+)#)
125             {
126 0           $sid = $1;
127             }
128             else
129             {
130 0           return $self->SUPER::parse_toc(%args);
131             }
132              
133 0           $info{title} = $self->parse_title(%args);
134 0           $info{author} = $self->parse_author(%args);
135 0           $info{summary} = $self->parse_summary(%args);
136 0           $info{characters} = $self->parse_characters(%args);
137 0           $info{universe} = 'Harry Potter';
138 0           $info{chapters} = $self->parse_chapter_urls(%args, sid=>$sid);
139              
140 0           return %info;
141             } # parse_toc
142              
143             =head2 parse_chapter_urls
144              
145             Figure out the URLs for the chapters of this story.
146              
147             =cut
148             sub parse_chapter_urls {
149 0     0 1   my $self = shift;
150 0           my %args = (
151             url=>'',
152             content=>'',
153             @_
154             );
155 0           my $content = $args{content};
156 0           my $sid = $args{sid};
157 0           my @chapters = ();
158 0 0         if (defined $args{urls})
159             {
160 0           @chapters = @{$args{urls}};
  0            
161             }
162 0 0         if (@chapters == 1)
163             {
164 0           @chapters = ();
165             # PetulantPoetess does not have a sane chapter system
166 0           my $fmt = 'http://www.thepetulantpoetess.com/viewstory.php?action=printable&sid=%d';
167 0           my $prev_sid = '';
168 0           while ($content =~ m#viewstory.php\?sid=(\d+)#sg)
169             {
170 0           my $ch_sid = $1;
171             # single-chapter stories end up with the same url multiple times
172 0 0         if ($ch_sid ne $prev_sid)
173             {
174 0           my $ch_url = sprintf($fmt, $ch_sid);
175 0 0         warn "chapter=$ch_url\n" if ($self->{verbose} > 1);
176 0           push @chapters, $ch_url;
177 0           $prev_sid = $ch_sid;
178             }
179             }
180             }
181              
182 0           return \@chapters;
183             } # parse_chapter_urls
184              
185             =head2 parse_author
186              
187             Get the author from the content
188              
189             =cut
190             sub parse_author {
191 0     0 1   my $self = shift;
192 0           my %args = (
193             url=>'',
194             content=>'',
195             @_
196             );
197              
198 0           my $content = $args{content};
199 0           my $author = '';
200 0 0         if ($content =~ m#<a href="viewuser.php\?uid=\d+">([^<]+)</a>#s)
201             {
202 0           $author = $1;
203             }
204             else
205             {
206 0           $author = $self->SUPER::parse_author(%args);
207             }
208 0           return $author;
209             } # parse_author
210              
211             1; # End of WWW::FetchStory::Fetcher::PetulantPoetess
212             __END__