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

filter: Statementstmt 7.3% 9 / 123 Branchbran 0.0% 0 / 66 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 20.0% 2 / 10 Podpod 100% 8 / 8 totaltotal 8.6% 19 / 221 CC 37 SCAR 53.6
CC
37
Cov
5%
extract_story
380.0
parse_chapter_urls
182.0
parse_toc
30.0
CRAP 212.7
« blib/lib/WWW/FetchStory/Fetcher/Teaspoon.pm ↑ Summary blib/lib/WWW/FetchStory/Fetcher/FanfictionNet.pm »
1 package WWW::FetchStory::Fetcher::Dreamwidth;
2 1 $WWW::FetchStory::Fetcher::Dreamwidth::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::Dreamwidth - fetching module for WWW::FetchStory
8  
9 =head1 VERSION
10  
11 version 0.2001
12  
13 =head1 DESCRIPTION
14  
15 This is the Dreamwidth 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.dreamwidth.org) Journalling site where some post their fiction.";
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 Dreamwidth fetcher, and then refinements for particular
46 Dreamwidth 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=1 SCAR=6.9
Pod documented
78 0 my $url = shift;
79  
80 0 return ($url =~ /\.dreamwidth\.org/);
81 } # allow
82  
83 =head1 Private Methods
84  
85 =head2 extract_story
86  
87 Extract the story-content from the fetched content.
88  
89 my ($story, $title) = $self->extract_story(content=>$content,
90 title=>$title);
91  
92 =cut
93  
94 sub extract_story {
95 0 ▶ my $self = shift;
Subroutine extract_story: not called CC=19 SCAR=59.4
Pod documented
96 0 my %args = (
97 content=>'',
98 title=>'',
99 @_
100 );
101 0 my $content = $args{content};
102  
103 0 my $user= '';
104 0 my $title = '';
105 0 my $url = '';
106 0 ▶ if ($content =~ m#<title>([\w]+):\s*([^<]+)</title>#s)
Branch: 2 branches
BranchTrueFalseTotal
if ($content =~ m[<title>([\w]+):\s*([^<]+)</title>]s) { } 0 0 0
elsif ($content =~ m[<title>([^<]+)</title>]s) { } 0 0 0
107 {
108 0 $user= $1;
109 0 $title = $2;
110 }
111 elsif ($content =~ m#<title>([^<]+)</title>#s)
112 {
113 0 $title = $1;
114 }
115 0 ▶ if ($content =~ m#([-\w]+)</b></a></span>\) wrote in <span class='ljuser'#s)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[([-\w]+)</b></a></span>\) wrote in <span class='ljuser']s) 0 0 0
116 {
117 0 $user = $1;
118 }
119  
120 0 my $year = '';
121 0 my $month = '';
122 0 my $day = '';
123 0 ▶ if ($content =~ m#wrote,<br /><font[^>]+>\@\s*<a href="[^"]+">(\d+)</a>-<a href="[^"]+">(\d+)</a>-<a href="[^"]+">(\d+)</a>#s)
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[wrote,<br /><font[^>]+>\@\s*<a href="[^"]+">(\d+)</a>-<a href="[^"]+">(\d+)</a>-<a href="[^"]+">(\d+)</a>]s) 0 0 0
124 {
125 0 $year = $1;
126 0 $month = $2;
127 0 $day = $3;
128 0 ▶ warn "year=$year,month=$month,day=$day\n" if ($self->{verbose} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
129 }
130  
131 0 ▶ if (!$url)
Branch: 1 branch
BranchTrueFalseTotal
unless ($url) 0 0 0
132 {
133 0 ▶ if ($content =~ m#<a[^>]*href=["']([^?\s]+)\?mode=reply["']\s*>Post a new comment#s)
Branch: 3 branches
BranchTrueFalseTotal
if ($content =~ /<a[^>]*href=["']([^?\s]+)\?mode=reply["']\s*>Post a new comment/s) { } 0 0 0
elsif ($content =~ /<a[^>]*href=["']([^?\s]+)\?mode=reply/s) { } 0 0 0
elsif ($content =~ m[<a[^>]*href="([^?\s]+)"\s*>Link</a>]s) { } 0 0 0
134 {
135 0 $url = $1;
136 }
137 elsif ($content =~ m#<a[^>]*href=["']([^?\s]+)\?mode=reply#s)
138 {
139 0 $url = $1;
140 }
141 elsif ($content =~ m#<a[^>]*href="([^?\s]+)"\s*>Link</a>#s)
142 {
143 0 $url = $1;
144 }
145 }
146 0 ▶ if (!$user && $url && ($url =~ m#http://([-\w]+)\.dreamwidth\.org#s))
Branch: 1 branch
BranchTrueFalseTotal
if (not $user and $url and $url =~ m[http://([-\w]+)\.dreamwidth\.org]s) 0 0 0
Condition0%0/6
not $user and $url and $url =~ m[http://([-\w]+)\.dreamwidth\.org]s
!ll&&!rl&&r
000
not $user and $url
!ll&&!rl&&r
000
MC/DC0%0/3
not $user and $url and $url =~ m[http://([-\w]+)\.dreamwidth\.org]s
not $user $url $url =~ m[http://([-\w]+)\.dreamwidth\.org]s
Truth table0%0/4
not(A) and B and C
A= not $userB= $urlC= $url =~ m[http://([-\w]+)\.dreamwidth\.org]s
ABCresult
0XX0
10X0
1100
1111
147 {
148 0 $user = $1;
149 }
150  
151 0 my $story = '';
152 0 ▶ if ($content =~ m#<div id='entrysubj'>(.*?)<div id='Comments'>#s)
Branch: 3 branches
BranchTrueFalseTotal
if ($content =~ /<div id='entrysubj'>(.*?)<div id='Comments'>/s) { } 0 0 0
elsif ($content =~ /<div id='entrysubj'>(.*?)<div role="navigation">/s) { } 0 0 0
elsif ($content =~ /<div class="entry"[^>]*>(.*?)<div class="tag">/s) { } 0 0 0
153 {
154 0 $story = $1;
155 }
156 elsif ($content =~ m#<div id='entrysubj'>(.*?)<div role="navigation">#s)
157 {
158 0 $story = $1;
159 }
160 elsif ($content =~ m#<div class="entry"[^>]*>(.*?)<div class="tag">#s)
161 {
162 0 $story = $1;
163 }
164 0 ▶ warn "user=$user, title=$title\n" if ($self->{verbose} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
165 0 ▶ warn "url=$url\n" if ($self->{verbose} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
166 0 ▶ if ($story)
Branch: 1 branch
BranchTrueFalseTotal
if ($story) { } 0 0 0
167 {
168 0 $story = $self->tidy_chars($story);
169 # remove cutid1
170 0 $story =~ s#<a name="cutid."></a>##sg;
171 }
172 else
173 {
174 0 print STDERR "story not found\n";
175 0 return $self->tidy_chars($content);
176 }
177  
178 0 my $out = <<EOT;
179 <h1>$title</h1>
180 <p>by $user</p>
181 <p>$year-$month-$day (from <a href='$url'>here</a>)</p>
182 <p>$story
183 EOT
184 0 return ($out, $title);
185 } # extract_story
186  
187 =head2 get_toc
188  
189 Get a table-of-contents page.
190  
191 =cut
192 sub get_toc {
193 0 ▶ my $self = shift;
Subroutine get_toc: not called CC=1 SCAR=6.9
Pod documented
194 0 my $url = shift;
195  
196 0 return $self->get_page("${url}?format=light");
197 } # get_toc
198  
199 =head2 parse_toc
200  
201 Parse the table-of-contents file.
202  
203 %info = $self->parse_toc(content=>$content,
204 url=>$url,
205 urls=>\@urls);
206  
207 This should return a hash containing:
208  
209 =over
210  
211 =item chapters
212  
213 An array of URLs for the chapters of the story. In the case where the
214 story only takes one page, that will be the chapter.
215 In the case where multiple URLs have been passed in, it will be those URLs.
216  
217 =item title
218  
219 The title of the story.
220  
221 =back
222  
223 It may also return additional information, such as Summary.
224  
225 =cut
226  
227 sub parse_toc {
228 0 ▶ my $self = shift;
Subroutine parse_toc: not called CC=5 SCAR=34.0
Pod documented
229 0 my %args = (
230 url=>'',
231 content=>'',
232 @_
233 );
234  
235 0 my $content = $args{content};
236 0 my $user = '';
237 0 my $is_community = 0;
238 0 ▶ if ($args{url} =~ m{http://([-\w]+)\.dreamwidth\.org})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"url"} =~ m[http://([-\w]+)\.dreamwidth\.org]) 0 0 0
239 {
240 0 $user = $1;
241 }
242 0 ▶ if ($user eq 'community')
Branch: 1 branch
BranchTrueFalseTotal
if ($user eq "community") 0 0 0
243 {
244 0 $is_community = 1;
245 0 $user = '';
246 0 ▶ if ($args{url} =~ m{http://community\.dreamwidth\.org/([-\w]+)})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"url"} =~ m[http://community\.dreamwidth\.org/([-\w]+)]) 0 0 0
247 {
248 0 $user = $1;
249 }
250 }
251  
252 0 my %info = ();
253 0 $info{url} = $args{url};
254  
255 0 $info{toc_first} = 1;
256  
257 0 my $title = $self->parse_title(%args);
258 0 $title =~ s/${user}:\s*//;
259 0 $info{title} = $title;
260  
261 0 my $summary = $self->parse_summary(%args);
262 0 $summary =~ s/"/'/g;
263 0 $info{summary} = $summary;
264  
265 0 $info{universe} = $self->parse_universe(%args);
266  
267 0 my $author = $self->parse_author(%args);
268 0 ▶ if (!$author)
Branch: 1 branch
BranchTrueFalseTotal
unless ($author) 0 0 0
269 {
270 0 $author = $user;
271 }
272 0 $info{author} = $author;
273  
274 0 my $characters = $self->parse_characters(%args);
275 0 $info{characters} = $characters;
276  
277 0 $info{chapters} = $self->parse_chapter_urls(%args,
278 user=>$user,
279 is_community=>$is_community);
280  
281 0 return %info;
282 } # parse_toc
283  
284 =head2 parse_chapter_urls
285  
286 Figure out the URLs for the chapters of this story.
287  
288 =cut
289 sub parse_chapter_urls {
290 0 ▶ my $self = shift;
Subroutine parse_chapter_urls: not called CC=13 SCAR=52.0
Pod documented
291 0 my %args = (
292 url=>'',
293 content=>'',
294 @_
295 );
296 0 my $content = $args{content};
297 0 my $user = $args{user};
298 0 my @chapters = ();
299 0 ▶ if (defined $args{urls})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{"urls"}) 0 0 0
300 {
301 0 @chapters = @{$args{urls}};
302 0 ▶ for (my $i = 0; $i < @chapters; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @chapters) 0 0 0
303 {
304 0 $chapters[$i] = sprintf('%s?format=light', $chapters[$i]);
305 }
306 }
307 0 ▶ if (@chapters == 1 and $user)
Branch: 1 branch
BranchTrueFalseTotal
if (@chapters == 1 and $user) 0 0 0
Condition0%0/3
@chapters == 1 and $user
!ll&&!rl&&r
000
MC/DC0%0/2
@chapters == 1 and $user
@chapters == 1 $user
Truth table0%0/3
A and B
A= @chapters == 1B= $user
ABresult
0X0
100
111
308 {
309 0 ▶ warn "user=$user\n" if ($self->{verbose} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
310 0 ▶ if ($args{is_community})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'is_community'}) { } 0 0 0
311 {
312 0 ▶ while ($content =~
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[href="(http://community\.dreamwidth\.org/$user/\d+.html)(#cutid\d)?">]gs) 0 0 0
313 m/href="(http:\/\/community\.dreamwidth\.org\/${user}\/\d+.html)(#cutid\d)?">/sg)
314 {
315 0 my $ch_url = $1;
316 0 ▶ if ($ch_url ne $args{url})
Branch: 1 branch
BranchTrueFalseTotal
if ($ch_url ne $args{"url"}) 0 0 0
317 {
318 0 ▶ warn "chapter=$ch_url\n" if ($self->{verbose} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
319 0 push @chapters, "${ch_url}?format=light";
320 }
321 }
322 }
323 else
324 {
325 0 ▶ while ($content =~
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ m[href="(http://$user\.dreamwidth\.org/\d+.html)(#cutid\d)?">]gs) 0 0 0
326 m/href="(http:\/\/${user}\.dreamwidth\.org\/\d+.html)(#cutid\d)?">/sg)
327 {
328 0 my $ch_url = $1;
329 0 ▶ if ($ch_url ne $args{url})
Branch: 1 branch
BranchTrueFalseTotal
if ($ch_url ne $args{"url"}) 0 0 0
330 {
331 0 ▶ warn "chapter=$ch_url\n" if ($self->{verbose} > 1);
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} > 1 0 0 0
332 0 push @chapters, "${ch_url}?format=light";
333 }
334 }
335 }
336 }
337  
338 0 return \@chapters;
339 } # parse_chapter_urls
340  
341 =head2 parse_author
342  
343 Get the author from the content
344  
345 =cut
346 sub parse_author {
347 0 ▶ my $self = shift;
Subroutine parse_author: not called CC=3 SCAR=24.8
Pod documented
348 0 my %args = (
349 url=>'',
350 content=>'',
351 @_
352 );
353  
354 0 my $content = $args{content};
355 0 my $author = $self->SUPER::parse_author(%args);
356  
357 0 ▶ if ($author =~ m#<span lj:user='\w+' style='white-space: nowrap;' class='ljuser'><a href='http://www\.dreamwidth\.org/profile\?user=\w+'><img src='http://www\.dreamwidth\.org/img/silk/identity/user\.png' alt='\[profile\] ' width='17' height='17' style='vertical-align: text-bottom; border: 0; padding-right: 1px;' /></a><a href='http://www\.dreamwidth\.org/profile\?user=\w+'><b>(.*?)</b></a></span>#)
Branch: 2 branches
BranchTrueFalseTotal
if ($author =~ m(<span lj:user='\w+' style='white-space: nowrap;' class='ljuser'><a href='http://www\.dreamwidth\.org/profile\?user=\w+'><img src='http://www\.dreamwidth\.org/img/silk/identity/user\.png' alt='\[profile\] ' width='17' height='17' style='vertical-align: text-bottom; border: 0; padding-right: 1px;' /></a><a href='http://www\.dreamwidth\.org/profile\?user=\w+'><b>(.*?)</b></a></span>)) { } 0 0 0
elsif ($author =~ m[<a href='http://[-\w]+\.dreamwidth\.org/'><b>(.*?)</b></a>]) { } 0 0 0
358 {
359 0 $author = $1;
360 }
361 elsif ($author =~ m#<a href='http://[-\w]+\.dreamwidth\.org/'><b>(.*?)</b></a>#)
362 {
363 0 $author = $1;
364 }
365 0 return $author;
366 } # parse_author
367  
368 1 1; # End of WWW::FetchStory::Fetcher::Dreamwidth
369 __END__
« blib/lib/WWW/FetchStory/Fetcher/Teaspoon.pm ↑ Summary blib/lib/WWW/FetchStory/Fetcher/FanfictionNet.pm »