| 1 | package Hubot::Scripts::shorten; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | $Hubot::Scripts::shorten::VERSION = '0.2.8'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use URI::QueryParam; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use HTTP::Request; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use LWP::UserAgent; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use JSON::XS; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use Encode 'decode'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | sub load { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
load: called CC=1 SCAR=0.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | my ( $class, $robot ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 14 | $robot->hear( | ||||||||||||||||||||||||||||||||||||||||||
| 15 | qr/(https?:\/\/\S+)/i, | ||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | my $msg = shift; | |||||||||||||||||||||||||||||||||||||||||
| 18 | 3 | my $bitly = $msg->match->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 19 | 3 | ▶ | if ( length $bitly > 50 | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
length $bitly > 50 and $ENV{"HUBOT_BITLY_USERNAME"} and $ENV{"HUBOT_BITLY_API_KEY"}
length $bitly > 50 and $ENV{"HUBOT_BITLY_USERNAME"}
MC/DC0%0/3
length $bitly > 50 and $ENV{"HUBOT_BITLY_USERNAME"} and $ENV{"HUBOT_BITLY_API_KEY"}
length $bitly > 50
$ENV{"HUBOT_BITLY_USERNAME"}
$ENV{"HUBOT_BITLY_API_KEY"}
Truth table50%2/4
A and B and C
A= length $bitly > 50B= $ENV{"HUBOT_BITLY_USERNAME"}C= $ENV{"HUBOT_BITLY_API_KEY"}
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | $uri->query_form_hash( | ||||||||||||||||||||||||||||||||||||||||||
| 25 | login => $ENV{HUBOT_BITLY_USERNAME}, | ||||||||||||||||||||||||||||||||||||||||||
| 26 | apiKey => $ENV{HUBOT_BITLY_API_KEY}, | ||||||||||||||||||||||||||||||||||||||||||
| 27 | 0 | 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 | ▶ | return unless $res->is_success; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 3 | ▶ | sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=14 SCAR=28.1
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | my ( $body, $hdr ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | ▶ | return if ( !$body || !$hdr->{Status} =~ /^2/ ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $body or !$hdr->{"Status"} =~ /^2/
MC/DC0%0/2
not $body or !$hdr->{"Status"} =~ /^2/
not $body
!$hdr->{"Status"} =~ /^2/
Truth table33%1/3
not(A) or not(B)
A= not $bodyB= !$hdr->{"Status"} =~ /^2/
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||
| 45 | ## content-type | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 3 | my @ct = split( /\s*,\s*/, $hdr->{'content-type'} ); | |||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | if ( grep {/^image\/.+$/i} @ct || grep { !/text/i } @ct ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@ct || grep({not /text/i;} @ct)
MC/DC0%0/2
@ct || grep({not /text/i;} @ct)
@ct
grep({not /text/i;} @ct)
Truth table33%1/3
A || B
A= @ctB= grep({not /text/i;} @ct)
| |||||||||||||||||||||||||||||||||||||||||||
| 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 | 3 | $body =~ s{\r\n}{\n}g; | |||||||||||||||||||||||||||||||||||||||||
| 55 | my @charset_lines | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | = grep { $_ !~ /script/ } grep {/charset/} split /\n/, | |||||||||||||||||||||||||||||||||||||||||
| 57 | $body; | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 3 | my $charset; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 3 | ▶ | if ( "@{[ @charset_lines ]}" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 60 | =~ /charset=(?:'([^']+?)'|"([^"]+?)"|([a-zA-Z0-9_-]+)\b)/ | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ) | ||||||||||||||||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | ▶ | $charset = lc( $1 || $2 || $3 ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%4/6
$1 || $2 || $3
$1 or $2
MC/DC0%0/3
$1 || $2 || $3
$1
$2
$3
Truth table50%2/4
A or B || C
A= $1B= $2C= $3
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | unless ($charset) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 1 | for my $ct (@ct) { | |||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | if ( $ct =~ m/charset\s*=\s*(.*)$/i ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 69 | 1 | $charset = $1; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $charset = 'utf-8'; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 3 | ▶ | $charset = 'euckr' if $charset =~ m/ksc5601/i; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | 3 | eval { $body = decode( $charset, $body ) }; | |||||||||||||||||||||||||||||||||||||||||
| 79 | 3 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 0 | return $msg->send("[$@] - $bitly"); | |||||||||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | my ($title) = $body =~ m/<title>(.*?)<\/title>/is; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | $title = 'no title' unless $title; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | $title =~ s/\n//g; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | $title =~ s/(^\s+|\s+$)//g; | |||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | ## unescape html | ||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | $title =~ s/&/&/g; | |||||||||||||||||||||||||||||||||||||||||
| 90 | 3 | $title =~ s/</</g; | |||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | $title =~ s/>/>/g; | |||||||||||||||||||||||||||||||||||||||||
| 92 | 3 | $title =~ s/"/"/g; | |||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | $title =~ s/'/'/g; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 3 | $msg->send("[$title] - $bitly"); | |||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | ); | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 2 | 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.8 | ||||||||||||||||||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||||||||||||||||