Sunday, 18 March 2012

Hourly timestamp in your twitter stream

This is such a silly project that I'm only going to post the code here as a curio. I have uploaded the code here in order to keep all it in one place ;-).

I liked the idea of an hourly time code in my twitter stream so started following @big_ben_clock which helpfully BONGs on the hour. This is OK if you are in the same time zone as Big Ben and can quickly enumerate the BONGS at, say, 9 O'Clock but useless otherwise.

I thought it would be worth a couple of minutes of my time to convert the tweet text to a localtime time of the tweets generated by @big_ben_clock making the 'service' more universal and get rid of the BONGS - which wear thin after a while.

@RealIvanSanchez has pointed out that there's an account @coucou_suisse that performs a similar service. So, take your pick.

Here's the code for the TTYtter extension: (needs Date::Manip)

use Date::Manip;

# Simply follow @big_ben_clock or @coucou_suisse and this will replace the tweet with a
# localtime conversion of the tweet timestamp.
# @pr4wn

sub bb_time_convert {
  my $date = shift;
  my $s;

  $s = substr( $date, 8, 5 ); # hh:mm
  return( $s );


$handle = sub {

my $ref = shift;

my $ESC = pack("C", 27);
my $BOLDON = ($ansi) ? "${ESC}[1;31m" : '';
my $BOLDOFF = ($ansi) ? "${ESC}[0m" : '';

if ($ref->{user}->{screen_name} eq 'big_ben_clock' ||
$ref->{user}->{screen_name} eq 'coucou_suisse' ) {
  my $created_at = ParseDate($ref->{'created_at'});
  $ref->{text} = $BOLDON . bb_time_convert($created_at) . $BOLDOFF;


&defaulthandle ( $ref );


Here's both accounts showing the same localtime despite being in different time zones.

Monday, 5 March 2012

Filtering your twitter feed with TTYtter

It's no huge deal, but I have noticed that there are certain categories of tweet that I have no interest in. Having written an extension for TTYtter previously, I thought that I'd knock up an extension to filter out tweets using regexp filters.

Of course, I took the precaution of RTFMing the TTYtter documentation before taking on the task, and discovered that the feature already exists, namely, the filter= directive in the .ttytterrc file. The lesson here is RTFM before re-inventing the wheel.

So, for the time being, I'm experimenting with these filters:

filter=(/Please RT/i|/pl[s|z] RT/i | /[@|#]TheWantApp/i | /#getglue/i | /#nowplaying/i | /[#|@]klout/i)

I'll see how it works out over the next few days and see if it behaves.

Update: The filter above works as advertised which is handy to know. I may consider doing a plugin to extend the capability with lists of rules and being able to use regexps on a wider range of data.

Off the top of my head, a rules file like:

#rule 1
( ( $ref->{user}->{screen_name} eq 'pr4wn' ) && ( $ref->{text} =~ /\blinux\b/i ) )
#rule 2
( $ref->{text} =~ /#dullhashtag/i )
#rule n

Then go through the rules and omit any matches.

Something for a rainy day, perhaps and not particularly urgent - I'm unsure how much it would be used ;-)

And here we have it. A proof of concept that I ran during Question Time (so I could predict active tweeters and hash tags). Apologies to @DIMBLEBOT ;-)

I've dediced to post code similar to below here. Simply download the ttytterkf tar.gz file and off you go.

$handle = sub {

  my $ref = shift; 

  my @filter_rules = ( 
  #Rules can be any legal true/false perl conditon, so fill your boots. 
  '( ( ($ref->{user}->{screen_name} =~ /^pr4wn$/i ) && ($ref->{text} =~ /\blinux\b/i ) )',
   #Some test rules to test during Question Time. 
  '( $ref->{user}->{screen_name} =~ /^DIMBLEBOT$/i )', 
  '( $ref->{text} =~ /\bdimbledance/i )',
  '( $ref->{text} =~ /#bbcqt/i )' 

  foreach my $rule ( @filter_rules ) { 

    $i = eval $rule; 

    if ( $i == 1 ) { return; } 


  &defaulthandle( $ref );