Skip to content

Instantly share code, notes, and snippets.

@afresh1
Created April 26, 2012 04:29
Show Gist options
  • Save afresh1/2495830 to your computer and use it in GitHub Desktop.
Save afresh1/2495830 to your computer and use it in GitHub Desktop.
A handy Twitter streaming API script to watch woot's deals (especially when there is a wootoff)
#!/usr/bin/perl
# Copyright (c) 2012 Andrew Fresh <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use 5.010;
use strict;
use warnings;
use Mojo::UserAgent;
# Note this requires IO::Socket::SSL for the https connection
use Mojo::JSON;
use Text::Wrap;
my $username = '';
my $password = '';
$Text::Wrap::columns = 72;
my $url
= "https://$username:$password\@stream.twitter.com/1/statuses/filter.json";
my @track = ( '#woot', '@woot', '#wootoff', '@wootoff' );
my @follow = (
734493, # woot
20557892, # wootoff
7696162, # wootshirt
15458304, # wootsellout
1647621, # wootwine
24259625, # wootdeals
);
my $ua = Mojo::UserAgent->new;
$ua->inactivity_timeout(90);
binmode STDOUT, ":utf8";
my $sleep = 20;
while (1) {
say 'connecting . . . ';
my $tx = $ua->build_form_tx(
$url,
{ track => join( ',', @track ),
follow => join( ',', @follow ),
}
);
$tx->res->content->unsubscribe('read')->on( read => \&got_tweet );
$ua->start($tx);
if ( $tx->res->is_status_class(200) ) {
$sleep = 20 + int rand 20;
}
else {
$sleep *= 2;
$sleep = 240 + int rand 60 if $sleep > 300;
}
say "sleeping for $sleep seconds";
sleep $sleep;
print 're';
}
sub unshorten_url {
my ($url) = @_;
state %cache;
return $cache{$url} if $cache{$url};
my $dest = Mojo::UserAgent->new->max_redirects(5)->get($url)->req->url;
$cache{$url} = $dest;
return $dest;
}
sub got_tweet {
my ( $content, $chunk ) = @_;
my $tweet = Mojo::JSON->decode($chunk);
return unless $tweet;
my $text = $tweet->{user}{screen_name} . ': ' . $tweet->{text};
$text =~ s/[^[:ascii:]]//gxms;
$text =~ s{(https?://\S+)}{unshorten_url($1)}ge;
my @created_at = split ' ', $tweet->{created_at};
my $initial = join ' ', @created_at[ 1, 2, 3 ];
my $secondary = join '', ' ' x length($initial);
my $wrap = '';
if ( $tweet->{user}{id} ~~ @follow ) {
$secondary = join '', "\a", '#*' x ( length($initial) / 2 ), '#';
$wrap = join '', "\a", '#*' x ( $Text::Wrap::columns / 2 - 1 ), '#';
say $wrap;
}
$_ .= ' | ' for $initial, $secondary;
say wrap( $initial, $secondary, $text );
say $wrap if $wrap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment