blib/lib/WWW/FetchStory/Fetcher/AO3.pm

filter: Statementstmt 5.9% 9 / 151 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 13.3% 2 / 15 Podpod 100% 13 / 13 totaltotal 10.1% 24 / 237 CC 29 SCAR 34.5
CC
29
Cov
4%
parse_universe
90.0
parse_category
42.0
parse_chapter_urls
20.0
CRAP 31.6
« blib/lib/WWW/FetchStory/Fetcher/HPAdultFanfiction.pm ↑ Summary blib/lib/WWW/FetchStory.pm »
1 package WWW::FetchStory::Fetcher::AO3;
2 1 $WWW::FetchStory::Fetcher::AO3::VERSION = '0.2004';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 =head1 NAME
6  
7 WWW::FetchStory::Fetcher::AO3 - fetching module for WWW::FetchStory
8  
9 =head1 VERSION
10  
11 version 0.2004
12  
13 =head1 DESCRIPTION
14  
15 This is the AO3 story-fetching plugin for WWW::FetchStory.
16  
17 =cut
18  
19 1 our @ISA = qw(WWW::FetchStory::Fetcher);
20  
21 =head1 METHODS
22  
23 =head2 info
24  
25 Information about the fetcher.
26  
27 $info = $self->info();
28  
29 =cut
30  
31 sub info {
32 0 ▶ my $self = shift;
Subroutine info: not called CC=1 SCAR=6.9
Pod documented
33
34 0 my $info = "http://www.archiveofourown.org AO3 General fanfic archive";
35  
36 0 return $info;
37 } # info
38  
39 =head2 priority
40  
41 The priority of this fetcher. Fetchers with higher priority
42 get tried first. This is useful where there may be a generic
43 fetcher for a particular site, and then a more specialized fetcher
44 for particular sections of a site. For example, there may be a
45 generic AO3 fetcher, and then refinements for particular
46 AO3 community, such as the sshg_exchange community.
47 This works as either a class function or a method.
48  
49 This must be overridden by the specific fetcher class.
50  
51 $priority = $self->priority();
52  
53 $priority = WWW::FetchStory::Fetcher::priority($class);
54  
55 =cut
56  
57 sub priority {
58 0 ▶ my $class = shift;
Subroutine priority: not called CC=1 SCAR=6.9
Pod documented
59  
60 0 return 1;
61 } # priority
62  
63 =head2 allow
64  
65 If this fetcher can be used for the given URL, then this returns
66 true.
67 This must be overridden by the specific fetcher class.
68  
69 if ($obj->allow($url))
70 {
71 ....
72 }
73  
74 =cut
75  
76 sub allow {
77 0 ▶ my $self = shift;
Subroutine allow: not called CC=2 SCAR=17.9
Pod documented
78 0 my $url = shift;
79  
80 0 ▶ return ($url =~ /archiveofourown\.org/ || $url =~ /ao3\.org/);
Condition0%0/3
$url =~ /archiveofourown\.org/ || $url =~ /ao3\.org/
l!l&&r!l&&!r
000
MC/DC0%0/2
$url =~ /archiveofourown\.org/ || $url =~ /ao3\.org/
$url =~ /archiveofourown\.org/ $url =~ /ao3\.org/
Truth table0%0/3
A || B
A= $url =~ /archiveofourown\.org/B= $url =~ /ao3\.org/
ABresult
1X1
011
000
81 } # allow
82  
83 =head1 Private Methods
84  
85 =head2 parse_toc
86  
87 Parse the table-of-contents file.
88  
89 %info = $self->parse_toc(content=>$content,
90 url=>$url,
91 urls=>\@urls);
92  
93 This should return a hash containing:
94  
95 =over
96  
97 =item chapters
98  
99 An array of URLs for the chapters of the story. In the case where the
100 story only takes one page, that will be the chapter.
101 In the case where multiple URLs have been passed in, it will be those URLs.
102  
103 =item title
104  
105 The title of the story.
106  
107 =back
108  
109 It may also return additional information, such as Summary.
110  
111 =cut
112  
113 sub parse_toc {
114 0 ▶ my $self = shift;
Subroutine parse_toc: not called CC=3 SCAR=24.8
Pod documented
115 0 my %args = (
116 url=>'',
117 content=>'',
118 @_
119 );
120  
121 0 my $content = $args{content};
122 0 my %info = ();
123 0 $info{url} = $args{url};
124  
125 0 my $sid='';
126 0 ▶ if ($args{url} =~ m#archiveofourown.org/works/(\d+)#)
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'url'} =~ m[archiveofourown.org/works/(\d+)]) { } 0 0 0
127 {
128 0 $sid = $1;
129 }
130 else
131 {
132 0 print STDERR "did not find SID for $args{url}";
133 0 return $self->SUPER::parse_toc(%args);
134 }
135  
136 0 $info{title} = $self->parse_title(%args);
137 0 $info{author} = $self->parse_author(%args);
138 0 $info{summary} = $self->parse_summary(%args);
139 0 $info{characters} = $self->parse_characters(%args);
140 0 $info{universe} = $self->parse_universe(%args);
141 0 $info{category} = $self->parse_category(%args);
142 0 $info{rating} = $self->parse_rating(%args);
143 0 $info{chapters} = $self->parse_chapter_urls(%args, sid=>$sid);
144 #$info{epub_url} = $self->parse_epub_url(%args, sid=>$sid);
145 0 ▶ if ($info{epub_url})
Branch: 1 branch
BranchTrueFalseTotal
if ($info{"epub_url"}) 0 0 0
146 {
147 0 $info{wordcount} = $self->parse_wordcount(%args);
148 }
149  
150 0 return %info;
151 } # parse_toc
152  
153 =head2 parse_chapter_urls
154  
155 Figure out the URLs for the chapters of this story.
156  
157 =cut
158 sub parse_chapter_urls {
159 0 ▶ my $self = shift;
Subroutine parse_chapter_urls: not called CC=4 SCAR=30.0
Pod documented
160 0 my %args = (
161 urls=>undef,
162 content=>'',
163 @_
164 );
165 0 my $content = $args{content};
166 0 my $sid = $args{sid};
167 0 my @chapters = ();
168 0 ▶ if (defined $args{urls})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"urls"}) 0 0 0
169 {
170 0 @chapters = @{$args{urls}};
171 }
172 0 ▶ if (@chapters == 1
Branch: 1 branch
BranchTrueFalseTotal
if (@chapters == 1 and $content =~ m[href="(/downloads/[-\/\w]+/$sid/[^.]+\.html)]) 0 0 0
Condition0%0/3
@chapters == 1 and $content =~ m[href="(/downloads/[-\/\w]+/$sid/[^.]+\.html)]
!ll&&!rl&&r
000
MC/DC0%0/2
@chapters == 1 and $content =~ m[href="(/downloads/[-\/\w]+/$sid/[^.]+\.html)]
@chapters == 1 $content =~ m[href="(/downloads/[-\/\w]+/$sid/[^.]+\.html)]
Truth table0%0/3
A and B
A= @chapters == 1B= $content =~ m[href="(/downloads/[-\/\w]+/$sid/[^.]+\.html)]
ABresult
0X0
100
111
173 and $content =~ m!href="(/downloads/[-\/\w]+/$sid/[^.]+\.html)!)
174 {
175 0 @chapters = ("http://archiveofourown.org$1");
176 }
177  
178 0 return \@chapters;
179 } # parse_chapter_urls
180  
181 =head2 parse_epub_url
182  
183 Figure out the URL for the EPUB version of this story.
184  
185 =cut
186 sub parse_epub_url {
187 0 ▶ my $self = shift;
Subroutine parse_epub_url: not called CC=2 SCAR=17.9
Pod documented
188 0 my %args = (
189 url=>'',
190 content=>'',
191 @_
192 );
193 0 my $content = $args{content};
194 0 my $sid = $args{sid};
195 0 my $epub_url = '';
196 0 ▶ if ($content =~ m!href="(/downloads/[-\/\w]+/$sid/[^.]+\.epub)!)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[href="(/downloads/[-\/\w]+/$sid/[^.]+\.epub)]) 0 0 0
197 {
198 0 $epub_url = ("http://archiveofourown.org$1");
199 }
200  
201 0 return $epub_url;
202 } # parse_epub_url
203  
204 =head2 parse_title
205  
206 Get the title.
207  
208 =cut
209 sub parse_title {
210 0 ▶ my $self = shift;
Subroutine parse_title: not called CC=3 SCAR=24.8
Pod documented
211 0 my %args = @_;
212  
213 0 my $content = $args{content};
214  
215 0 my $title = '';
216 0 ▶ if ($content =~ m!<h2 class="title heading">\s*([^<]*)</h2>!s)
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ m[<h2 class="title heading">\s*([^<]*)</h2>]s) { } 0 0 0
elsif ($content =~ m[<h1>([^<]*)</h1>]) { } 0 0 0
217 {
218 0 $title = $1;
219 0 $title =~ s/\s*$//s; # remove any trailing whitespace
220 }
221 elsif ($content =~ m!<h1>([^<]*)</h1>!)
222 {
223 0 $title = $1;
224 }
225 else
226 {
227 0 $title = $self->SUPER::parse_title(%args);
228 }
229 0 return $title;
230 } # parse_title
231  
232 =head2 parse_author
233  
234 Get the author.
235  
236 =cut
237 sub parse_author {
238 0 ▶ my $self = shift;
Subroutine parse_author: not called CC=2 SCAR=17.9
Pod documented
239 0 my %args = @_;
240  
241 0 my $content = $args{content};
242  
243 0 say STDERR "start of parse_author";
244 0 my $author = '';
245 0 ▶ if ($content =~ m! href="/users/\w+/pseuds/\w+">([^<]+)</a>!)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[ href="/users/\w+/pseuds/\w+">([^<]+)</a>]) { } 0 0 0
246 {
247 0 $author = $1;
248 }
249 else
250 {
251 0 $author = $self->SUPER::parse_author(%args);
252 }
253 0 $author =~ s/_/ /g;
254 0 return $author;
255 } # parse_author
256  
257 =head2 parse_summary
258  
259 Get the summary.
260  
261 =cut
262 sub parse_summary {
263 0 ▶ my $self = shift;
Subroutine parse_summary: not called CC=2 SCAR=17.9
Pod documented
264 0 my %args = @_;
265  
266 0 my $content = $args{content};
267  
268 0 my $summary = '';
269 0 ▶ if ($content =~ m!<h3[^>]*>Summary:</h3>\s*<blockquote class="userstuff"><p>([^<]+)</p></blockquote>!s)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[<h3[^>]*>Summary:</h3>\s*<blockquote class="userstuff"><p>([^<]+)</p></blockquote>]s) { } 0 0 0
270 {
271 0 $summary = $1;
272 }
273 else
274 {
275 0 $summary = $self->SUPER::parse_summary(%args);
276 }
277 # AO3 tends to have messy HTML stuff stuck in the summary
278 0 $summary =~ s!&lt;[a-zA-Z]&gt;!!g;
279 0 $summary =~ s!&lt;/[a-zA-Z]&gt;!!g;
280 0 $summary =~ s!<\w+>!!g;
281 0 $summary =~ s!</\w+>!!g;
282 0 $summary =~ s!&#x27;!'!g;
283 0 $summary =~ s!&#39;!'!g;
284 0 return $summary;
285 } # parse_summary
286  
287 =head2 parse_wordcount
288  
289 Get the wordcount.
290  
291 =cut
292 sub parse_wordcount {
293 0 ▶ my $self = shift;
Subroutine parse_wordcount: not called CC=2 SCAR=17.9
Pod documented
294 0 my %args = @_;
295  
296 0 my $content = $args{content};
297  
298 0 my $words = '';
299 0 ▶ if ($content =~ m!\((\d+) words\)!m)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\((\d+) words\)/m) 0 0 0
300 {
301 0 $words = $1;
302 }
303 0 return $words;
304 } # parse_wordcount
305  
306 =head2 parse_characters
307  
308 Get the characters.
309  
310 =cut
311 sub parse_characters {
312 0 ▶ my $self = shift;
Subroutine parse_characters: not called CC=4 SCAR=30.0
Pod documented
313 0 my %args = @_;
314  
315 0 my $content = $args{content};
316  
317 0 my $characters = '';
318 0 ▶ if ($content =~ m!<dd class="character tags">(.*?)</dd>!s)
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ m[<dd class="character tags">(.*?)</dd>]s) { } 0 0 0
elsif ($content =~ /^Characters: (.*?)$/m) { } 0 0 0
319 {
320 # multiple characters inside links
321 0 my $str = $1;
322 0 my @chars = ();
323 0 ▶ while ($str =~ m!([^><]+)</a>!g)
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[([^><]+)</a>]g) 0 0 0
324 {
325 0 push @chars, $1;
326 }
327 0 $characters = join(', ', @chars);
328 }
329 elsif ($content =~ m!^Characters: (.*?)$!m)
330 {
331 0 $characters = $1;
332 }
333 else
334 {
335 0 $characters = $self->SUPER::parse_characters(%args);
336 }
337 # Remove the (Universe) part of the characters
338 0 $characters =~ s!\s*\([^)]+\)!!g;
339 0 return $characters;
340 } # parse_characters
341  
342 =head2 parse_universe
343  
344 Get the universe.
345  
346 =cut
347 sub parse_universe {
348 0 ▶ my $self = shift;
Subroutine parse_universe: not called CC=9 SCAR=45.0
Pod documented
349 0 my %args = @_;
350  
351 0 my $content = $args{content};
352  
353 0 my $universe = '';
354 0 ▶ if ($content =~ m!Fandom: &lt;a href=&quot;https?://archiveofourown\.org/tags/.*?&quot;&gt;(.*?)&lt;/a&gt!)
Branch: 3 branches
BranchTrueFalseTotal
if ($content =~ m[Fandom: &lt;a href=&quot;https?://archiveofourown\.org/tags/.*?&quot;&gt;(.*?)&lt;/a&gt]) { } 0 0 0
elsif ($content =~ m[^Fandom: (&lt;a href=&quot;https?://archiveofourown.org/tags/.*?,.*?)$]m) { } 0 0 0
elsif ($content =~ m[^Fandom: &lt;a href=&quot;https?://archiveofourown.org/tags/.*?&quot;&gt;(.*?)&lt;/a&gt;$]m) { } 0 0 0
355 {
356 0 $universe = $1;
357 }
358 elsif ($content =~ m!^Fandom: (&lt;a href=&quot;https?://archiveofourown.org/tags/.*?,.*?)$!m)
359 {
360 0 my $fandoms = $1;
361 0 my @fds = split(/,/, $fandoms);
362 0 my @universes = ();
363 0 foreach my $fd (@fds)
364 {
365 0 ▶ if ($fd =~ m!&lt;a href=&quot;https?://archiveofourown.org/tags/.*?&quot;&gt;(.*?)&lt;/a&gt;!m)
Branch: 1 branch
BranchTrueFalseTotal
if ($fd =~ m[&lt;a href=&quot;https?://archiveofourown.org/tags/.*?&quot;&gt;(.*?)&lt;/a&gt;]m) 0 0 0
366 {
367 0 push @universes, $1;
368 }
369 }
370 0 $universe = join(', ', @universes);
371 }
372 elsif ($content =~ m!^Fandom: &lt;a href=&quot;https?://archiveofourown.org/tags/.*?&quot;&gt;(.*?)&lt;/a&gt;$!m)
373 {
374 0 $universe = $1;
375 }
376 else
377 {
378 0 $universe = $self->SUPER::parse_universe(%args);
379 }
380 # Minor adjustments to AO3 tags
381 0 ▶ if ($universe =~ m!Harry Potter - J\. K\. Rowling!)
Branch: 3 branches
BranchTrueFalseTotal
if ($universe =~ /Harry Potter - J\. K\. Rowling/) { } 0 0 0
elsif ($universe =~ /(Doctor Who)/) { } 0 0 0
elsif ($universe =~ /Blake&amp;#39;s 7/) { } 0 0 0
382 {
383 0 $universe =~ s/\s*-\s*J\. K\. Rowling//;
384 }
385 elsif ($universe =~ m!(Doctor Who)!)
386 {
387 0 $universe = $1;
388 }
389 elsif ($universe =~ m!Blake&amp;#39;s 7!)
390 {
391 0 $universe = 'Blakes 7';
392 }
393 0 return $universe;
394 } # parse_universe
395  
396 =head2 parse_category
397  
398 Get the category.
399  
400 =cut
401 sub parse_category {
402 0 ▶ my $self = shift;
Subroutine parse_category: not called CC=6 SCAR=37.4
Pod documented
403 0 my %args = @_;
404  
405 0 my $content = $args{content};
406  
407 0 my $category = '';
408 0 ▶ if ($content =~ m!<dd class="freeform tags">(.*?)</dd>!s)
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ m[<dd class="freeform tags">(.*?)</dd>]s) { } 0 0 0
elsif ($content =~ m[Additional Tags:\s*</dt>\s*<dd class="freeform tags">\s*<ul[^>]*>\s*(.*?)\s*</ul>]s) { } 0 0 0
409 {
410 # multiple categories inside links
411 0 my $str = $1;
412 0 my @cats = ();
413 0 ▶ while ($str =~ m!([^><]+)</a>!g)
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[([^><]+)</a>]g) 0 0 0
414 {
415 0 push @cats, $1;
416 }
417 0 $category = join(', ', @cats);
418 }
419 elsif ($content =~ m!Additional Tags:\s*</dt>\s*<dd class="freeform tags">\s*<ul[^>]*>\s*(.*?)\s*</ul>!s)
420 {
421 0 my $categories = $1;
422 0 my @cats = split(/<li>/, $categories);
423 0 my @categories = ();
424 0 foreach my $cat (@cats)
425 {
426 0 ▶ if ($cat =~ m!class="tag">([^<]+)</a>!)
Branch: 1 branch
BranchTrueFalseTotal
if ($cat =~ m[class="tag">([^<]+)</a>]) 0 0 0
427 {
428 0 push @categories, $1;
429 }
430 }
431 0 $category = join(', ', @categories);
432 }
433 else
434 {
435 0 $category = $self->SUPER::parse_category(%args);
436 }
437 0 return $category;
438 } # parse_category
439  
440 1 1; # End of WWW::FetchStory::Fetcher::AO3
441 __END__
« blib/lib/WWW/FetchStory/Fetcher/HPAdultFanfiction.pm ↑ Summary blib/lib/WWW/FetchStory.pm »