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

filter: Statementstmt 6.0% 9 / 149 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.2% 24 / 235 CC 29 SCAR 32.9
CC
29
Cov
4%
parse_universe
72.0
parse_category
42.0
parse_chapter_urls
20.0
CRAP 26.7
« blib/lib/WWW/FetchStory.pm ↑ Summary blib/lib/WWW/FetchStory/Fetcher/RestrictedSection.pm »
1 package WWW::FetchStory::Fetcher::AO3;
2 1 $WWW::FetchStory::Fetcher::AO3::VERSION = '0.2001';
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.2001
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=3 SCAR=24.8
Pod documented
239 0 my %args = @_;
240  
241 0 my $content = $args{content};
242  
243 0 my $author = '';
244 0 ▶ if ($content =~ m!<a href="/users/\w+/pseuds/\w+" class="login author">([^<]+)</a>!)
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ m[<a href="/users/\w+/pseuds/\w+" class="login author">([^<]+)</a>]) { } 0 0 0
elsif ($content =~ m[&lt;a href=&quot;http://archiveofourown.org/users/\w+&quot;&gt;&lt;strong&gt;([-\w\s]+)&lt;/strong&gt;&lt;/a&gt;]) { } 0 0 0
245 {
246 0 $author = $1;
247 }
248 elsif ($content =~ m!&lt;a href=&quot;http://archiveofourown.org/users/\w+&quot;&gt;&lt;strong&gt;([-\w\s]+)&lt;/strong&gt;&lt;/a&gt;!)
249 {
250 0 $author = $1;
251 }
252 else
253 {
254 0 $author = $self->SUPER::parse_author(%args);
255 }
256 0 $author =~ s/_/ /g;
257 0 return $author;
258 } # parse_author
259  
260 =head2 parse_summary
261  
262 Get the summary.
263  
264 =cut
265 sub parse_summary {
266 0 ▶ my $self = shift;
Subroutine parse_summary: not called CC=2 SCAR=17.9
Pod documented
267 0 my %args = @_;
268  
269 0 my $content = $args{content};
270  
271 0 my $summary = '';
272 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
273 {
274 0 $summary = $1;
275 }
276 else
277 {
278 0 $summary = $self->SUPER::parse_summary(%args);
279 }
280 # AO3 tends to have messy HTML stuff stuck in the summary
281 0 $summary =~ s!&lt;[a-zA-Z]&gt;!!g;
282 0 $summary =~ s!&lt;/[a-zA-Z]&gt;!!g;
283 0 $summary =~ s!<\w+>!!g;
284 0 $summary =~ s!</\w+>!!g;
285 0 $summary =~ s!&#x27;!'!g;
286 0 $summary =~ s!&#39;!'!g;
287 0 return $summary;
288 } # parse_summary
289  
290 =head2 parse_wordcount
291  
292 Get the wordcount.
293  
294 =cut
295 sub parse_wordcount {
296 0 ▶ my $self = shift;
Subroutine parse_wordcount: not called CC=2 SCAR=17.9
Pod documented
297 0 my %args = @_;
298  
299 0 my $content = $args{content};
300  
301 0 my $words = '';
302 0 ▶ if ($content =~ m!\((\d+) words\)!m)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /\((\d+) words\)/m) 0 0 0
303 {
304 0 $words = $1;
305 }
306 0 return $words;
307 } # parse_wordcount
308  
309 =head2 parse_characters
310  
311 Get the characters.
312  
313 =cut
314 sub parse_characters {
315 0 ▶ my $self = shift;
Subroutine parse_characters: not called CC=4 SCAR=30.0
Pod documented
316 0 my %args = @_;
317  
318 0 my $content = $args{content};
319  
320 0 my $characters = '';
321 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
322 {
323 # multiple characters inside links
324 0 my $str = $1;
325 0 my @chars = ();
326 0 ▶ while ($str =~ m!([^><]+)</a>!g)
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[([^><]+)</a>]g) 0 0 0
327 {
328 0 push @chars, $1;
329 }
330 0 $characters = join(', ', @chars);
331 }
332 elsif ($content =~ m!^Characters: (.*?)$!m)
333 {
334 0 $characters = $1;
335 }
336 else
337 {
338 0 $characters = $self->SUPER::parse_characters(%args);
339 }
340 0 return $characters;
341 } # parse_characters
342  
343 =head2 parse_universe
344  
345 Get the universe.
346  
347 =cut
348 sub parse_universe {
349 0 ▶ my $self = shift;
Subroutine parse_universe: not called CC=8 SCAR=42.8
Pod documented
350 0 my %args = @_;
351  
352 0 my $content = $args{content};
353  
354 0 my $universe = '';
355 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
356 {
357 0 $universe = $1;
358 }
359 elsif ($content =~ m!^Fandom: (&lt;a href=&quot;https?://archiveofourown.org/tags/.*?,.*?)$!m)
360 {
361 0 my $fandoms = $1;
362 0 my @fds = split(/,/, $fandoms);
363 0 my @universes = ();
364 0 foreach my $fd (@fds)
365 {
366 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
367 {
368 0 push @universes, $1;
369 }
370 }
371 0 $universe = join(', ', @universes);
372 }
373 elsif ($content =~ m!^Fandom: &lt;a href=&quot;https?://archiveofourown.org/tags/.*?&quot;&gt;(.*?)&lt;/a&gt;$!m)
374 {
375 0 $universe = $1;
376 }
377 else
378 {
379 0 $universe = $self->SUPER::parse_universe(%args);
380 }
381 # Minor adjustments to AO3 tags
382 0 ▶ if ($universe =~ m!Harry Potter - J\. K\. Rowling!)
Branch: 2 branches
BranchTrueFalseTotal
if ($universe =~ /Harry Potter - J\. K\. Rowling/) { } 0 0 0
elsif ($universe =~ /(Doctor Who)/) { } 0 0 0
383 {
384 0 $universe =~ s/\s*-\s*J\. K\. Rowling//;
385 }
386 elsif ($universe =~ m!(Doctor Who)!)
387 {
388 0 $universe = $1;
389 }
390 0 return $universe;
391 } # parse_universe
392  
393 =head2 parse_category
394  
395 Get the category.
396  
397 =cut
398 sub parse_category {
399 0 ▶ my $self = shift;
Subroutine parse_category: not called CC=6 SCAR=37.4
Pod documented
400 0 my %args = @_;
401  
402 0 my $content = $args{content};
403  
404 0 my $category = '';
405 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
406 {
407 # multiple categories inside links
408 0 my $str = $1;
409 0 my @cats = ();
410 0 ▶ while ($str =~ m!([^><]+)</a>!g)
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ m[([^><]+)</a>]g) 0 0 0
411 {
412 0 push @cats, $1;
413 }
414 0 $category = join(', ', @cats);
415 }
416 elsif ($content =~ m!Additional Tags:\s*</dt>\s*<dd class="freeform tags">\s*<ul[^>]*>\s*(.*?)\s*</ul>!s)
417 {
418 0 my $categories = $1;
419 0 my @cats = split(/<li>/, $categories);
420 0 my @categories = ();
421 0 foreach my $cat (@cats)
422 {
423 0 ▶ if ($cat =~ m!class="tag">([^<]+)</a>!)
Branch: 1 branch
BranchTrueFalseTotal
if ($cat =~ m[class="tag">([^<]+)</a>]) 0 0 0
424 {
425 0 push @categories, $1;
426 }
427 }
428 0 $category = join(', ', @categories);
429 }
430 else
431 {
432 0 $category = $self->SUPER::parse_category(%args);
433 }
434 0 return $category;
435 } # parse_category
436  
437 1 1; # End of WWW::FetchStory::Fetcher::AO3
438 __END__
« blib/lib/WWW/FetchStory.pm ↑ Summary blib/lib/WWW/FetchStory/Fetcher/RestrictedSection.pm »