Created
February 18, 2010 08:25
-
-
Save makotoworld/307474 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/perl | |
use strict; | |
use warnings; | |
use WebService::Simple; | |
use WebService::Simple::Parser::JSON; | |
use Perl6::Say; | |
use Data::Dumper; | |
use Encode; | |
use JSON; | |
my @query = qw/聴覚障害 手話 聴覚障がい 聴力障害 ろうあ者 聾/; | |
my $start = 0; | |
sub bit_ly{ | |
my ($URL) = @_; | |
my $parser = WebService::Simple::Parser::JSON->new; | |
$parser->{json}->utf8(1); | |
my $bit_ly = WebService::Simple->new( | |
base_url => "http://api.bit.ly/shorten", | |
response_parser => $parser, | |
params => { version => "2.0.1", login => "username", apiKey => "apikey", longUrl => $URL } | |
); | |
$bit_ly->proxy(['http'], 'http://example.com:8080'); | |
my $response = $bit_ly->get(); | |
return $response->parse_response->{results}->{$URL}->{shortUrl}; | |
} | |
sub google_news{ | |
my ($query) = @_; | |
my $parser = WebService::Simple::Parser::JSON->new; | |
$parser->{json}->utf8(1); | |
my $google = WebService::Simple->new( | |
base_url => "http://ajax.googleapis.com/ajax/services/search/news", | |
response_parser => $parser, | |
params => { v => "1.0", rsz => "large", hl => "ja", scoring => "d", ned => "jp" } | |
); | |
$google->proxy(['http'], 'http://example.com:8080'); | |
my $response = $google->get( { q => $query, start => $start } ); | |
#say Dumper($response->parse_response); | |
say $response->parse_response->{responseData}->{cursor}->{estimatedResultCount}; | |
my $count = $response->parse_response->{responseData}->{cursor}->{estimatedResultCount} % 8; | |
say $count; | |
for(my $i = 0; $i <= 10; $i++){ | |
say $i; | |
$response = $google->get( { q => $query, start => $i*8 } ); | |
foreach my $item (@{$response->parse_response->{responseData}->{results}}){ | |
say encode('shift-jis', $item->{titleNoFormatting}); # Shift-JIS Win32 only | |
# say $item->{titleNoFormatting}; #UTF-8 | |
say $item->{unescapedUrl}; | |
# say bit_ly($item->{unescapedUrl}); # bit_ly 変換 | |
} | |
} | |
} | |
#foreach(@query){ | |
# utf8::decode($_); | |
# google_news($_); | |
#} | |
say bit_ly('http://www.makotoworld.com'); | |
__END__ | |
CREATE TABLE `deaf_news_bot` ( | |
`id` int(10) NOT NULL AUTO_INCREMENT, | |
`title` varchar(20) NOT NULL, | |
`url` varchar(255) NOT NULL, | |
`bitly_url` varchar(25) NOT NULL, | |
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | |
`updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', | |
PRIMARY KEY (`id`), | |
UNIQUE (`url`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8 | |
use lib './db6/'; | |
use Web::DB; | |
my $db = Web::DB->new; | |
my $row = $db->find_or_create('deaf_news_bot', | |
{ | |
title => 'test', | |
url => 'http://example.com', | |
bitly_url => 'http://example.com', | |
} | |
); | |
$row = $db->search('deaf_news_bot')->first; | |
print $row->id."\n"; | |
print $row->title."\n"; | |
print $row->url."\n"; | |
print $row->bitly_url."\n"; | |
print $row->created_at."\n"; | |
print $row->updated_at."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment