| 1 | package URL::Social::Reddit; | ||||||||||
| 2 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 6 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 6 | extends 'URL::Social::BASE'; | |||||||||
| 6 | |||||||||||
| 7 | 6 | ▶ | use URL::Social::Reddit::Post; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =head1 NAME | ||||||||||
| 10 | |||||||||||
| 11 | URL::Social::Reddit - Interface to the Reddit API. | ||||||||||
| 12 | |||||||||||
| 13 | =head1 DESCRIPTION | ||||||||||
| 14 | |||||||||||
| 15 | Do not use this module directly. Access it from L<URL::Social> instead. | ||||||||||
| 16 | Note, however, that this module behaves a bit different than the others. | ||||||||||
| 17 | |||||||||||
| 18 | A post on reddit can be a duplicate and/or it can end up in different | ||||||||||
| 19 | subreddits. | ||||||||||
| 20 | |||||||||||
| 21 | use URL::Social; | ||||||||||
| 22 | |||||||||||
| 23 | my $social = URL::Social->new( | ||||||||||
| 24 | url => '...', | ||||||||||
| 25 | ); | ||||||||||
| 26 | |||||||||||
| 27 | print $social->reddit->upvote_count . "\n"; # Sum of all posts' upvotes | ||||||||||
| 28 | |||||||||||
| 29 | # ...or... | ||||||||||
| 30 | |||||||||||
| 31 | foreach my $post ( @{$social->reddit->posts} ) { | ||||||||||
| 32 | print $post->upvote_count . "\n"; | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | See L<URL::Social::Reddit::Post> for an overview of the data you can access | ||||||||||
| 36 | for each post. | ||||||||||
| 37 | |||||||||||
| 38 | =head1 METHODS | ||||||||||
| 39 | |||||||||||
| 40 | =cut | ||||||||||
| 41 | |||||||||||
| 42 | 6 | has 'data' => ( isa => 'Maybe[HashRef]', is => 'ro', lazy_build => 1 ); | |||||||||
| 43 | has 'posts' => ( | ||||||||||
| 44 | traits => [ 'Array' ], | ||||||||||
| 45 | isa => 'ArrayRef[URL::Social::Reddit::Post]', | ||||||||||
| 46 | is => 'ro', | ||||||||||
| 47 | 2 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 6 | handles => { | |||||||||
| 49 | all_posts => 'elements', | ||||||||||
| 50 | add_post => 'push', | ||||||||||
| 51 | }, | ||||||||||
| 52 | ); | ||||||||||
| 53 | |||||||||||
| 54 | 6 | has 'some_count' => ( isa => 'HashRef[Int]', is => 'ro', lazy_build => 1 ); | |||||||||
| 55 | 6 | has 'upvote_count' => ( isa => 'Int', is => 'ro', lazy_build => 1 ); | |||||||||
| 56 | 6 | has 'downvote_count' => ( isa => 'Int', is => 'ro', lazy_build => 1 ); | |||||||||
| 57 | 6 | has 'comment_count' => ( isa => 'Int', is => 'ro', lazy_build => 1 ); | |||||||||
| 58 | |||||||||||
| 59 | 2 | ▶ | sub _build_data { | ||||||||
|
Subroutine
_build_data: called CC=5 SCAR=17.0
| |||||||||||
| 60 | 2 | my $self = shift; | |||||||||
| 61 | |||||||||||
| 62 | 2 | my $url = 'http://www.reddit.com/api/info.json?url=' . $self->url; | |||||||||
| 63 | |||||||||||
| 64 | 2 | ▶ | if ( my $json = $self->get_url_json($url) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 65 | 2 | ▶ | if ( my $data = $json->{data} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 66 | 2 | ▶ | if ( my $children = $data->{children} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | 2 | foreach my $c ( @{$children} ) { | |||||||||
| 68 | 6 | $c = $c->{data}; | |||||||||
| 69 | |||||||||||
| 70 | my $post = URL::Social::Reddit::Post->new( | ||||||||||
| 71 | author => $c->{author}, | ||||||||||
| 72 | created => $c->{created}, | ||||||||||
| 73 | created_utc => $c->{created_utc}, | ||||||||||
| 74 | domain => $c->{domain}, | ||||||||||
| 75 | upvote_count => $c->{ups}, | ||||||||||
| 76 | downvote_count => $c->{downs}, | ||||||||||
| 77 | comment_count => $c->{num_comments}, | ||||||||||
| 78 | score => $c->{score}, | ||||||||||
| 79 | subreddit => $c->{subreddit}, | ||||||||||
| 80 | permalink => $c->{permalink}, | ||||||||||
| 81 | title => $c->{title}, | ||||||||||
| 82 | 6 | ); | |||||||||
| 83 | |||||||||||
| 84 | 6 | $self->add_post( $post ); | |||||||||
| 85 | } | ||||||||||
| 86 | } | ||||||||||
| 87 | 2 | return $data; | |||||||||
| 88 | } | ||||||||||
| 89 | else { | ||||||||||
| 90 | 0 | return undef; | |||||||||
| 91 | } | ||||||||||
| 92 | } | ||||||||||
| 93 | else { | ||||||||||
| 94 | 0 | return undef; | |||||||||
| 95 | } | ||||||||||
| 96 | } | ||||||||||
| 97 | |||||||||||
| 98 | 2 | ▶ | sub _build_some_count { | ||||||||
|
Subroutine
_build_some_count: called CC=2 SCAR=6.9
| |||||||||||
| 99 | 2 | my $self = shift; | |||||||||
| 100 | |||||||||||
| 101 | 2 | $self->_build_data; | |||||||||
| 102 | |||||||||||
| 103 | 2 | my %counts = ( | |||||||||
| 104 | upvote => 0, | ||||||||||
| 105 | downvote => 0, | ||||||||||
| 106 | comment => 0, | ||||||||||
| 107 | ); | ||||||||||
| 108 | |||||||||||
| 109 | 2 | foreach my $post ( @{$self->posts} ) { | |||||||||
| 110 | 6 | $counts{upvote} += $post->upvote_count; | |||||||||
| 111 | 6 | $counts{downvote} += $post->downvote_count; | |||||||||
| 112 | 6 | $counts{comment} += $post->comment_count; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | 2 | return \%counts; | |||||||||
| 116 | } | ||||||||||
| 117 | |||||||||||
| 118 | =head2 upvote_count | ||||||||||
| 119 | |||||||||||
| 120 | Returns the number of upvotes the post has received. | ||||||||||
| 121 | |||||||||||
| 122 | =cut | ||||||||||
| 123 | |||||||||||
| 124 | 1 | ▶ | sub _build_upvote_count { | ||||||||
|
Subroutine
_build_upvote_count: called CC=1 SCAR=0.0
| |||||||||||
| 125 | 1 | return shift->some_count->{upvote}; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | =head2 downvote_count | ||||||||||
| 129 | |||||||||||
| 130 | Returns the number of downvotes the post has received. | ||||||||||
| 131 | |||||||||||
| 132 | =cut | ||||||||||
| 133 | |||||||||||
| 134 | 1 | ▶ | sub _build_downvote_count { | ||||||||
|
Subroutine
_build_downvote_count: called CC=1 SCAR=0.0
| |||||||||||
| 135 | 1 | return shift->some_count->{downvote}; | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | =head2 comment_count | ||||||||||
| 139 | |||||||||||
| 140 | Returns the number of comments the post has received. | ||||||||||
| 141 | |||||||||||
| 142 | =cut | ||||||||||
| 143 | |||||||||||
| 144 | 2 | ▶ | sub _build_comment_count { | ||||||||
|
Subroutine
_build_comment_count: called CC=1 SCAR=0.0
| |||||||||||
| 145 | 2 | return shift->some_count->{comment}; | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | # | ||||||||||
| 149 | # The End | ||||||||||
| 150 | # | ||||||||||
| 151 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||
| 152 | |||||||||||
| 153 | 6 | 1; | |||||||||
| 154 | |||||||||||
| 155 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 156 | |||||||||||
| 157 | Copyright 2013-2014 Tore Aursand. | ||||||||||
| 158 | |||||||||||
| 159 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 160 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||
| 161 | copy of the full license at: | ||||||||||
| 162 | |||||||||||
| 163 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||
| 164 | |||||||||||
| 165 | Any use, modification, and distribution of the Standard or Modified | ||||||||||
| 166 | Versions is governed by this Artistic License. By using, modifying or | ||||||||||
| 167 | distributing the Package, you accept this license. Do not use, modify, | ||||||||||
| 168 | or distribute the Package, if you do not accept this license. | ||||||||||
| 169 | |||||||||||
| 170 | If your Modified Version has been derived from a Modified Version made | ||||||||||
| 171 | by someone other than you, you are nevertheless required to ensure that | ||||||||||
| 172 | your Modified Version complies with the requirements of this license. | ||||||||||
| 173 | |||||||||||
| 174 | This license does not grant you the right to use any trademark, service | ||||||||||
| 175 | mark, tradename, or logo of the Copyright Holder. | ||||||||||
| 176 | |||||||||||
| 177 | This license includes the non-exclusive, worldwide, free-of-charge | ||||||||||
| 178 | patent license to make, have made, use, offer to sell, sell, import and | ||||||||||
| 179 | otherwise transfer the Package with respect to any patent claims | ||||||||||
| 180 | licensable by the Copyright Holder that are necessarily infringed by the | ||||||||||
| 181 | Package. If you institute patent litigation (including a cross-claim or | ||||||||||
| 182 | counterclaim) against any party alleging that the Package constitutes | ||||||||||
| 183 | direct or contributory patent infringement, then this Artistic License | ||||||||||
| 184 | to you shall terminate on the date that such litigation is filed. | ||||||||||
| 185 | |||||||||||
| 186 | Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER | ||||||||||
| 187 | AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. | ||||||||||
| 188 | THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR | ||||||||||
| 189 | PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY | ||||||||||
| 190 | YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR | ||||||||||
| 191 | CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR | ||||||||||
| 192 | CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, | ||||||||||
| 193 | EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||||||||||