line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hubot::Scripts::googleImage; |
2
|
|
|
|
|
|
|
$Hubot::Scripts::googleImage::VERSION = '0.1.9'; |
3
|
1
|
|
|
1
|
|
2449
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
36
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
5
|
1
|
|
|
1
|
|
6
|
use JSON::XS; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
812
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub load { |
8
|
0
|
|
|
0
|
0
|
|
my ( $class, $robot ) = @_; |
9
|
|
|
|
|
|
|
$robot->respond( |
10
|
|
|
|
|
|
|
qr/(image|img)( me)? (.*)/i, |
11
|
|
|
|
|
|
|
sub { |
12
|
0
|
|
|
0
|
|
|
my $msg = shift; |
13
|
0
|
|
|
|
|
|
imageMe( $msg, $msg->match->[2], sub { $msg->send(shift) } ); |
|
0
|
|
|
|
|
|
|
14
|
0
|
|
|
|
|
|
$msg->message->finish; |
15
|
|
|
|
|
|
|
} |
16
|
0
|
|
|
|
|
|
); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
$robot->respond( |
19
|
|
|
|
|
|
|
qr/animate(?: me)? (.*)/i, |
20
|
|
|
|
|
|
|
sub { |
21
|
0
|
|
|
0
|
|
|
my $msg = shift; |
22
|
0
|
|
|
|
|
|
imageMe( $msg, $msg->match->[0], 1, sub { $msg->send(shift) } ); |
|
0
|
|
|
|
|
|
|
23
|
0
|
|
|
|
|
|
$msg->message->finish; |
24
|
|
|
|
|
|
|
} |
25
|
0
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
$robot->respond( |
28
|
|
|
|
|
|
|
qr/(?:mo?u)?sta(?:s|c)he?(?: me)? (.*)/i, |
29
|
|
|
|
|
|
|
sub { |
30
|
0
|
|
|
0
|
|
|
my $msg = shift; |
31
|
0
|
|
|
|
|
|
my $type = int( rand(3) ); |
32
|
0
|
|
|
|
|
|
my $mustachify = "http://mustachify.me/$type?src="; |
33
|
0
|
|
|
|
|
|
my $imagery = $msg->match->[0]; |
34
|
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
|
if ( $imagery =~ /https?:\/\//i ) { |
36
|
0
|
|
|
|
|
|
$msg->send("$mustachify$imagery"); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
else { |
39
|
|
|
|
|
|
|
imageMe( $msg, $imagery, 0, 1, |
40
|
0
|
|
|
|
|
|
sub { $msg->send("$mustachify$imagery") } ); |
|
0
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
0
|
|
|
|
|
|
$msg->message->finish; |
44
|
|
|
|
|
|
|
} |
45
|
0
|
|
|
|
|
|
); |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub imageMe { |
49
|
0
|
|
|
0
|
0
|
|
my ( $msg, $query, $animated, $faces, $cb ) = @_; |
50
|
0
|
0
|
|
|
|
|
$cb = $animated if ref $animated eq 'CODE'; |
51
|
0
|
0
|
0
|
|
|
|
$cb = $faces if defined $faces && ref $faces eq 'CODE'; |
52
|
0
|
|
|
|
|
|
my $q = { v => '1.0', rsz => '8', q => $query, safe => 'active' }; |
53
|
0
|
0
|
0
|
|
|
|
$q->{as_filetype} = 'gif' |
|
|
|
0
|
|
|
|
|
54
|
|
|
|
|
|
|
if defined $animated && ref $animated ne 'CODE' && $animated == 1; |
55
|
0
|
0
|
0
|
|
|
|
$q->{imgtype} = 'face' |
|
|
|
0
|
|
|
|
|
56
|
|
|
|
|
|
|
if defined $faces && ref $faces ne 'CODE' && $faces == 1; |
57
|
|
|
|
|
|
|
$msg->http('http://ajax.googleapis.com/ajax/services/search/images') |
58
|
|
|
|
|
|
|
->query($q)->get( |
59
|
|
|
|
|
|
|
sub { |
60
|
0
|
|
|
0
|
|
|
my ( $body, $hdr ) = @_; |
61
|
0
|
|
|
|
|
|
my $images = decode_json($body); |
62
|
0
|
|
|
|
|
|
$images = $images->{responseData}{results}; |
63
|
0
|
0
|
|
|
|
|
if (@$images) { |
64
|
0
|
|
|
|
|
|
my $image = $msg->random(@$images); |
65
|
0
|
|
|
|
|
|
$cb->( $image->{unescapedUrl} ); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
0
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
1; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 NAME |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Hubot::Scripts::googleImage - A way to interact with the Google Images API. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 VERSION |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
version 0.1.9 |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 SYNOPSIS |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
# required Hubot v0.0.9 or higher |
84
|
|
|
|
|
|
|
hubot image me <query> - The Original. Queries Google Images for <query> and returns a random top result. |
85
|
|
|
|
|
|
|
hubot animate me <query> - The same thing as `image me`, except adds a few parameters to try to return an animated GIF instead. |
86
|
|
|
|
|
|
|
hubot mustache me <url> - Adds a mustache to the specified URL. |
87
|
|
|
|
|
|
|
hubot mustache me <query> - Searches Google Images for the specified query and mustaches it. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head1 AUTHOR |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Hyungsuk Hong <hshong@perl.kr> |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |