3779533345_5fe2ae2f09_m

I’ve long been interested in creating some physical world manifestation of [sometimes relevant] events in the virtual world. For FSoM episode 2, we decided to present the notion of a modern newswire. The concept is simple; one just queries a RESTful API or RSS feed and passes the formatted output to a line printer. With an always-on embedded computer, this becomes a device people can incorporate into their every day environments.

So, at this point, now running in the TE shop is a newswire that spits out all mentions on Twitter of my username: @temotorworks. Take advantage of this fact and let us know what you’d like to see on upcoming episodes of Fold, Spindle, or Mutilate.

[ More EpisodesFSoM Archive ]


The Modern Newswire diagram:

Information about the Twitter API used in this video:

Twitter API
Net::Twitter

The Perl script used to follow Twitter name mentions:
(one would run this with the output redirected to /dev/lp0 or whatever your particular line printer device is.)

#!/usr/bin/perl

# Twitter Newswire uses the Twitter API to monitor mentions of 
# one's username and tail's that feed.

use Net::Twitter;
use Getopt::Long;

# Setup auto flush for STDOUT
$| = 1;

# Parse command line options.
# Ensure a username and password are specified
GetOptions ('user=s' => \$opt_username, 'pass=s' => \$opt_password, 
	    'a' => \$opt_show_all, 'f:i' => \$opt_frequency);
unless ( $opt_username && $opt_password ) {
	print "Specify a username and password with --user and --pass.\n"; 
	exit 2;
}

$opt_frequency = 60 unless ($opt_frequency);
$retry_time = 4 * $opt_frequency;

## Sign in, create Net::Twitter object
$nt = Net::Twitter->new(
	traits 		=> [qw/API::REST WrapError/],
	username	=> $opt_username,
	password	=> $opt_password
);

## Download updates
print "Downloading Updates.\n";
print "Checking for updates every $opt_frequency seconds.\n\n";

# Get the ID of the most recent update, our starting point.  
my $statuses = get_updates();
if (@$statuses[0]) { $last_id = @$statuses[0]->{id};} 
else { die "Can't get ID of most recent update and so can't initialize."; }

# Show all updates if $opt_show_all is set. 
if ( $opt_show_all ) {
	print_statuses ($statuses);
}

# Main loop.  Check for status updates.
while ( 1 ) {
	# Get and print new statuses
	my $statuses = get_updates($last_id);
	$last_id = @$statuses[0]->{id} if @$statuses[0];
	print_statuses ($statuses);
	sleep($opt_frequency);	
}

# Returns an array of updates since $last_id, if specified.
sub get_updates {
	my ($last_id) = @_;
	my $statuses;
	my $mention_arg;
	
	if ( defined($last_id) ) { 
		$mention_arg = {since_id => $last_id};
	} else {
		$mention_arg = {};
	}
	
	while ( ! defined( $statuses = $nt->mentions( $mention_arg ) ) ) {
		print "Unable to connect to Twitter.  Trying again in " . 
		      $retry_time . " seconds.\n";
		sleep($retry_time);
	}

	return $statuses;
}

# Displays an entire array of status updates
sub print_statuses {
	my ($statuses) = @_;
	for my $status (@$statuses) { print_status ($status); }
}

# Displays one status update
sub print_status {
	my ($status) = @_;
	print "<$status->{user}{screen_name}> $status->{text}\n\n";
	print "$status->{created_at}\n";
	print "Posted from: $status->{source}\n";	
	print "\n\n\n\n\n";		# scroll the printer paper.
}