| 1 | # | ||||||||||||||||||||||||||||||||
| 2 | # Ignorant Yahoo 360 blog scraper (blog.360.yahoo.com) | ||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||
| 4 | # $Id: Yahoo360.pm 168 2009-05-31 11:51:37Z cosimo $ | ||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||
| 6 | package WWW::Scraper::Yahoo360; | ||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use Date::Parse (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use File::Slurp (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | use HTTP::Date (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 15 | 1 | ▶ | use JSON::XS (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 16 | 1 | ▶ | use WWW::Mechanize (); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | use constant BLOG_URL => q{http://blog.360.yahoo.com/blog/}; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 19 | 1 | ▶ | use constant LOGIN_FORM => q{login_form}; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | use constant LOGIN_URL => q{https://login.yahoo.com/config/login_verify2?.intl=us&.done=http%3A%2F%2Fblog.360.yahoo.com%2Fblog%2F%3F.login%3D1&.src=360}; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||
| 22 | 1 | our $DEBUG = 0; | |||||||||||||||||||||||||||||||
| 23 | 1 | our $VERSION = '0.09'; | |||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 1 | ▶ | sub new { | ||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 26 | 1 | my ($class, $args) = @_; | |||||||||||||||||||||||||||||||
| 27 | 1 | ▶ | $class = ref $class || $class || __PACKAGE__; | ||||||||||||||||||||||||||||||
|
Condition40%2/5
ref $class || $class || __PACKAGE__
ref $class or $class
MC/DC0%0/2
ref $class || $class || __PACKAGE__
ref $class
$class
Truth table33%1/3
A or B
A= ref $classB= $class
| |||||||||||||||||||||||||||||||||
| 28 | 1 | my $self = $args; | |||||||||||||||||||||||||||||||
| 29 | 1 | bless $self, $class; | |||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | # Fetches high-level blog information | ||||||||||||||||||||||||||||||||
| 33 | 8 | ▶ | sub blog_info { | ||||||||||||||||||||||||||||||
|
Subroutine
blog_info: called CC=9 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 34 | 8 | my ($self, $blog_page) = @_; | |||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||
| 36 | 8 | ▶ | if (! $blog_page) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 37 | 0 | $self->debug('Fetching blog main page'); | |||||||||||||||||||||||||||||||
| 38 | 0 | $blog_page = $self->blog_main_page(); | |||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if (! $blog_page) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 40 | 0 | $self->debug('Failed to fetch blog main page'); | |||||||||||||||||||||||||||||||
| 41 | 0 | return; | |||||||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | # Get sharing level | ||||||||||||||||||||||||||||||||
| 46 | # <p class="footnote">Your blog can be seen by <strong>Public</strong> | ||||||||||||||||||||||||||||||||
| 47 | # | ||||||||||||||||||||||||||||||||
| 48 | # or: | ||||||||||||||||||||||||||||||||
| 49 | # <p class="footnote">Your blog can be seen by <strong>Just me (private)</strong> | ||||||||||||||||||||||||||||||||
| 50 | # <p class="footnote">Your blog can be seen by <strong>Friends</strong> | ||||||||||||||||||||||||||||||||
| 51 | # | ||||||||||||||||||||||||||||||||
| 52 | 8 | my $sharing = q{}; | |||||||||||||||||||||||||||||||
| 53 | 8 | ▶ | if ($blog_page =~ m{Your blog can be seen by <strong>([\w\(\)\s]+)</strong>}m) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | 8 | $sharing = lc $1; | |||||||||||||||||||||||||||||||
| 56 | 8 | ▶ | if ($sharing =~ m{just me}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 57 | 2 | $sharing = 'private'; | |||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||
| 59 | elsif ($sharing =~ m{friend}) { | ||||||||||||||||||||||||||||||||
| 60 | 0 | $sharing = 'friends'; | |||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | 8 | $self->debug('Blog sharing found to be "', $sharing, '"'); | |||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||
| 65 | else { | ||||||||||||||||||||||||||||||||
| 66 | 0 | $self->debug('Blog sharing string not found'); | |||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | # Get title | ||||||||||||||||||||||||||||||||
| 70 | 8 | my $title = q{}; | |||||||||||||||||||||||||||||||
| 71 | 8 | ▶ | if ($blog_page =~ m{<h3>([^<]+)<span class="view-toggle">Full Post View}m) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 72 | 8 | $title = $1; | |||||||||||||||||||||||||||||||
| 73 | 8 | $self->debug('Blog title found to be "', $title, '"'); | |||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | # Get number of posts | ||||||||||||||||||||||||||||||||
| 77 | # | ||||||||||||||||||||||||||||||||
| 78 | # <span><em>1 - 5</em> of <em class="limit">13</em> ... | ||||||||||||||||||||||||||||||||
| 79 | 8 | my $start = | |||||||||||||||||||||||||||||||
| 80 | my $end = | ||||||||||||||||||||||||||||||||
| 81 | my $count = 0; | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | 8 | ▶ | if ($blog_page =~ m{<span><em>(\d+) \- (\d+)</em> of <em class="limit">(\d+)</em>}m) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 84 | 8 | $start = $1; | |||||||||||||||||||||||||||||||
| 85 | 8 | $end = $2; | |||||||||||||||||||||||||||||||
| 86 | 8 | $count = $3; | |||||||||||||||||||||||||||||||
| 87 | 8 | $self->debug('Blog post counts found. Start:', $start, ' End:', $end, ' Count:', $count); | |||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||||||
| 90 | 0 | $self->debug('Blog post counts not found'); | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | 8 | my $link = q{}; | |||||||||||||||||||||||||||||||
| 94 | 8 | ▶ | if ($blog_page =~ m{<a href="([^"]+)" class="selected">My Blog</a>}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 8 | $link = $1; | |||||||||||||||||||||||||||||||
| 96 | 8 | $self->debug('Blog URL found: ', $link); | |||||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||||||
| 98 | else { | ||||||||||||||||||||||||||||||||
| 99 | 0 | $self->debug('Blog URL not found'); | |||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 8 | $title =~ s{^\s+}{}; | |||||||||||||||||||||||||||||||
| 103 | 8 | $title =~ s{\s+$}{}; | |||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | return { | ||||||||||||||||||||||||||||||||
| 106 | 8 | sharing => $sharing, | |||||||||||||||||||||||||||||||
| 107 | title => $title, | ||||||||||||||||||||||||||||||||
| 108 | start => $start, | ||||||||||||||||||||||||||||||||
| 109 | end => $end, | ||||||||||||||||||||||||||||||||
| 110 | count => $count, | ||||||||||||||||||||||||||||||||
| 111 | link => $link, | ||||||||||||||||||||||||||||||||
| 112 | lastBuildDate => HTTP::Date::time2str(), | ||||||||||||||||||||||||||||||||
| 113 | language => 'en-us', | ||||||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||
| 118 | # Fetches the user's main blog page | ||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub blog_main_page { | ||||||||||||||||||||||||||||||
|
Subroutine
blog_main_page: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 120 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | 0 | my $mech = $self->mech(); | |||||||||||||||||||||||||||||||
| 123 | 0 | $mech->get(BLOG_URL); | |||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if ($mech->success()) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 126 | 0 | $self->debug('Blog main page downloaded successfully'); | |||||||||||||||||||||||||||||||
| 127 | 0 | return $mech->content(); | |||||||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 0 | $self->debug('Blog main page download failed'); | |||||||||||||||||||||||||||||||
| 131 | 0 | Carp::croak("Failed to retrieve blog main page"); | |||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||
| 134 | # Builds the url to fetch a specific blog page | ||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub blog_page_url { | ||||||||||||||||||||||||||||||
|
Subroutine
blog_page_url: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 136 | 0 | my ($self, $link, $start, $per_page, $count) = @_; | |||||||||||||||||||||||||||||||
| 137 | 0 | my $url = $link; | |||||||||||||||||||||||||||||||
| 138 | 0 | my $last = $start + $per_page - 1; | |||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | if ($last > $count) { $last = $count } | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 140 | 0 | $url .= '&l=' . $start; | |||||||||||||||||||||||||||||||
| 141 | 0 | $url .= '&u=' . $last; | |||||||||||||||||||||||||||||||
| 142 | 0 | $url .= '&mx=' . $count; | |||||||||||||||||||||||||||||||
| 143 | 0 | $url .= '&lmt=' . $per_page; | |||||||||||||||||||||||||||||||
| 144 | 0 | return $url; | |||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||
| 147 | 120 | ▶ | sub debug { | ||||||||||||||||||||||||||||||
|
Subroutine
debug: called CC=2 SCAR=11.6
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 148 | 120 | ▶ | return unless $DEBUG; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||
| 150 | 0 | my ($self, @msg) = @_; | |||||||||||||||||||||||||||||||
| 151 | 0 | print STDERR @msg, "\n"; | |||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||
| 153 | 0 | return; | |||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | # Logs in to Yahoo | ||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | sub login { | ||||||||||||||||||||||||||||||
|
Subroutine
login: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 158 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||
| 160 | 0 | my $user = $self->{username}; | |||||||||||||||||||||||||||||||
| 161 | 0 | my $pass = $self->{password}; | |||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||
| 163 | 0 | my $mech = $self->mech(); | |||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||
| 165 | 0 | $mech->get(LOGIN_URL); | |||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||
| 167 | 0 | $mech->submit_form( | |||||||||||||||||||||||||||||||
| 168 | form_name => LOGIN_FORM, | ||||||||||||||||||||||||||||||||
| 169 | fields => { | ||||||||||||||||||||||||||||||||
| 170 | login => $user, | ||||||||||||||||||||||||||||||||
| 171 | passwd => $pass, | ||||||||||||||||||||||||||||||||
| 172 | '.persistent' => 'y', | ||||||||||||||||||||||||||||||||
| 173 | }, | ||||||||||||||||||||||||||||||||
| 174 | button => '.save', | ||||||||||||||||||||||||||||||||
| 175 | ); | ||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||
| 177 | # Not sure how to make this more robust | ||||||||||||||||||||||||||||||||
| 178 | 0 | my $next_page = $mech->content(); | |||||||||||||||||||||||||||||||
| 179 | 0 | ▶ | if ($next_page =~ m{Invalid ID or password}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 180 | 0 | $self->debug('Login to Yahoo service failed for user "', $user, '"'); | |||||||||||||||||||||||||||||||
| 181 | 0 | return; | |||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||
| 184 | 0 | my $ok = $mech->success(); | |||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if ($ok) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 187 | 0 | $self->debug('Login to Yahoo service succeeded'); | |||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | else { | ||||||||||||||||||||||||||||||||
| 190 | 0 | $self->debug('Login to Yahoo service failed. Unknown reason?'); | |||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 0 | return $ok; | |||||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||
| 196 | # Dumps last accessed page content to STDOUT | ||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | sub dump { | ||||||||||||||||||||||||||||||
|
Subroutine
dump: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 198 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 199 | 0 | print $self->mech->content(); | |||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||
| 202 | # Retrieves all comments in the user's blog | ||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | sub get_blog_comments { | ||||||||||||||||||||||||||||||
|
Subroutine
get_blog_comments: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 204 | 0 | my ($self, $posts) = @_; | |||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | if (! $posts) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 207 | 0 | return; | |||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||
| 210 | 0 | my @comments; | |||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||
| 212 | 0 | for my $post (@{$posts}) { | |||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||
| 214 | # No comments, don't fetch them | ||||||||||||||||||||||||||||||||
| 215 | 0 | ▶ | if ($post->{comments} == 0) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 216 | 0 | $self->debug('No comments for post ', $post->{title}); | |||||||||||||||||||||||||||||||
| 217 | 0 | next; | |||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||
| 220 | #print qq{Found $post->{comments} comments for blog post "$post->{title}"\n}; | ||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | if (my $post_comm = $self->get_blogpost_comments($post)) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 223 | 0 | $self->debug('Got ', scalar(@{ $post_comm }), ' comments for post ', $post->{title}); | |||||||||||||||||||||||||||||||
| 224 | 0 | push @comments, @{ $post_comm }; | |||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||
| 229 | 0 | return \@comments; | |||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||||||
| 232 | # Retrieves all comments to a single blog post | ||||||||||||||||||||||||||||||||
| 233 | 2 | ▶ | sub get_blogpost_comments { | ||||||||||||||||||||||||||||||
|
Subroutine
get_blogpost_comments: called CC=7 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||||||||||||
| 234 | 2 | my ($self, $post, $page) = @_; | |||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||
| 236 | # If we didn't get a pre-saved html page, get it now | ||||||||||||||||||||||||||||||||
| 237 | 2 | ▶ | if (! $page) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 238 | 0 | $self->mech->get($post->{link}); | |||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | $page = $self->mech->success | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 240 | ? $self->mech->content() | ||||||||||||||||||||||||||||||||
| 241 | : q{}; | ||||||||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||
| 244 | 2 | ▶ | if (! $page) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 245 | 0 | warn "ERROR fetching blogpost comments for $post->{title}\n"; | |||||||||||||||||||||||||||||||
| 246 | 0 | return; | |||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||
| 249 | 2 | my @comments; | |||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||
| 251 | 2 | ▶ | while ($page =~ m{<li class="user-name"><a href="([^"]+)" title="([^"]+)">}mg) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||
| 253 | my $comment = { | ||||||||||||||||||||||||||||||||
| 254 | 'user-profile' => $1, | ||||||||||||||||||||||||||||||||
| 255 | username => $2, | ||||||||||||||||||||||||||||||||
| 256 | link => $post->{link}, | ||||||||||||||||||||||||||||||||
| 257 | 6 | }; | |||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||
| 259 | # Comments can span multiple lines | ||||||||||||||||||||||||||||||||
| 260 | # but are always enclosed between <p class="comment"> and </p> | ||||||||||||||||||||||||||||||||
| 261 | 6 | ▶ | if ($page =~ m{<p class="comment">(.*?)</p>}sg) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 262 | 6 | $comment->{comment} = $1; | |||||||||||||||||||||||||||||||
| 263 | 6 | $comment->{comment} =~ s{^\s+}{}; | |||||||||||||||||||||||||||||||
| 264 | 6 | $comment->{comment} =~ s{\s+$}{}; | |||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||
| 267 | 6 | ▶ | if ($page =~ m{<p class="datestamp">([^<]+)\s*<}mg) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 268 | 6 | $comment->{date} = $1; | |||||||||||||||||||||||||||||||
| 269 | 6 | $comment->{date} =~ s{^\s+}{}; | |||||||||||||||||||||||||||||||
| 270 | 6 | $comment->{date} =~ s{\s+$}{}; | |||||||||||||||||||||||||||||||
| 271 | 6 | $comment->{date} = $self->parse_date($comment->{date}); | |||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||
| 274 | $self->debug( | ||||||||||||||||||||||||||||||||
| 275 | 'Found comment "', $comment->{comment}, | ||||||||||||||||||||||||||||||||
| 276 | 6 | '" by "', $comment->{username}, '"' | |||||||||||||||||||||||||||||||
| 277 | ); | ||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||
| 279 | 6 | push @comments, $comment; | |||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||
| 282 | 2 | $self->debug('Found ', scalar(@comments), ' comments to blog post ', $post->{link}); | |||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||
| 284 | 2 | return \@comments; | |||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||
| 287 | # Gets all blog posts by a user | ||||||||||||||||||||||||||||||||
| 288 | 4 | ▶ | sub get_blog_posts { | ||||||||||||||||||||||||||||||
|
Subroutine
get_blog_posts: called CC=18 SCAR=32.1
Pod
documented
| |||||||||||||||||||||||||||||||||
| 289 | 4 | my ($self, $blog_page, %overrides) = @_; | |||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||
| 291 | 4 | $self->debug("Start parsing of blog posts"); | |||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||
| 293 | 4 | ▶ | if (! $blog_page) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 294 | 0 | $self->debug("Downloading of main blog page"); | |||||||||||||||||||||||||||||||
| 295 | 0 | ▶ | $blog_page ||= $self->blog_main_page(); | ||||||||||||||||||||||||||||||
|
Condition0%0/3
$blog_page ||= $self->blog_main_page
MC/DC0%0/2
$blog_page ||= $self->blog_main_page
$blog_page
$self->blog_main_page
Truth table0%0/3
A ||= B
A= $blog_pageB= $self->blog_main_page
| |||||||||||||||||||||||||||||||||
| 296 | 0 | $self->debug("Download complete"); | |||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||
| 298 | else { | ||||||||||||||||||||||||||||||||
| 299 | 4 | $self->debug("Blog main page was already supplied. No need to download."); | |||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||
| 302 | 4 | my $blog_info = $self->blog_info($blog_page); | |||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||
| 304 | 4 | for (keys %overrides) { | |||||||||||||||||||||||||||||||
| 305 | 12 | $blog_info->{$_} = $overrides{$_}; | |||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||
| 308 | 4 | my $link = $blog_info->{link}; | |||||||||||||||||||||||||||||||
| 309 | 4 | my $start = $blog_info->{start}; | |||||||||||||||||||||||||||||||
| 310 | 4 | my $count = $blog_info->{count}; | |||||||||||||||||||||||||||||||
| 311 | 4 | my $end_page = $blog_info->{end}; | |||||||||||||||||||||||||||||||
| 312 | 4 | my $end_blog = $start + $count - 1; | |||||||||||||||||||||||||||||||
| 313 | 4 | my $per_page = $end_page - $start + 1; | |||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||
| 315 | 4 | my @posts = (); | |||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||
| 317 | 4 | $self->debug("Parsing posts ($start .. $end_blog)"); | |||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||
| 319 | # Prevent endless loops | ||||||||||||||||||||||||||||||||
| 320 | 4 | ▶ | if ($start > $end_page) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 321 | 0 | $start = $end_page; | |||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||
| 324 | 4 | ▶ | for (my $n = $start; $n <= $end_blog; ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||
| 326 | 4 | $self->debug( | |||||||||||||||||||||||||||||||
| 327 | 'Reading post n. ', $n, | ||||||||||||||||||||||||||||||||
| 328 | ' end_of_page:', $end_page, | ||||||||||||||||||||||||||||||||
| 329 | ' end_of_blog:', $end_blog, | ||||||||||||||||||||||||||||||||
| 330 | ); | ||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||
| 332 | # Fetch next page and continue | ||||||||||||||||||||||||||||||||
| 333 | 4 | ▶ | if ($n >= $end_page && $end_page < $end_blog) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$n >= $end_page and $end_page < $end_blog
MC/DC0%0/2
$n >= $end_page and $end_page < $end_blog
$n >= $end_page
$end_page < $end_blog
Truth table66%2/3
A and B
A= $n >= $end_pageB= $end_page < $end_blog
| |||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||
| 335 | 0 | my $next_page_url = $self->blog_page_url( | |||||||||||||||||||||||||||||||
| 336 | $link, $end_page + 1, $per_page, $count | ||||||||||||||||||||||||||||||||
| 337 | ); | ||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||
| 339 | 0 | $end_page += $per_page; | |||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||
| 341 | 0 | $self->mech->get($next_page_url); | |||||||||||||||||||||||||||||||
| 342 | 0 | $self->debug('Next url is:', $next_page_url); | |||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||
| 344 | 0 | $blog_page = $self->mech->content(); | |||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | if (! $blog_page) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 346 | 0 | $self->debug('Failed to read url: ', $next_page_url); | |||||||||||||||||||||||||||||||
| 347 | 0 | last; | |||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||
| 352 | 4 | my $found_posts = 0; | |||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||
| 354 | 4 | ▶ | while ($blog_page =~ m{<dt class="post-head">([^<]+)</dt>}gm) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||
| 356 | # Blog post title | ||||||||||||||||||||||||||||||||
| 357 | 10 | my $title = $1; | |||||||||||||||||||||||||||||||
| 358 | 10 | my $post = { | |||||||||||||||||||||||||||||||
| 359 | title => $1, | ||||||||||||||||||||||||||||||||
| 360 | description => '' | ||||||||||||||||||||||||||||||||
| 361 | }; | ||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||
| 363 | 10 | $self->debug('Found new blog post "', $title, '" (', $n, ')'); | |||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||
| 365 | 10 | $found_posts = 1; | |||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||
| 367 | # Main picture of the blog post | ||||||||||||||||||||||||||||||||
| 368 | 10 | ▶ | if ($blog_page =~ m{<div class="image-wrapper">(.*?)</div>}gsmc) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 369 | 10 | my $pic = $1; | |||||||||||||||||||||||||||||||
| 370 | 10 | $pic =~ s{^\s*}{}mx; | |||||||||||||||||||||||||||||||
| 371 | 10 | $pic =~ s{\s*$}{}mx; | |||||||||||||||||||||||||||||||
| 372 | 10 | ▶ | if ($pic) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 373 | 4 | $post->{description} = '<div align="center">' . $pic . '</div>'; | |||||||||||||||||||||||||||||||
| 374 | 4 | $self->debug(' Image: ', substr($pic, 0, 30), '...'); | |||||||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||
| 378 | # Blog post content | ||||||||||||||||||||||||||||||||
| 379 | # Read until the end of line (there might be multiple <div>s) | ||||||||||||||||||||||||||||||||
| 380 | 10 | ▶ | if ($blog_page =~ m{<div class="content-wrapper">(.*)</div>}gmc) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 381 | 10 | $post->{description} .= $1; | |||||||||||||||||||||||||||||||
| 382 | 10 | $self->debug(' Content: ', substr($1, 0, 30), '...'); | |||||||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||
| 385 | # Tags | ||||||||||||||||||||||||||||||||
| 386 | 10 | ▶ | if ($blog_page =~ m{<form><input type="hidden" name="tagslist" value="([^"]*)"}gm) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 387 | 10 | $post->{tags} = $1; | |||||||||||||||||||||||||||||||
| 388 | 10 | $self->debug(' Tags: ', $1); | |||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||
| 391 | # Date of post | ||||||||||||||||||||||||||||||||
| 392 | 10 | ▶ | if ($blog_page =~ m{<span>([^<]+)<a href="[^"]+">Edit</a>}gm) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 393 | 10 | $post->{pubDate} = HTTP::Date::time2str($self->parse_date($1)); | |||||||||||||||||||||||||||||||
| 394 | 10 | $self->debug(' Date: ', $1); | |||||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||||||
| 397 | # Permanent link | ||||||||||||||||||||||||||||||||
| 398 | 10 | ▶ | if ($blog_page =~ m{<a href="([^"]+)">Permanent Link</a>}gm) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 399 | 10 | $post->{link} = $1; | |||||||||||||||||||||||||||||||
| 400 | 10 | $self->debug(' Permalink: ', $1); | |||||||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||
| 403 | # No. of comments | ||||||||||||||||||||||||||||||||
| 404 | 10 | ▶ | if ($blog_page =~ m{<a href="[^"]+">(\d+) Comments?</a>}gm) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 405 | 10 | $post->{comments} = $1; | |||||||||||||||||||||||||||||||
| 406 | 10 | $self->debug(' Comments: ', $1); | |||||||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||
| 409 | 10 | push @posts, $post; | |||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||
| 411 | 10 | $n++; | |||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||
| 415 | 4 | ▶ | if (not $found_posts) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 416 | 1 | last; | |||||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||
| 421 | 4 | return \@posts; | |||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||
| 423 | } | ||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||
| 425 | # Mechanize object accessor | ||||||||||||||||||||||||||||||||
| 426 | 0 | ▶ | sub mech { | ||||||||||||||||||||||||||||||
|
Subroutine
mech: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||
| 427 | 0 | my ($self) = @_; | |||||||||||||||||||||||||||||||
| 428 | 0 | ▶ | if (! exists $self->{_mech}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 429 | 0 | $self->{_mech} = WWW::Mechanize->new(); | |||||||||||||||||||||||||||||||
| 430 | } | ||||||||||||||||||||||||||||||||
| 431 | 0 | return $self->{_mech}; | |||||||||||||||||||||||||||||||
| 432 | } | ||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||
| 434 | # Tries to parse a date in the Yahoo 360 format | ||||||||||||||||||||||||||||||||
| 435 | 20 | ▶ | sub parse_date { | ||||||||||||||||||||||||||||||
|
Subroutine
parse_date: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||
| 436 | 20 | my ($self, $date) = @_; | |||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||
| 438 | 20 | $date =~ s{^\s+}{}; | |||||||||||||||||||||||||||||||
| 439 | 20 | $date =~ s{\s+$}{}; | |||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||
| 441 | 20 | ▶ | if ($date =~ m{^ (\w{3})\w+ \s (\w{3})\w* \s (\d+), \s (\d+) \s - \s (\d+):(\d+)([ap]m) \s \((.*)\) \s* $}x) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 442 | 20 | my $dow = $1; | |||||||||||||||||||||||||||||||
| 443 | 20 | my $month = $2; | |||||||||||||||||||||||||||||||
| 444 | 20 | my $day = $3; | |||||||||||||||||||||||||||||||
| 445 | 20 | my $year = $4; | |||||||||||||||||||||||||||||||
| 446 | 20 | my $hours = $5; | |||||||||||||||||||||||||||||||
| 447 | 20 | my $mins = $6; | |||||||||||||||||||||||||||||||
| 448 | 20 | my $ampm = uc $7; | |||||||||||||||||||||||||||||||
| 449 | 20 | my $tz = uc $8; | |||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||
| 451 | # Indochina time zone is not recognized by Date::Parse | ||||||||||||||||||||||||||||||||
| 452 | 20 | ▶ | if ($tz eq 'ICT') { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 453 | 12 | $tz = 'UTC+07'; | |||||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||
| 456 | 20 | ▶ | if ($ampm eq 'AM' && $hours == 12) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition100%3/3
$ampm eq "AM" and $hours == 12
MC/DC100%2/2
$ampm eq "AM" and $hours == 12
$ampm eq "AM"
$hours == 12
Truth table100%3/3
A and B
A= $ampm eq "AM"B= $hours == 12
| |||||||||||||||||||||||||||||||||
| 457 | 1 | $hours = 0; | |||||||||||||||||||||||||||||||
| 458 | } | ||||||||||||||||||||||||||||||||
| 459 | ▶ | elsif ($ampm eq 'PM' && $hours != 12) { | |||||||||||||||||||||||||||||||
|
Condition100%3/3
$ampm eq "PM" and $hours != 12
MC/DC100%2/2
$ampm eq "PM" and $hours != 12
$ampm eq "PM"
$hours != 12
Truth table100%3/3
A and B
A= $ampm eq "PM"B= $hours != 12
| |||||||||||||||||||||||||||||||||
| 460 | 8 | $hours += 12; | |||||||||||||||||||||||||||||||
| 461 | 8 | ▶ | if ($hours > 23) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 462 | 0 | $hours -= 24; | |||||||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||||||
| 464 | } | ||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||
| 466 | 20 | my $time = "$hours:$mins:00"; | |||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||
| 468 | # Wed, 16 Jun 94 07:29:35 CST | ||||||||||||||||||||||||||||||||
| 469 | 20 | $date = "$day $month $year $time $tz"; | |||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||
| 471 | #arn "# Converted to [$date]\n"; | ||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||
| 473 | } | ||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||
| 475 | 20 | my $epoch = Date::Parse::str2time($date); | |||||||||||||||||||||||||||||||
| 476 | #arn "# str2time($date) returns ($epoch)\n"; | ||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||
| 478 | 20 | return $epoch; | |||||||||||||||||||||||||||||||
| 479 | } | ||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||
| 481 | 1 | 1; | |||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||
| 483 | __END__ | ||||||||||||||||||||||||||||||||