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.

#ttytterkf
$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 ); 


  return; 


}

No comments: