Skip to content

Instantly share code, notes, and snippets.

@aerith
Created July 16, 2009 05:16
Show Gist options
  • Save aerith/148237 to your computer and use it in GitHub Desktop.
Save aerith/148237 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/perl
use strict;
use warnings;
use URI;
use IO::Socket;
use MIME::Base64;
use JSON::Syck;
my $config = YAML::Syck::Load(do { local $/; <DATA> });
my ($username, $password) = @{ $config }{qw/username password/};
my $endpoint = URI->new($config->{endpoint});
my $socket = IO::Socket::INET->new(PeerAddr => $endpoint->host, PeerPort => $endpoint->port);
$socket->send(sprintf("GET %s HTTP/1.0\r\n", $endpoint->path));
$socket->send(sprintf("Authorization: Basic %s\r\n\r\n", MIME::Base64::encode_base64("$username:$password")));
my $buffer;
my $flag = 0;
my %header = ();
STREAM: while ( my $buffer = <$socket> ) {
unless ( $flag ) {
if ( $buffer =~ /^HTTP/i ) {
my ($version, $code, $message) = split /\s+/, $buffer, 3;
last unless int $code == 200;
} elsif ( $buffer =~ /^([^\:]+)\:(.+)$/ ) {
$header{ lc $1 } = $2;
} else {
$flag = 1;
}
next;
}
my $content = JSON::Syck::Load($buffer);
print $content->{text}, "\n";
}
$socket->close();
exit;
__END__
endpoint: http://stream.twitter.com/spritzer.json
username: foo
password: hoge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment