Parsing a Tweet

I’ve been using the Twitter stream code, and it works really well. Now, I would like to parse the entire returned tweet for other text. It looks like I can use split() like this:
local tweetParsed = split(tweet.text, " ");
and get an array that I could check. Are there any better ways of doing this?

Is it possible to use the Squirrel regexp class?

This seems to work:
local tweetParsed = split(tweet.text, " "); foreach(string in tweetParsed) { if (string == "#Red"){ device.send("rgb", color_red); }

If you tweet using #Neopixel and a color, like: #Red #Green #Blue you’ll change the color of my Neopixel ring.

Squirrel regexp does work, yes, at least on the agent. Can’t remember if it’s in the device too.

Hashtags are also returned as entities. Here’s some code you can use to loop through and log all of the hashtags in a tweet:

if ("entities" in tweet && "hashtags" in tweet.entities) { foreach(tag in tweet.entities.hashtags) { // do something interesting with hashtags server.log(tag.text); } }

Nice! Are there any other entities? How are they defined?

You also get media things (images, videos, links, etc) and all kinds of other goodies.

If you search Twitter’s API documentation, you should be able to get a complete list of what can be returned from searches/streams/etc and how it looks.

Or pass your tweet object into the logTable function from this post

Oh… duh. That worked really well. I’m going to repost your code with one correction. The compiler yelled about not declaring x.

`function logTable(t, i = 0) {
local indentString = “”;
for(local x = 0; x < i; x++) indentString += “.”;

foreach(k, v in t) {
    if (typeof(v) == "table" || typeof(v) == "array") {
        local par = "[]";
        if (typeof(v) == "table") par = "{}";
        
        server.log(indentString + k + ": " + par[0].tochar());
        logTable(v, i+4);
        server.log(par[1].tochar());
    } 
    else { 
        server.log(indentString + k + ": " + v);
    }
}

}

http.onerequest(function(req, resp) {
try {
logTable(req);
resp.send(200, “OK”);
} catch (ex) {
resp.send(500, "Error: " + ex);
}
}`

Thanks!