| 1 | package WWW::UsePerl::Journal::Comment; | ||||||||||
| 2 | |||||||||||
| 3 | 6 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 6 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 6 | ▶ | use vars qw($VERSION $AUTOLOAD); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 6 | $VERSION = '0.15'; | |||||||||
| 8 | |||||||||||
| 9 | #---------------------------------------------------------------------------- | ||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | WWW::UsePerl::Journal::Comment - Handles the retrieval of UsePerl journal entry comments. | ||||||||||
| 14 | |||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||
| 16 | |||||||||||
| 17 | my $comment = WWW::UsePerl::Journal::Comment->new( | ||||||||||
| 18 | # required | ||||||||||
| 19 | j => $journal, | ||||||||||
| 20 | cid => $commentid, | ||||||||||
| 21 | eid => $entryid, | ||||||||||
| 22 | extract => $extract | ||||||||||
| 23 | ); | ||||||||||
| 24 | |||||||||||
| 25 | $comment->subject(); | ||||||||||
| 26 | |||||||||||
| 27 | # called from WWW::UsePerl::Journal::Thread object | ||||||||||
| 28 | $thread->comment( $cid )->content(); | ||||||||||
| 29 | |||||||||||
| 30 | =head1 DESCRIPTION | ||||||||||
| 31 | |||||||||||
| 32 | A collection of routines to handle the retrieval of comments from a | ||||||||||
| 33 | UsePerl (L<http://use.perl.org/>) journal entry. | ||||||||||
| 34 | |||||||||||
| 35 | =cut | ||||||||||
| 36 | |||||||||||
| 37 | # ------------------------------------- | ||||||||||
| 38 | # Library Modules | ||||||||||
| 39 | |||||||||||
| 40 | 6 | ▶ | use base qw(Class::Accessor::Fast); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 41 | |||||||||||
| 42 | 6 | ▶ | use HTTP::Request::Common; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | 6 | ▶ | use LWP::UserAgent; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | 6 | ▶ | use Time::Piece; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | 6 | ▶ | use WWW::UsePerl::Journal; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | # ------------------------------------- | ||||||||||
| 48 | # Constants & Variables | ||||||||||
| 49 | |||||||||||
| 50 | 6 | ▶ | use constant USEPERL => 'http://use.perl.org/use.perl.org'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 51 | 6 | ▶ | use overload q{""} => sub { $_[0]->stringify() }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.2
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||
| 52 | |||||||||||
| 53 | 6 | my %months = ( | |||||||||
| 54 | 'January' => 1, | ||||||||||
| 55 | 'February' => 2, | ||||||||||
| 56 | 'March' => 3, | ||||||||||
| 57 | 'April' => 4, | ||||||||||
| 58 | 'May' => 5, | ||||||||||
| 59 | 'June' => 6, | ||||||||||
| 60 | 'July' => 7, | ||||||||||
| 61 | 'August' => 8, | ||||||||||
| 62 | 'September' => 9, | ||||||||||
| 63 | 'October' => 10, | ||||||||||
| 64 | 'November' => 11, | ||||||||||
| 65 | 'December' => 12, | ||||||||||
| 66 | ); | ||||||||||
| 67 | |||||||||||
| 68 | # ------------------------------------- | ||||||||||
| 69 | # Public Interface | ||||||||||
| 70 | |||||||||||
| 71 | =head1 PUBLIC INTERFACE | ||||||||||
| 72 | |||||||||||
| 73 | =head2 The Constructor | ||||||||||
| 74 | |||||||||||
| 75 | =over 4 | ||||||||||
| 76 | |||||||||||
| 77 | =item new | ||||||||||
| 78 | |||||||||||
| 79 | Each comment is retrieved as an object. Note that the parent object | ||||||||||
| 80 | (from WWW::UsePerl::Journal), thread id and comment id are mandatory | ||||||||||
| 81 | requirements to create the object. | ||||||||||
| 82 | |||||||||||
| 83 | =back | ||||||||||
| 84 | |||||||||||
| 85 | =cut | ||||||||||
| 86 | |||||||||||
| 87 | sub new { | ||||||||||
| 88 | 2 | ▶ | my ($class,%opts) = @_; | ||||||||
|
Subroutine
new: called CC=4 SCAR=17.2
Pod
documented
| |||||||||||
| 89 | |||||||||||
| 90 | 2 | for(qw/j eid cid extract/) { | |||||||||
| 91 | 5 | ▶ | return unless(exists $opts{$_}); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | die "No parent object" | ||||||||||
| 95 | 1 | ▶ | unless $opts{j}->isa('WWW::UsePerl::Journal'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | |||||||||||
| 97 | 0 | my %atts = map {$_ => $opts{$_}} qw(j eid cid extract); | |||||||||
| 98 | 0 | my $self = bless \%atts, $class; | |||||||||
| 99 | |||||||||||
| 100 | 0 | $self->_get_content(); | |||||||||
| 101 | |||||||||||
| 102 | 0 | return $self; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | #---------------------------------------------------------------------------- | ||||||||||
| 106 | # Accessors | ||||||||||
| 107 | |||||||||||
| 108 | =head2 The Accessors | ||||||||||
| 109 | |||||||||||
| 110 | The following accessor methods are available: | ||||||||||
| 111 | |||||||||||
| 112 | id | ||||||||||
| 113 | date | ||||||||||
| 114 | subject | ||||||||||
| 115 | user | ||||||||||
| 116 | uid | ||||||||||
| 117 | score | ||||||||||
| 118 | content | ||||||||||
| 119 | |||||||||||
| 120 | All functions can be called to return the current value of the associated | ||||||||||
| 121 | object variable. | ||||||||||
| 122 | |||||||||||
| 123 | =cut | ||||||||||
| 124 | |||||||||||
| 125 | 6 | __PACKAGE__->mk_accessors($_) for qw(cid date subject user uid score content); | |||||||||
| 126 | |||||||||||
| 127 | =head2 Methods | ||||||||||
| 128 | |||||||||||
| 129 | =over 4 | ||||||||||
| 130 | |||||||||||
| 131 | =item stringify - For String Context | ||||||||||
| 132 | |||||||||||
| 133 | When an object is called directly, stringification occurs. Safer to | ||||||||||
| 134 | use -E<gt>content instead. | ||||||||||
| 135 | |||||||||||
| 136 | =back | ||||||||||
| 137 | |||||||||||
| 138 | =cut | ||||||||||
| 139 | |||||||||||
| 140 | sub stringify { | ||||||||||
| 141 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
stringify: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 142 | 0 | return $self->content(); | |||||||||
| 143 | } | ||||||||||
| 144 | |||||||||||
| 145 | # ------------------------------------- | ||||||||||
| 146 | # The Private Subs | ||||||||||
| 147 | |||||||||||
| 148 | # name: _get_content | ||||||||||
| 149 | # args: self .... object itself | ||||||||||
| 150 | # retv: content text | ||||||||||
| 151 | # desc: Retrieves the content and additional information for a given | ||||||||||
| 152 | # comment. Splits the fields into object variables and returns | ||||||||||
| 153 | # the content text | ||||||||||
| 154 | |||||||||||
| 155 | sub _get_content { | ||||||||||
| 156 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_get_content: not called CC=14 SCAR=53.5
| |||||||||||
| 157 | |||||||||||
| 158 | 0 | my $content = $self->{extract}; | |||||||||
| 159 | |||||||||||
| 160 | 0 | ▶ | if($self->{j}->debug) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 0 | $self->{j}->log('mess' => "\n#_get_content: content=[$content]\n"); | |||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | 0 | ▶ | return $self->{j}->error("Error getting entry") unless $content; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | |||||||||||
| 166 | # remember there are different presentations for dates!!!! | ||||||||||
| 167 | |||||||||||
| 168 | 0 | my ($string,$format); | |||||||||
| 169 | 0 | $content =~ s/\n//g; | |||||||||
| 170 | 0 | my @fields = ( $content =~ m! | |||||||||
| 171 | <li\s+id="tree_(\d+)"\s+class="comment[^"]*"> # comment id | ||||||||||
| 172 | .*? <h4><a[^>]+>([^<]+)</a> # subject | ||||||||||
| 173 | .*? <span\s+id="comment_score_\1"\s+class="score"> | ||||||||||
| 174 | .*? Score:(\d+).*?</h4> # score | ||||||||||
| 175 | .*? <a\s+href="[./\w]+/index.html">\s*(\w+) # username | ||||||||||
| 176 | .*? \((\d+)\)</a> # userid | ||||||||||
| 177 | (?:\s+<span\s+class="otherdetails" | ||||||||||
| 178 | .*? on\s+(\d+\.\d+.\d+\s\d+:\d+) # date/time - "2003.05.20 17:31" or "Friday August 08 2003, @01:51PM" | ||||||||||
| 179 | .*? </span>)? | ||||||||||
| 180 | .*? <div\s+id="comment_body_\1">(.*?)</div> # text | ||||||||||
| 181 | !mixs ); | ||||||||||
| 182 | |||||||||||
| 183 | 0 | ($self->{pid}) = $content =~ m/id="commtree_(\d+)"/; | |||||||||
| 184 | |||||||||||
| 185 | 0 | ▶ | if($self->{j}->debug) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 186 | 0 | ▶ | $self->{j}->log('mess' => "\n#_get_content: fields=[".(join("][",map {$_||''} @fields))."]\n"); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 187 | } | ||||||||||
| 188 | |||||||||||
| 189 | 0 | ▶ | return unless(@fields); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | |||||||||||
| 191 | 0 | ▶ | if($fields[5]) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 192 | 0 | my ($year, $month, $day, $hr, $mi) = $fields[5] =~ m! (\d+)\.(\d+)\.(\d+) .*? (\d+):(\d+) !smx; | |||||||||
| 193 | 0 | ▶ | unless($day) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 194 | 0 | my $amp; | |||||||||
| 195 | 0 | ($month, $day, $year, $hr, $mi, $amp) = $fields[5] =~ m! \w+\s+ (\w+) \s+(\d+)\s*(\d*), \s+ @(\d+):(\d+)([AP]M) !smx; | |||||||||
| 196 | 0 | $month = $months{$month}; | |||||||||
| 197 | 0 | ▶ | $year = (localtime)[5] unless($year); # current year formatting drops the year. | ||||||||
|
Branch: 1 branch
| |||||||||||
| 198 | 0 | ▶ | $hr += 12 if ($amp eq 'PM'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 199 | 0 | ▶ | $hr = 0 if $hr == 24; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 200 | } | ||||||||||
| 201 | |||||||||||
| 202 | 0 | ▶ | if($self->{j}->debug) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | 0 | $self->{j}->log('mess' => "\n#_get_content: date=[$year $month $day ${hr}:$mi]\n"); | |||||||||
| 204 | } | ||||||||||
| 205 | |||||||||||
| 206 | # sometimes Time::Piece can't parse the date :( | ||||||||||
| 207 | 0 | eval { | |||||||||
| 208 | 0 | $self->{date} = Time::Piece->strptime( | |||||||||
| 209 | "$month $day $year ${hr}:$mi", | ||||||||||
| 210 | '%m %d %Y %H:%M' | ||||||||||
| 211 | ); | ||||||||||
| 212 | }; | ||||||||||
| 213 | |||||||||||
| 214 | 0 | ▶ | if($self->{j}->debug) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 215 | 0 | $self->{j}->log('mess' => "\n#_get_content: date=[".$self->{date}."]\n"); | |||||||||
| 216 | } | ||||||||||
| 217 | } | ||||||||||
| 218 | |||||||||||
| 219 | 0 | $self->{subject} = $fields[1]; | |||||||||
| 220 | 0 | $self->{score} = $fields[2]; | |||||||||
| 221 | 0 | $self->{user} = $fields[3]; | |||||||||
| 222 | 0 | $self->{uid} = $fields[4]; | |||||||||
| 223 | 0 | $self->{content} = $fields[6]; | |||||||||
| 224 | |||||||||||
| 225 | 0 | ▶ | return unless($self->{content}); # What no content! | ||||||||
|
Branch: 1 branch
| |||||||||||
| 226 | |||||||||||
| 227 | 0 | $self->{content} =~ s!(\s+<(?:p|br /)>)*$!!gi; # remove trailing whitespace formatting | |||||||||
| 228 | 0 | $self->{content} =~ s!\s+(<(p|br /)>)!$1!gi; # remove whitespace before whitespace formatting | |||||||||
| 229 | 0 | $self->{content} =~ s!(<(p|br /)>){2,}!<p>!gi; # remove repeated whitespace formatting | |||||||||
| 230 | |||||||||||
| 231 | 0 | return; | |||||||||
| 232 | } | ||||||||||
| 233 | |||||||||||
| 234 | ▶ | sub DESTROY {} | |||||||||
|
Subroutine
DESTROY: not called CC=1 SCAR=0.0
| |||||||||||
| 235 | |||||||||||
| 236 | 6 | 1; | |||||||||
| 237 | |||||||||||
| 238 | __END__ | ||||||||||