code and design reflected in occasionally coherent words

Article Search:

October 8, 2009

OK part 2 of the thing about leveraging the Twitter API methods to create profiles that reflect the mood of the last status update. In the first part I wrote some quick code to programmatically change the profile color settings using the Twitter API's Update Profile capabilities. If you can't easily control it, it does no good.

In truth receiving input from tweets via the Twitter API is potentially one of the most useful tricks the API offers. The next big step will be to on how people will interface with the mood program. This is a bit more involved but not much. If you have a full-on client you can just add a button, checkbox, or some such input element. In the last entry the #:-) suggestion was hinted at for our server based solution. In general the # denotes a searchable keyword however I believe special characters such as : don't work as keywords.

To make the service easy to interface we will use hashes followed by simple smiles to make up our vocabulary of mood commands.

for instance:

  • #:) could trigger bright colors
  • #;) could trigger flirty colors
  • #:( could trigger gloomy colors
  • #:O , #=^ , #b===D , #:-s , #:\ , #...

Now all we need is a script that uses the Twitter API to watch for tweets which contain these commands and update the person's profile appropriately. Our script that monitors people's tweets for these mood commands could be croned or triggered by a pageload with an 'if timestamp % 5 = 0 include' type statement or other regular frequent event. If you cron it you may want to set it to run every 5 minutes or every 15, depending on your server load etc. Even witha few hundred frequent users it shouldn't amount to much, and if it does you can host the signups and database from a server and run the status watcher/profile updater/event handler scripts from any lampy home box with a half decent connection.

We have 3 possible ways of checking for user mood command events.

The first is to perform frequent searches for the mood commands using the Twitter API search methods. To do this you would invoke the searches and compare/query the database for the usernames in the results. You would also check the timestamp on the status update vs. the last mood update in the database.

The second option is to make a project profile and follow only the users of the project. In some other small projects I've done this and it works well. This is a great way to do things. Twitter even provides an RSS feed of the updates from the friends the account is following. I'm going to use this option for this example because even though I don't plan on more that a handful of users, it's more interesting to me. Besides I'm trying to keeep awake through jury selection. Grumble... Grumble... The only real drawback is you need to set up a script to request friends. This is easy but I'm not going to get into it just yet.

The third option is to check the individual user's status updates at regular intervals, check the timestamps, then parse/search the update for valid commands. This would be easier if you only had one or two accounts, to check but when you get into more accounts fewer requests and slightly more processing might be better.

So the code for checking the last tweet for a profile will use:

http://twitter.com/statuses/friends_timeline/profile_id.xml

and the parameters count, and page. Count and page will allow you to access up to 200 tweets or limit the number of tweets. Neither will be used at this point but are worth mentioning.

This bit of code will...

  1. authenticate and request the friends_timeline using cURL
  2. use regular expressions to match mood commands in the last update
  3. trigger the appropriate response if the command is found
  4. ?
  5. profit

This code won't...

  • connect to a database
  • remember the last update between checks
  • check for more than one command

but it will get things going in the right direction. Part 3 will get into connecting it to a database etc.

The first part of the code should be familiar, the Twitter API stuff. The friends timeline uses GET instead of POST, but still requires authentication for private friends. Beyond that is some basic parsing and searching.

$uname = 'mainaccount';
$pword = 'mainpass';
$count_no = "count=50";
$ftURL = "http://twitter.com/statuses/friends_timeline.xml?" . $count_no;
$pattern = "#\#:-?\)#";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "$ftURL");
curl_setopt($curl, CURLOPT_USERPWD, "$uname:$pword");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);


$resp = curl_exec($curl);
curl_close($curl);
$xml = simplexml_load_string($resp);
foreach($xml->children() as $status){
//    information you want to use or store
$cur_user_id = $status->user->id;
$cur_text = $status->text;
if(preg_match($pattern, $cur_text)){
//    things you want to execute if pattern matches
echo "#:-) found for ";
echo $cur_user_id;
}else{
//    if pattern doesn't match
echo "-no-";
}
}

You could use this bit of code to update or control a number of other services through twitter as well. It could be used in conjunction with news feed searches to pull info directly relating to friend's discussions, automatically pull their twitpics for a collage or whatever.

October 1, 2009

With the widespread usage of Twitter clients profiles often get overlooked. People who actually like to ogle each others profile pages may appreciate the option to make your profile capable of better inflecting the ever changing mood of the content. Twitter allows you to upload background images and change most of your profile’s color scheme. …continue reading…

July 11, 2009

Since I’m in the JSON mood I wanted to add a quick follow-up post to my Google AJAX image search post. This uses PHP’s cURL functions and JSON (JavaScript Object Notation) functions to get the top 20 twitter word trends for each hour the day. In case you haven’t noticed yet you need to get …continue reading…

...