Day 21 – Modifying Facebook Stream With Kynetx

At a recent family dinner on Christmas Eve, some of the family was joking about how they never understand my status updates on Twitter and Facebook, especially when it’s about programming or Kynetx. This discussion gave me the idea to create a Kynetx app for them that they could run that would modify my status updates to something other than what I had actually said.

ruleset a60x512 {
  meta {
    name "MikeGrace-status-update-translator"
    description <<
      MikeGrace-status-update-translator
    >>
    author "Mike Grace"
    logging on
  }

  global {
    datasource insult:HTML <- "http://www.pangloss.com/seidel/Shaker/index.html?" cachable for 1 second;
  }
  rule find_status_updates_by_mike_grace {
    select when pageview ".*"
    {
      notify("Starting to look for status upates by Mike Grace","");
      emit <|

        // get app object to raise web events
        app = KOBJ.get_application("a60x512");

        // function that finds FB status updates by Mike Grace
        function findMikeGrace() {

          // loop through each stream item on the page that hasn't been processed already by the app
          $K("li[id^=stream_story]:not(li[kfbt])").each(function() {
            var currentStreamItem = this;
            // grab the current stream item posters name
            var name = $K(currentStreamItem).find(".actorName").text();

            // mark the stream item as being processed to reduce future processing times
            $K(currentStreamItem).attr("kfbt","y");

            // is the stream item by the perpetrator?
            if (name == "Michael Grace") {

              // strikethrough the original update
              $K(currentStreamItem).find(".messageBody").wrap("<strike />");

              // get selector to return translation of status update
              var returnSelector = $K(currentStreamItem).attr("id");
              returnSelector = "li#"+returnSelector+" .messageBody";

              // raise web event to get translation for non geeks
              app.raise_event("get_insult", {"returnSelector":returnSelector});

            } // end of checking name

          }); // end of looping through unprocessed stream items

          // call myself again later to process new items on the page
          setTimeout(function() {
            findMikeGrace();
          }, 9000);
        }

        // start the process of finding the perpetrator
        findMikeGrace();
      |>;
    }
  }

  rule get_insult {
    select when web get_insult
    pre {
      selector = event:param("returnSelector");
      insulter = datasource:insult("#{selector}");
      foundInsult = insulter.query("font");
      singleInsult = foundInsult[0];
    }
    {
      emit <|
        console.log(singleInsult);
        $K(selector).parent().after("<br/>"+singleInsult);
      |>;
    }
  }
}
  • 12 shakespearian insults datasource
  • 14-62 ‘find_status_updates_by_mike_grace’ puts JavaScript on the page to find the status update and raise an event to get the¬†Shakespearian¬†insults
  • 17 show user app is working
  • 21 get app object to use later to raise web event to get insult
  • 24 wrap JavaScript in function to call multiple times with a setTimeout to look for new content on the page
  • 27 select stream story items that haven’t already been processed by the app and iterate through each of them
  • 65 selects when the JavaScript raises the web event of ‘get_insult’
  • 67 get the return jQuery selector that was passed from the web event so we know where to put the insult
  • 68 query the datasource which will get the entire HTML page set in the global block – I put the selector in as the query parameter to try and get better random insults but it didn’t help
  • 69 using the query function to filter through the returned HTML page and get just the insult
  • 70 the query function always returns an array so this grabs the first one and returns it
  • 74 for my testing so I could see the insult being returned
  • 75 append the insult to the selector element – I was originally putting it after the selector but was part of the strikethrough so instead of changing the selector that was passed through the web event I used jQuery to go up one parent element and then put the content after

App run on facebook.com with bookmarklet

Video demo of app running on facebook.com

You’ll have to friend me on facebook if you want to try out the demo app. You can friend me and let me know that you are trying out the app if you like => http://www.facebook.com/mikegrace

Get the bookmarklet to try it out yourself!

Gratuitous day 21 Grace face

About these ads
This entry was posted in Kynetx. Bookmark the permalink.

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