Day 35 – Find And Replace Words In Twitter Tweet Stream

I recently built a Kynetx app to remove foul language from my Twitter tweet stream and thought it was interesting enough to share. The code is mostly JavaScript but there are a few concepts that are really good to know when building a Kynetx app that uses a lot of JavaScript. You can find the original app on my blog over at http://geek.michaelgrace.org/2011/01/foul-fowl-control-kynetx-app-for-twitter-com/

ruleset a60x551 {
  meta {
    name "Foul Fowl Control"
    description <<
      hides foul language in twitter stream
    >>
    author "Mike Grace"
    logging on
  }

  dispatch {
    domain "twitter.com"
  }

  rule find_dem_foul_fowls {
    select when pageview ".*"
    {
      emit <|
        // add repeat function to String object
        // http://stackoverflow.com/questions/4549894/how-can-i-repeat-strings-in-javascript
        String.prototype.repeat = function(times) {
           return (new Array(times + 1)).join(this);
        };

        // list of words to find and replace
        // var foulWords = ["it","is","the","on","a","i","of","my","me","for","in","up","and","to","be","that"];

        // build regex to find and replace foul words
        KOBJ.a60x550.theReplacer = new RegExp('\\b('+foulWords.join('|')+')\\b','gi');

        // attatch function to global KOBJ object so it's callable outside of closure
        KOBJ.a60x550.findFoulFowls = function() {

          // process tweets not marked as processed
          $(".stream-item:not(.defouled)").each(function(index, element) {

            // mark as processed and cache object
            var $current = $K(element).addClass("defouled");

            // get text of tweet
            var $tweet = $current.find(".tweet-text");
            var tweet = $tweet.text();

            // clean it up!
            var needsCleaning = false;
            var cleanTweet = tweet.replace(KOBJ.a60x550.theReplacer, function(match) {
              needsCleaning = true;
              return "*".repeat(match.length);
            });

            // only write back to the DOM if the tweet was changed because DOM access is expen$ive!
            if (needsCleaning) {
              $tweet.text(cleanTweet);
            };

          }); // end of each

          setTimeout(function() {
            KOBJ.a60x550.findFoulFowls();
          }, 1000);
        };

        KOBJ.a60x550.findFoulFowls();
      |>;
    }
  }
}

The code is fairly well commented so I’ll just point out one thing. Because I am using a setTimout function to recursively call the function that looks for new tweets, I needed to attach the function to the global KOBJ object. This is because the emitted JavaScript gets executed inside a closure and won’t be accessible from outside the closure.

Advertisements
This entry was posted in Kynetx. Bookmark the permalink.

One Response to Day 35 – Find And Replace Words In Twitter Tweet Stream

  1. Pingback: Foul Fowl Control Kynetx App For Twitter.com – Michael Grace

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s