I've recently released WebService::ReutersConnect. It's a Perl modules that interfaces with the ReutersConnect's API in OO style. To demonstrate it and hopefully entertain you on this Friday, here's how to use it to watch the world go by in glorious ASCII and from the comfort of your command line. To put it shorter: The perfect Friday Time Waster.
The ingredients
To cook this recipe, you will need:
While you can install all of this with the CPAN command, I suggest you install most of their dependencies through your OS packaging system first.
If you're debian based, here are the debs to install: libmoose-perl libdatetime-perl libdatetime-format-iso8601-perl libtest-fatal-perl libxml-libxml-perl libwww-perl liblog-log4perl-perl liburi-perl liblibaa1-dev
Then just install WebService::ReutersConnect and Text::AAlib via your favourite channel.
The Recipe
The Recipe is pretty straight forward, it goes like this:
Preamble. We just load the required packages and also make sure that Log4perl is not going to complain about lack of initialisation:
#! /usr/bin/perl -w
use strict;
use warnings;
use WebService::ReutersConnect qw/:demo/;
use Log::Log4perl qw/:easy/;
use Text::AAlib;
use Imager;
Log::Log4perl->easy_init($WARN);
Building the $reuters object and querying the freshest image. Here we use the demo credentials and search for the latest picture:
my $reuters = WebService::ReutersConnect
->new({ username => REUTERS_DEMOUSER,
password => REUTERS_DEMOPASSWORD
});
my ( $item ) = $reuters
->fetch_search({ limit => 1,
media_types => [ 'P' ],
sort => 'date' });
Building the ASCII of the picture. We just download the preview URL and render it in ASCII using Text::AAlib:
## Load preview image
my $res = $reuters->user_agent->get($item->preview_url());
unless( $res->is_success() ){
die $res->status_line();
}
## Build and scale image
my $bin_image = $res->content();
my $img = Imager->new( data => $bin_image , type => 'jpeg' ) || die Imager->errstr();
$img = $img->convert( preset => 'grey' );
$img = $img->scaleX(scalefactor => 2); ## Tweak to your taste
$img = $img->scale(scalefactor => 0.5); ## Tweak to your taste
## Build ASCII Version
my ($width, $height) = ($img->getwidth, $img->getheight);
my $aa = Text::AAlib->new( width => $width, height => $height );
Rendering the whole thing on the console. The rendering parameters are OK for me, but you might have to play with them depending on your taste/colour scheme:
## Print ASCII Image
print $aa->render( dither => 0 ,
gamma => 1.83,
bright => 50,
contrast => 60,
color => 0
);
## And some info
print "\n ".$item->date_created().' : '.$item->headline()." \n";
print "\n ".$item->preview_url()."\n";
And that's it! Now you can put all of that together, or if you're lazy, you can just copy/paste the whole thing from this pastebin.
Feel free to experiment with the options and the code and tell us about your improvement in the comments!
Happy coding!
Jerome.
Bummer, can't seem to connect (with or without proxy). Getting 403 errors. Probably geo-limited? (I'm accessing from Indonesia.)
ReplyDeleteHi Steven, I'm not aware of any IP geo restriction, but as I'm not from Reuters, I cannot guarantee that. I'm accessing the API without any problem from the UK. Let's see which of the CPAN smoke tests break in a few days :)
DeleteDoes it give you any more detail error message?