| 1 | |||
| 2 | =head1 NAME | ||
| 3 | |||
| 4 | WWW::PushBullet - Module giving easy access to PushBullet API | ||
| 5 | |||
| 6 | =head1 DESCRIPTION | ||
| 7 | |||
| 8 | Module giving easy access to PushBullet API | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | use WWW::PushBullet; | ||
| 13 | |||
| 14 | my $pb = WWW::PushBullet->new({apikey => $apikey}); | ||
| 15 | |||
| 16 | $pb->push_address({ device_id => $device_id, name => $name, | ||
| 17 | address => $address }); | ||
| 18 | |||
| 19 | $pb->push_file({ device_id => $device_id, file => $filename); | ||
| 20 | |||
| 21 | $pb->push_link({ device_id => $device_id, title => $title, | ||
| 22 | url => $url }); | ||
| 23 | |||
| 24 | $pb->push_list({ device_id => $device_id, title => $title, | ||
| 25 | items => \@items }); | ||
| 26 | |||
| 27 | $pb->push_note({ device_id => $device_id, title => $title, | ||
| 28 | body => $body }); | ||
| 29 | |||
| 30 | =cut | ||
| 31 | |||
| 32 | package WWW::PushBullet; | ||
| 33 | |||
| 34 | use strict; | ||
| 35 | use warnings; | ||
| 36 | |||
| 37 | use Data::Dump qw(dump); | ||
| 38 | use JSON; | ||
| 39 | use LWP::UserAgent; | ||
| 40 | |||
| 41 | our $VERSION = '1.2.0'; | ||
| 42 | |||
| 43 | my %PUSHBULLET = ( | ||
| 44 | REALM => 'Pushbullet', | ||
| 45 | SERVER => 'api.pushbullet.com:443', | ||
| 46 | URL_APIV2 => 'https://api.pushbullet.com/v2', | ||
| 47 | ); | ||
| 48 | |||
| 49 | $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; | ||
| 50 | |||
| 51 | =head1 SUBROUTINES/METHODS | ||
| 52 | |||
| 53 | =head2 new($params) | ||
| 54 | |||
| 55 | Creates a new instance of PushBullet API | ||
| 56 | |||
| 57 | my $pb = WWW::PushBullet->new({apikey => $apikey}); | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub new | ||
| 62 | { | ||
| 63 | my ($class, $params) = @_; | ||
| 64 | |||
| 65 | return (undef) if (!defined $params->{apikey}); | ||
| 66 | my $ua = LWP::UserAgent->new; | ||
| 67 | $ua->agent("WWW::PushBullet/$VERSION"); | ||
| 68 | $ua->proxy('https', $params->{proxy}) if (defined $params->{proxy}); | ||
| 69 | $ua->credentials($PUSHBULLET{SERVER}, $PUSHBULLET{REALM}, | ||
| 70 | $params->{apikey}, ''); | ||
| 71 | |||
| 72 | my $self = { | ||
| 73 | _ua => $ua, | ||
| 74 | _apikey => $params->{apikey}, | ||
| 75 | _debug => $params->{debug} || 0, | ||
| 76 | }; | ||
| 77 | |||
| 78 | bless $self, $class; | ||
| 79 | |||
| 80 | return ($self); | ||
| 81 | } | ||
| 82 | |||
| 83 | =head2 DEBUG | ||
| 84 | |||
| 85 | Prints Debug message when '_debug' is enabled | ||
| 86 | |||
| 87 | =cut | ||
| 88 | |||
| 89 | sub DEBUG | ||
| 90 | { | ||
| 91 | my ($self, $line) = @_; | ||
| 92 | |||
| 93 | if ($self->{_debug}) | ||
| 94 | { | ||
| 95 | my $str = sprintf '[DEBUG] %s', $line; | ||
| 96 | printf "$str\n"; | ||
| 97 | |||
| 98 | return ($str); | ||
| 99 | } | ||
| 100 | |||
| 101 | return (undef); | ||
| 102 | } | ||
| 103 | |||
| 104 | =head2 api_key() | ||
| 105 | |||
| 106 | Returns current PushBullet API key | ||
| 107 | |||
| 108 | my $apikey = $pb->api_key(); | ||
| 109 | |||
| 110 | =cut | ||
| 111 | |||
| 112 | sub api_key | ||
| 113 | { | ||
| 114 | my $self = shift; | ||
| 115 | |||
| 116 | return ($self->{_apikey}); | ||
| 117 | } | ||
| 118 | |||
| 119 | =head2 debug_mode | ||
| 120 | |||
| 121 | Sets Debug mode | ||
| 122 | |||
| 123 | $pb->debug_mode(1); | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub debug_mode | ||
| 128 | { | ||
| 129 | my ($self, $mode) = @_; | ||
| 130 | |||
| 131 | $self->{_debug} = $mode; | ||
| 132 | |||
| 133 | return ($self->{_debug}); | ||
| 134 | } | ||
| 135 | |||
| 136 | =head2 contacts() | ||
| 137 | |||
| 138 | Returns list of contacts | ||
| 139 | |||
| 140 | my $contacts = $pb->contacts(); | ||
| 141 | |||
| 142 | foreach my $c (@{$contacts}) | ||
| 143 | { | ||
| 144 | printf "Device '%s' => id %s\n", $d->{extras}->{model}, $d->{id}; | ||
| 145 | } | ||
| 146 | |||
| 147 | =cut | ||
| 148 | |||
| 149 | sub contacts | ||
| 150 | { | ||
| 151 | my $self = shift; | ||
| 152 | |||
| 153 | my $res = $self->{_ua}->get("$PUSHBULLET{URL_APIV2}/contacts"); | ||
| 154 | |||
| 155 | if ($res->is_success) | ||
| 156 | { | ||
| 157 | my $data = JSON->new->decode($res->content); | ||
| 158 | return ($data->{contacts}); | ||
| 159 | } | ||
| 160 | else | ||
| 161 | { | ||
| 162 | print $res->status_line, "\n"; | ||
| 163 | return (undef); | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | =head2 devices() | ||
| 168 | |||
| 169 | Returns list of devices | ||
| 170 | |||
| 171 | my $devices = $pb->devices(); | ||
| 172 | |||
| 173 | foreach my $d (@{$devices}) | ||
| 174 | { | ||
| 175 | printf "Device '%s' (%s)=> id %s\n", | ||
| 176 | $d->{nickname}, $d->{model}, $d->{iden}; | ||
| 177 | } | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub devices | ||
| 182 | { | ||
| 183 | my $self = shift; | ||
| 184 | |||
| 185 | my $res = $self->{_ua}->get("$PUSHBULLET{URL_APIV2}/devices"); | ||
| 186 | |||
| 187 | if ($res->is_success) | ||
| 188 | { | ||
| 189 | my $data = JSON->new->decode($res->content); | ||
| 190 | return ($data->{devices}); | ||
| 191 | } | ||
| 192 | else | ||
| 193 | { | ||
| 194 | print $res->status_line, "\n"; | ||
| 195 | return (undef); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | =head2 _pushes($content) | ||
| 200 | |||
| 201 | Generic pushes function (not supposed to be used directly) | ||
| 202 | |||
| 203 | =cut | ||
| 204 | |||
| 205 | sub _pushes | ||
| 206 | { | ||
| 207 | my ($self, $content) = @_; | ||
| 208 | |||
| 209 | my $type = undef; | ||
| 210 | foreach my $i (0 .. $#{$content}) | ||
| 211 | { | ||
| 212 | $type = $content->[$i + 1] if ($content->[$i] eq 'type'); | ||
| 213 | } | ||
| 214 | my $res = $self->{_ua}->post( | ||
| 215 | "$PUSHBULLET{URL_APIV2}/pushes", | ||
| 216 | Content_Type => ($type eq 'file' ? 'form-data' : undef), | ||
| 217 | Content => $content | ||
| 218 | ); | ||
| 219 | |||
| 220 | if ($res->is_success) | ||
| 221 | { | ||
| 222 | my $data = JSON->new->decode($res->content); | ||
| 223 | return ($data); | ||
| 224 | } | ||
| 225 | else | ||
| 226 | { | ||
| 227 | print $res->status_line, "\n"; | ||
| 228 | return (undef); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | =head2 push_address($params) | ||
| 233 | |||
| 234 | Pushes address (with name & address) | ||
| 235 | |||
| 236 | $pb->push_address( | ||
| 237 | { | ||
| 238 | device_id => $device_id, | ||
| 239 | name => 'GooglePlex', | ||
| 240 | address => '1600 Amphitheatre Pkwy, Mountain View, CA 94043, Etats-Unis' | ||
| 241 | } | ||
| 242 | ); | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub push_address | ||
| 247 | { | ||
| 248 | my ($self, $params) = @_; | ||
| 249 | |||
| 250 | my $content = [ | ||
| 251 | type => 'address', | ||
| 252 | device_id => $params->{device_id}, | ||
| 253 | name => $params->{name}, | ||
| 254 | address => $params->{address}, | ||
| 255 | ]; | ||
| 256 | $self->DEBUG(sprintf('push_address: %s', dump($content))); | ||
| 257 | my $result = $self->_pushes($content); | ||
| 258 | |||
| 259 | return ($result); | ||
| 260 | } | ||
| 261 | |||
| 262 | =head2 push_file($params) | ||
| 263 | |||
| 264 | Pushes file | ||
| 265 | |||
| 266 | $pb->push_file({ device_id => $device_id, file => '/var/www/index.html' }); | ||
| 267 | |||
| 268 | =cut | ||
| 269 | |||
| 270 | sub push_file | ||
| 271 | { | ||
| 272 | my ($self, $params) = @_; | ||
| 273 | |||
| 274 | my $content = [ | ||
| 275 | type => 'file', | ||
| 276 | device_id => $params->{device_id}, | ||
| 277 | file => [$params->{file}], | ||
| 278 | ]; | ||
| 279 | $self->DEBUG(sprintf('push_file: %s', dump($content))); | ||
| 280 | my $result = $self->_pushes($content); | ||
| 281 | |||
| 282 | return ($result); | ||
| 283 | } | ||
| 284 | |||
| 285 | =head2 push_link($params) | ||
| 286 | |||
| 287 | Pushes link (with title & url) | ||
| 288 | |||
| 289 | $pb->push_link( | ||
| 290 | { | ||
| 291 | device_id => $device_id, | ||
| 292 | title => 'WWW::PushBullet Perl module on GitHub', | ||
| 293 | url => 'https://github.com/sebthebert/WWW-PushBullet' | ||
| 294 | } | ||
| 295 | ); | ||
| 296 | |||
| 297 | =cut | ||
| 298 | |||
| 299 | sub push_link | ||
| 300 | { | ||
| 301 | my ($self, $params) = @_; | ||
| 302 | |||
| 303 | my $content = [ | ||
| 304 | type => 'link', | ||
| 305 | device_id => $params->{device_id}, | ||
| 306 | title => $params->{title}, | ||
| 307 | url => $params->{url}, | ||
| 308 | ]; | ||
| 309 | $self->DEBUG(sprintf('push_link: %s', dump($content))); | ||
| 310 | my $result = $self->_pushes($content); | ||
| 311 | |||
| 312 | return ($result); | ||
| 313 | } | ||
| 314 | |||
| 315 | =head2 push_list($params) | ||
| 316 | |||
| 317 | Pushes list (with title & items) | ||
| 318 | |||
| 319 | $pb->push_list( | ||
| 320 | { | ||
| 321 | device_id => $device_id, | ||
| 322 | title => 'One list with 3 items', | ||
| 323 | items => [ 'One', 'Two', 'Three' ] | ||
| 324 | } | ||
| 325 | ); | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | sub push_list | ||
| 330 | { | ||
| 331 | my ($self, $params) = @_; | ||
| 332 | |||
| 333 | my $content = [ | ||
| 334 | type => 'list', | ||
| 335 | device_id => $params->{device_id}, | ||
| 336 | title => $params->{title}, | ||
| 337 | items => $params->{items}, | ||
| 338 | ]; | ||
| 339 | $self->DEBUG(sprintf('push_list: %s', dump($content))); | ||
| 340 | my $result = $self->_pushes($content); | ||
| 341 | |||
| 342 | return ($result); | ||
| 343 | } | ||
| 344 | |||
| 345 | =head2 push_note($params) | ||
| 346 | |||
| 347 | Pushes note (with title & body) | ||
| 348 | |||
| 349 | $pb->push_note( | ||
| 350 | { | ||
| 351 | device_id => $device_id, | ||
| 352 | title => 'Note Title', | ||
| 353 | body => 'Note Body' | ||
| 354 | } | ||
| 355 | ); | ||
| 356 | |||
| 357 | =cut | ||
| 358 | |||
| 359 | sub push_note | ||
| 360 | { | ||
| 361 | my ($self, $params) = @_; | ||
| 362 | |||
| 363 | my $content = [ | ||
| 364 | type => 'note', | ||
| 365 | device_id => $params->{device_id}, | ||
| 366 | title => $params->{title}, | ||
| 367 | body => $params->{body}, | ||
| 368 | ]; | ||
| 369 | $self->DEBUG(sprintf('push_note: %s', dump($content))); | ||
| 370 | my $result = $self->_pushes($content); | ||
| 371 | |||
| 372 | return ($result); | ||
| 373 | } | ||
| 374 | |||
| 375 | =head2 version() | ||
| 376 | |||
| 377 | Returns WWW::PushBullet module version | ||
| 378 | |||
| 379 | =cut | ||
| 380 | |||
| 381 | sub version | ||
| 382 | { | ||
| 383 | return ($VERSION); | ||
| 384 | } | ||
| 385 | |||
| 386 | 1; | ||
| 387 | |||
| 388 | =head1 LICENSE | ||
| 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 | =head1 AUTHOR | ||
| 394 | |||
| 395 | Sebastien Thebert <www-pushbullet@onetool.pm> | ||
| 396 | |||
| 397 | =cut |