Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
82 lines (68 sloc) 1.89 KB
#!/usr/bin/perl
package BotModules::Instagram;
use LWP::Protocol::https;
use LWP;
use JSON qw(decode_json);
use URI qw();
use URI::QueryParam qw();
use Data::Dumper;
use vars qw(@ISA);
@ISA = qw(BotModules);
1;
sub getInstagramData {
my $self = shift;
my $requestUrl = $_[0];
my $ua = LWP::UserAgent->new();
$ua->agent("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22");
my $request = new HTTP::Request 'GET' => $requestUrl;
$request->header('Accept' => 'application/json');
my $response = $ua->request($request);
my $decoded_json = JSON::decode_json( $response->{'_content'} );
return $decoded_json;
}
sub HeardInstagram {
my $self = shift;
my ($event, $url) = @_;
my $url = "http://api.instagram.com/oembed?url=$url";
my $json = $self->getInstagramData($url);
my $title = $json->{'title'};
my $type = $json->{'type'};
if ($type eq 'video') {
$self->say($event, "[Video] - $title");
} else {
$self->say($event, "[Image] - $title");
}
}
sub ToldInstagram {
my $self = shift;
my ($event, $url) = @_;
my $url = "http://api.instagram.com/oembed?url=$url";
my $json = $self->getInstagramData($url);
my $title = $json->{'title'};
my $type = $json->{'type'};
if ($type eq 'video') {
$self->say($event, "$event->{'from'}: [Video] - $title");
} else {
$self->say($event, "$event->{'from'}: [Image] - $title");
}
}
sub Heard {
my $self = shift;
my ($event, $message) = @_;
if (my ($url) = $message =~ m/.*(http(s)?:\/\/(www\.)?instagram\.com\/p\/[0-9a-zA-Z_?=&-]+)\s*.*$/osi) {
$self->HeardInstagram($event, $url);
} else {
return $self->SUPER::Heard(@_);
}
return 0;
}
sub Told {
my $self = shift;
my ($event, $message) = @_;
if (my ($url) = $message =~ m/.*(http(s)?:\/\/(www\.)?instagram\.com\/p\/[0-9a-zA-Z_?=&-]+)\s*.*$/osi) {
$self->ToldInstagram($event, $url);
} else {
return $self->SUPER::Told(@_);
}
return 0;
}