Created
February 23, 2013 18:50
-
-
Save yseto/5020849 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use LWP::UserAgent; | |
use DBIx::Custom; | |
use DBIx::Connector; | |
use Encode; | |
my $connector = | |
DBIx::Connector->new( 'dbi:mysql:host=localhost;database=database;', | |
'username', 'password', | |
{ %{ DBIx::Custom->new->default_option }, mysql_enable_utf8 => 1 } ); | |
my $db = DBIx::Custom->connect( connector => $connector ); | |
my $array = $db->execute( | |
"SELECT * FROM twitter WHERE text LIKE '%http://t.co%' ORDER BY time DESC ;" | |
); | |
foreach my $p ( @{ $array->all } ) { | |
my $origin = $p->{text}; | |
print encode_utf8($p->{text}) . "\n"; | |
my $text_expand = expandprocess( $p->{text} ); | |
print encode_utf8($text_expand) . "\n"; | |
if ($p->{text} ne $text_expand){ | |
$db->execute("UPDATE twitter SET text2 = :text2 , text = :text WHERE id = :id;", | |
{ | |
text => $text_expand, | |
text2 => $origin , | |
id => $p->{id}, | |
}); | |
print "UPDATE\n"; | |
} | |
print "-----------\n"; | |
} | |
sub uriexpand { | |
my $uri = shift; | |
my $ua = LWP::UserAgent->new; | |
my $request = HTTP::Request->new( GET => $uri ); | |
my $response = $ua->request($request); | |
$ua->timeout(1); | |
$ua->agent( | |
"Opera/9.80 (Windows NT 6.1; WOW64; U; ja) Presto/2.10.289 Version/12.02" | |
); | |
if ( $response->is_success and $response->previous ) { | |
return $response->request->uri; | |
} | |
return $uri; | |
} | |
sub expandprocess { | |
my $msg = shift; | |
$msg =~ | |
s/(https?|ftp)(:\/\/[[:alnum:]\+\$\;\?\.%,!#~*\/:@&=_-]+)/{ &uriexpand($1.$2) }/eg; | |
return $msg; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment