line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Hubot::Scripts::shorten; |
2
|
|
|
|
|
|
|
$Hubot::Scripts::shorten::VERSION = '0.2.7'; |
3
|
1
|
|
|
1
|
|
1089
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
37
|
|
4
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
5
|
1
|
|
|
1
|
|
25969
|
use URI; |
|
1
|
|
|
|
|
10652
|
|
|
1
|
|
|
|
|
39
|
|
6
|
1
|
|
|
1
|
|
1628
|
use URI::QueryParam; |
|
1
|
|
|
|
|
862
|
|
|
1
|
|
|
|
|
28
|
|
7
|
1
|
|
|
1
|
|
930
|
use HTTP::Request; |
|
1
|
|
|
|
|
30783
|
|
|
1
|
|
|
|
|
37
|
|
8
|
1
|
|
|
1
|
|
1372
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
34686
|
|
|
1
|
|
|
|
|
43
|
|
9
|
1
|
|
|
1
|
|
1617
|
use JSON::XS; |
|
1
|
|
|
|
|
8780
|
|
|
1
|
|
|
|
|
88
|
|
10
|
1
|
|
|
1
|
|
1323
|
use Encode 'decode'; |
|
1
|
|
|
|
|
14649
|
|
|
1
|
|
|
|
|
1157
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub load { |
13
|
0
|
|
|
0
|
0
|
|
my ( $class, $robot ) = @_; |
14
|
|
|
|
|
|
|
$robot->hear( |
15
|
|
|
|
|
|
|
qr/(https?:\/\/\S+)/i, |
16
|
|
|
|
|
|
|
sub { |
17
|
0
|
|
|
0
|
|
|
my $msg = shift; |
18
|
0
|
|
|
|
|
|
my $bitly = $msg->match->[0]; |
19
|
0
|
0
|
0
|
|
|
|
if ( length $bitly > 50 |
|
|
|
0
|
|
|
|
|
20
|
|
|
|
|
|
|
&& $ENV{HUBOT_BITLY_USERNAME} |
21
|
|
|
|
|
|
|
&& $ENV{HUBOT_BITLY_API_KEY} ) |
22
|
|
|
|
|
|
|
{ |
23
|
0
|
|
|
|
|
|
my $uri = URI->new("http://api.bitly.com/v3/shorten"); |
24
|
0
|
|
|
|
|
|
$uri->query_form_hash( |
25
|
|
|
|
|
|
|
login => $ENV{HUBOT_BITLY_USERNAME}, |
26
|
|
|
|
|
|
|
apiKey => $ENV{HUBOT_BITLY_API_KEY}, |
27
|
|
|
|
|
|
|
longUrl => $bitly, |
28
|
|
|
|
|
|
|
format => 'json' |
29
|
|
|
|
|
|
|
); |
30
|
0
|
|
|
|
|
|
my $ua = LWP::UserAgent->new; |
31
|
0
|
|
|
|
|
|
my $req = HTTP::Request->new( 'GET' => $uri ); |
32
|
0
|
|
|
|
|
|
my $res = $ua->request($req); |
33
|
0
|
0
|
|
|
|
|
return unless $res->is_success; |
34
|
0
|
|
|
|
|
|
my $data = decode_json( $res->content ); |
35
|
0
|
|
|
|
|
|
$bitly = $data->{data}{url}; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
$msg->http( $msg->match->[0] )->header( "User-Agent", |
39
|
|
|
|
|
|
|
"Mozilla/5.0 (X11; Linux x86_64; rv:10.0.7) Gecko/20100101 Firefox/10.0.7 Iceweasel/10.0.7" |
40
|
|
|
|
|
|
|
)->get( |
41
|
|
|
|
|
|
|
sub { |
42
|
0
|
|
|
|
|
|
my ( $body, $hdr ) = @_; |
43
|
0
|
0
|
0
|
|
|
|
return if ( !$body || !$hdr->{Status} =~ /^2/ ); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
## content-type |
46
|
0
|
|
|
|
|
|
my @ct = split( /\s*,\s*/, $hdr->{'content-type'} ); |
47
|
0
|
0
|
0
|
|
|
|
if ( grep {/^image\/.+$/i} @ct || grep { !/text/i } @ct ) { |
|
0
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
|
return $msg->send("[$ct[0]] - $bitly"); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
### charset |
52
|
|
|
|
|
|
|
### <meta http-equiv="Content-Type" content="text/html; charset=euc-kr"> |
53
|
|
|
|
|
|
|
### [FILTER] - <script type="text/javascript" src="http://news.chosun.com/dhtm/js/gnb_news_2011.js" charset="euc-kr"></script> |
54
|
0
|
|
|
|
|
|
$body =~ s{\r\n}{\n}g; |
55
|
|
|
|
|
|
|
my @charset_lines |
56
|
0
|
|
|
|
|
|
= grep { $_ !~ /script/ } grep {/charset/} split /\n/, |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
$body; |
58
|
0
|
|
|
|
|
|
my $charset; |
59
|
0
|
0
|
|
|
|
|
if ( "@{[ @charset_lines ]}" |
|
0
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=~ /charset=(?:'([^']+?)'|"([^"]+?)"|([a-zA-Z0-9_-]+)\b)/ |
61
|
|
|
|
|
|
|
) |
62
|
|
|
|
|
|
|
{ |
63
|
0
|
|
0
|
|
|
|
$charset = lc( $1 || $2 || $3 ); |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
0
|
|
|
|
|
unless ($charset) { |
67
|
0
|
|
|
|
|
|
for my $ct (@ct) { |
68
|
0
|
0
|
|
|
|
|
if ( $ct =~ m/charset\s*=\s*(.*)$/i ) { |
69
|
0
|
|
|
|
|
|
$charset = $1; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
else { |
72
|
0
|
|
|
|
|
|
$charset = 'utf-8'; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
$charset = 'euckr' if $charset =~ m/ksc5601/i; |
78
|
0
|
|
|
|
|
|
eval { $body = decode( $charset, $body ) }; |
|
0
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if ($@) { |
80
|
0
|
|
|
|
|
|
return $msg->send("[$@] - $bitly"); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
0
|
|
|
|
|
|
my ($title) = $body =~ m/<title>(.*?)<\/title>/is; |
84
|
0
|
0
|
|
|
|
|
$title = 'no title' unless $title; |
85
|
0
|
|
|
|
|
|
$title =~ s/\n//g; |
86
|
0
|
|
|
|
|
|
$title =~ s/(^\s+|\s+$)//g; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
## unescape html |
89
|
0
|
|
|
|
|
|
$title =~ s/&/&/g; |
90
|
0
|
|
|
|
|
|
$title =~ s/</</g; |
91
|
0
|
|
|
|
|
|
$title =~ s/>/>/g; |
92
|
0
|
|
|
|
|
|
$title =~ s/"/"/g; |
93
|
0
|
|
|
|
|
|
$title =~ s/'/'/g; |
94
|
|
|
|
|
|
|
|
95
|
0
|
|
|
|
|
|
$msg->send("[$title] - $bitly"); |
96
|
|
|
|
|
|
|
} |
97
|
0
|
|
|
|
|
|
); |
98
|
|
|
|
|
|
|
} |
99
|
0
|
|
|
|
|
|
); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=pod |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=encoding utf-8 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 NAME |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Hubot::Scripts::shorten |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 VERSION |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
version 0.2.7 |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=head1 SYNOPSIS |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
<url> - Shorten the URL using bit.ly |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head1 DESCRIPTION |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Shorten URLs with bit.ly |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 CONFIGURATION |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=over |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item HUBOT_BITLY_USERNAME |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=item HUBOT_BITLY_API_KEY |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=back |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 AUTHOR |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Hyungsuk Hong <hshong@perl.kr> |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Hyungsuk Hong. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
143
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=cut |