| 1 | package Net::Plurk; | ||
| 2 | 10 | ▶ | use feature ':5.10'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 0 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 4 | use URI; | ||
| 5 | use JSON::Any; | ||
| 6 | use AnyEvent::HTTP; | ||
| 7 | use Net::Plurk::UserProfile; | ||
| 8 | use Net::Plurk::OAuth; | ||
| 9 | use DateTime; | ||
| 10 | use Data::Dumper; | ||
| 11 | |||
| 12 | use namespace::autoclean; | ||
| 13 | |||
| 14 | has consumer_key => ( isa => 'Str', is => 'ro'); | ||
| 15 | has consumer_secret => ( isa => 'Str', is => 'ro'); | ||
| 16 | has cookie => ( isa => 'HashRef', is => 'rw', default => sub{{}}); | ||
| 17 | has oauth => ( | ||
| 18 | isa => 'Net::Plurk::OAuth', | ||
| 19 | is => 'rw', | ||
| 20 | lazy_build => 1, | ||
| 21 | builder => '_init_oauth'); | ||
| 22 | has own_profile => ( isa => 'Net::Plurk::UserProfile', is => 'rw'); | ||
| 23 | has publicProfiles => ( isa => 'HashRef', is => 'rw', default => sub {{}}); | ||
| 24 | #has plurks => ( isa => 'ArrayRef[Net::Plurk::Plurk]', is => 'rw', default => sub {[]}); | ||
| 25 | #has plurk_users => ( isa => 'HashRef[Net::Plurk::User]', is => 'rw', default => sub {{}}); | ||
| 26 | has plurks => ( isa => 'ArrayRef', is => 'rw', default => sub {[]}); | ||
| 27 | has plurk_users => ( isa => 'HashRef', is => 'rw', default => sub {{}}); | ||
| 28 | has lastPollingTime => (isa => 'DateTime', is => 'rw', | ||
| 29 | # default is 5 mins ago | ||
| 30 | default => sub {DateTime->from_epoch( epoch => time() - 5 * 60 );} | ||
| 31 | ); | ||
| 32 | has events => ( isa => 'HashRef[CodeRef]', is => 'rw', default => sub {{}}); | ||
| 33 | has unreadCount => ( isa => 'Int', is => 'ro' ); | ||
| 34 | has unreadAll => ( isa => 'Int', is => 'ro' ); | ||
| 35 | has unreadMy => ( isa => 'Int', is => 'ro' ); | ||
| 36 | has unreadPrivate => ( isa => 'Int', is => 'ro' ); | ||
| 37 | has unreadResponded => ( isa => 'Int', is => 'ro' ); | ||
| 38 | has raw_output => ( isa => 'Int', is => 'rw', default => 0); | ||
| 39 | |||
| 40 | =head1 NAME | ||
| 41 | |||
| 42 | Net::Plurk - A perl interface to Plurk API | ||
| 43 | |||
| 44 | =head1 VERSION | ||
| 45 | |||
| 46 | Version 0.11 | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | our $VERSION = '0.11'; | ||
| 51 | |||
| 52 | =head1 SYNOPSIS | ||
| 53 | |||
| 54 | Quick summary of what the module does. | ||
| 55 | |||
| 56 | Perhaps a little code snippet. | ||
| 57 | |||
| 58 | use Net::Plurk; | ||
| 59 | my $key = $CONSUMER_KEY; | ||
| 60 | my $secret = $CONSUMER_SECRET; | ||
| 61 | my $access_token = $ACCESS_TOKEN | ||
| 62 | my $access_secret = $ACCESS_TOKEN_SECRET | ||
| 63 | my $p = Net::Plurk->new(consumer_key => $key, consumer_secret => $secret); | ||
| 64 | my $p->authorize(access_token => $access_token, | ||
| 65 | access_token_secret => $access_secret) | ||
| 66 | my $profile = $p->get_own_profile(); | ||
| 67 | $p->add_events( | ||
| 68 | on_new_plurk => sub { | ||
| 69 | my $plurk = shift; | ||
| 70 | use Data::Dumper; warn Dumper $plurk; | ||
| 71 | }, | ||
| 72 | on_private_plurk => sub { | ||
| 73 | my $plurk = shift; | ||
| 74 | # blah | ||
| 75 | }, | ||
| 76 | ); | ||
| 77 | $p->listen; | ||
| 78 | my $json = $p->callAPI( '/api'); | ||
| 79 | ... | ||
| 80 | |||
| 81 | =head1 EXPORT | ||
| 82 | |||
| 83 | A list of functions that can be exported. You can delete this section | ||
| 84 | if you don't export anything, such as for a purely object-oriented module. | ||
| 85 | |||
| 86 | =head1 FUNCTIONS | ||
| 87 | |||
| 88 | =head2 callAPI | ||
| 89 | |||
| 90 | Everything from here | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub _init_oauth { | ||
| 95 | my ($self) = @_; | ||
| 96 | return Net::Plurk::OAuth->new( | ||
| 97 | consumer_key => $self->consumer_key, | ||
| 98 | consumer_secret => $self->consumer_secret | ||
| 99 | ); | ||
| 100 | } | ||
| 101 | |||
| 102 | =head2 errorcode | ||
| 103 | |||
| 104 | return errorcode | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | sub errorcode { | ||
| 109 | my ($self) = @_; | ||
| 110 | return $self->oauth->_errorcode; | ||
| 111 | } | ||
| 112 | |||
| 113 | =head2 errormsg | ||
| 114 | |||
| 115 | return errormsg | ||
| 116 | |||
| 117 | =cut | ||
| 118 | |||
| 119 | sub errormsg { | ||
| 120 | my ($self) = @_; | ||
| 121 | return $self->oauth->_errormsg; | ||
| 122 | } | ||
| 123 | |||
| 124 | =head2 authorize | ||
| 125 | |||
| 126 | authorize with access token/secret | ||
| 127 | |||
| 128 | =cut | ||
| 129 | |||
| 130 | sub authorize { | ||
| 131 | my ($self, %args) = @_; | ||
| 132 | return $self->oauth->authorize( | ||
| 133 | access_token => $args{'token'} || $args{'access_token'}, | ||
| 134 | access_token_secret => $args{'token_secret'} || $args{'access_token_secret'}, | ||
| 135 | ) | ||
| 136 | }; | ||
| 137 | |||
| 138 | =head2 callAPI | ||
| 139 | |||
| 140 | Everything from here | ||
| 141 | |||
| 142 | =cut | ||
| 143 | |||
| 144 | sub callAPI { | ||
| 145 | my ($self, $path, %args) = @_; | ||
| 146 | my ($data, $header); | ||
| 147 | |||
| 148 | $path = '/APP'.$path unless $path =~ m#/(?:APP|OAuth)#; | ||
| 149 | return $self->oauth->request($path, %args); | ||
| 150 | } | ||
| 151 | |||
| 152 | =head2 _get_unique_id | ||
| 153 | |||
| 154 | given nick_name, return unique_id | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub _get_unique_id { | ||
| 159 | my ($self, $nick_name) = @_; | ||
| 160 | # check if we have it in cache, since we only want to retreive unique id | ||
| 161 | $self->get_public_profile($nick_name) unless $self->publicProfiles->{ $nick_name }; | ||
| 162 | return $self->publicProfiles->{$nick_name}->{user_info}->id; | ||
| 163 | } | ||
| 164 | |||
| 165 | =head2 get_nick_name | ||
| 166 | |||
| 167 | given unique_id, return nick_name | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub get_nick_name { | ||
| 172 | my ($self, $id) = @_; | ||
| 173 | |||
| 174 | $self->get_own_profile() unless $self->own_profile; | ||
| 175 | return $self->own_profile->nick_name | ||
| 176 | if $self->own_profile->{user_info}->{id} eq $id; | ||
| 177 | |||
| 178 | my $profile = $self->get_public_profile($id); | ||
| 179 | return $profile->nick_name; | ||
| 180 | } | ||
| 181 | |||
| 182 | =head2 get_public_profile | ||
| 183 | |||
| 184 | call /Profile/getPublicProfile | ||
| 185 | |||
| 186 | =cut | ||
| 187 | |||
| 188 | sub get_public_profile { | ||
| 189 | my ($self, $user) = @_; | ||
| 190 | my $json_data = $self->callAPI( | ||
| 191 | '/Profile/getPublicProfile', | ||
| 192 | user_id => $user, | ||
| 193 | ); | ||
| 194 | return $json_data if $self->raw_output; | ||
| 195 | $self->publicProfiles->{ $user } = Net::Plurk::PublicUserProfile->new($json_data); | ||
| 196 | return $self->publicProfiles->{ $user }; | ||
| 197 | } | ||
| 198 | |||
| 199 | =head2 get_own_profile | ||
| 200 | |||
| 201 | call /Profile/getOwnProfile | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub get_own_profile { | ||
| 206 | my ($self, $user) = @_; | ||
| 207 | my $json_data = $self->callAPI( | ||
| 208 | '/Profile/getOwnProfile', | ||
| 209 | ); | ||
| 210 | return $json_data if $self->raw_output; | ||
| 211 | $self->own_profile(Net::Plurk::UserProfile->new($json_data)); | ||
| 212 | return $self->own_profile; | ||
| 213 | } | ||
| 214 | |||
| 215 | =head2 get_new_plurks | ||
| 216 | |||
| 217 | call /Polling/getPlurks | ||
| 218 | arguments => | ||
| 219 | offset: Return plurks newer than offset, formatted as 2009-6-20T21:55:34. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | sub get_new_plurks { | ||
| 224 | my ($self, %args) = @_; | ||
| 225 | $args{offset} //= $self->lastPollingTime; | ||
| 226 | $args{limit} //= 50; # default is 50 | ||
| 227 | $args{offset} = $args{offset}->strftime("%Y-%m-%dT%H:%M:%S"); | ||
| 228 | my $json_data = $self->callAPI( | ||
| 229 | '/Polling/getPlurks', | ||
| 230 | offset => $args{offset}, | ||
| 231 | limit => $args{limit}, | ||
| 232 | ); | ||
| 233 | return $json_data if $self->raw_output; | ||
| 234 | $self->plurk_users($json_data->{plurk_users}); | ||
| 235 | $self->plurks($json_data->{plurks}); | ||
| 236 | $self->lastPollingTime(DateTime->now); | ||
| 237 | return $self->plurks; | ||
| 238 | } | ||
| 239 | |||
| 240 | =head2 karma | ||
| 241 | |||
| 242 | return user's karma, or specify user => 'who' | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub karma { | ||
| 247 | my ($self, %args) = @_; | ||
| 248 | return $self->get_public_profile($args{user})->user_info->karma if $args{user}; | ||
| 249 | return 0; | ||
| 250 | # TODO: if authorized, return own karma, otherwise fail? | ||
| 251 | #return $self->own_profile->user_info->karma; | ||
| 252 | } | ||
| 253 | |||
| 254 | =head2 follow | ||
| 255 | |||
| 256 | return 1 if followed someone, 0 otherwise (see errormsg) | ||
| 257 | |||
| 258 | =cut | ||
| 259 | |||
| 260 | sub follow { | ||
| 261 | my ($self, $user_id) = @_; | ||
| 262 | |||
| 263 | # if input user nick_name instead of unique id | ||
| 264 | $user_id = $self->_get_unique_id($user_id) if $user_id !~ m/^\d+$/; | ||
| 265 | |||
| 266 | my $json_data = $self->callAPI( | ||
| 267 | '/FriendsFans/setFollowing', | ||
| 268 | user_id => $user_id, | ||
| 269 | follow => 'true', | ||
| 270 | ); | ||
| 271 | return $json_data if $self->raw_output; | ||
| 272 | return 0 if $self->errormsg; | ||
| 273 | return 1; | ||
| 274 | } | ||
| 275 | |||
| 276 | =head2 unfollow | ||
| 277 | |||
| 278 | return 1 if unfollowed someone, 0 otherwise (see errormsg) | ||
| 279 | |||
| 280 | =cut | ||
| 281 | |||
| 282 | sub unfollow { | ||
| 283 | my ($self, $user_id) = @_; | ||
| 284 | |||
| 285 | # if input user nick_name instead of unique id | ||
| 286 | $user_id = $self->_get_unique_id($user_id) if $user_id !~ m/^\d+$/; | ||
| 287 | |||
| 288 | my $json_data = $self->callAPI( | ||
| 289 | '/FriendsFans/setFollowing', | ||
| 290 | user_id => $user_id, | ||
| 291 | follow => 'false', | ||
| 292 | ); | ||
| 293 | return $json_data if $self->raw_output; | ||
| 294 | return 0 if $self->errormsg; | ||
| 295 | return 1; | ||
| 296 | } | ||
| 297 | |||
| 298 | =head2 add_plurk | ||
| 299 | |||
| 300 | add_plurk ($content, $qualifier %opt) | ||
| 301 | %opt: limited_to, no_comment, lang | ||
| 302 | |||
| 303 | =cut | ||
| 304 | |||
| 305 | sub add_plurk { | ||
| 306 | my ($self, $content, $qualifier, %opt) = @_; | ||
| 307 | $qualifier //= 'says'; | ||
| 308 | my $json_data = $self->callAPI( | ||
| 309 | '/Timeline/plurkAdd', | ||
| 310 | qualifier => $qualifier, | ||
| 311 | content => $content, | ||
| 312 | %opt, | ||
| 313 | ); | ||
| 314 | return $json_data if $self->raw_output; | ||
| 315 | return Net::Plurk::Plurk->new($json_data) if !$self->errormsg; | ||
| 316 | return ; | ||
| 317 | } | ||
| 318 | |||
| 319 | =head2 get_plurk | ||
| 320 | |||
| 321 | get_plurk ($plurk_id) | ||
| 322 | $plurk_id can be base 36 encoded, or not | ||
| 323 | |||
| 324 | =cut | ||
| 325 | |||
| 326 | sub get_plurk { | ||
| 327 | my ($self, $plurk_id) = @_; | ||
| 328 | use Math::Base36 ':all'; | ||
| 329 | $plurk_id = decode_base36($plurk_id) unless $plurk_id =~ m/^\d+$/m; | ||
| 330 | my $json_data = $self->callAPI( | ||
| 331 | '/Timeline/getPlurk', | ||
| 332 | plurk_id => $plurk_id, | ||
| 333 | ); | ||
| 334 | return $json_data if $self->raw_output; | ||
| 335 | # XXX: didn't handle $json_data->{user} | ||
| 336 | return Net::Plurk::Plurk->new($json_data->{plurk}) if !$self->errormsg; | ||
| 337 | return ; | ||
| 338 | } | ||
| 339 | |||
| 340 | =head1 AUTHOR | ||
| 341 | |||
| 342 | Cheng-Lung Sung, C<< <clsung at cpan.org> >> | ||
| 343 | |||
| 344 | =head1 BUGS | ||
| 345 | |||
| 346 | Please report any bugs or feature requests to C<bug-net-plurk at rt.cpan.org>, or through | ||
| 347 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-Plurk>. I will be notified, and then you'll | ||
| 348 | automatically be notified of progress on your bug as I make changes. | ||
| 349 | |||
| 350 | |||
| 351 | |||
| 352 | |||
| 353 | =head1 SUPPORT | ||
| 354 | |||
| 355 | You can find documentation for this module with the perldoc command. | ||
| 356 | |||
| 357 | perldoc Net::Plurk | ||
| 358 | |||
| 359 | |||
| 360 | You can also look for information at: | ||
| 361 | |||
| 362 | =over 4 | ||
| 363 | |||
| 364 | =item * RT: CPAN's request tracker | ||
| 365 | |||
| 366 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-Plurk> | ||
| 367 | |||
| 368 | =item * AnnoCPAN: Annotated CPAN documentation | ||
| 369 | |||
| 370 | L<http://annocpan.org/dist/Net-Plurk> | ||
| 371 | |||
| 372 | =item * CPAN Ratings | ||
| 373 | |||
| 374 | L<http://cpanratings.perl.org/d/Net-Plurk> | ||
| 375 | |||
| 376 | =item * Search CPAN | ||
| 377 | |||
| 378 | L<http://search.cpan.org/dist/Net-Plurk/> | ||
| 379 | |||
| 380 | =back | ||
| 381 | |||
| 382 | |||
| 383 | =head1 ACKNOWLEDGEMENTS | ||
| 384 | |||
| 385 | |||
| 386 | =head1 COPYRIGHT & LICENSE | ||
| 387 | |||
| 388 | Copyright 2009~2011 Cheng-Lung Sung, all rights reserved. | ||
| 389 | |||
| 390 | This program is free software; you can redistribute it and/or modify it | ||
| 391 | under the same terms as Perl itself. | ||
| 392 | |||
| 393 | |||
| 394 | =cut | ||
| 395 | |||
| 396 | no Moose; | ||
| 397 | __PACKAGE__->meta->make_immutable; | ||
| 398 | 1; # End of Net::Plurk | ||