TAG | Open Source
Download the source code mentioned in this blog post.
I’m a hopeless code junkie. I love to write code. Most people do one thing for work and then another for their hobby. My girlfriend for instance works as an IT / Systems Engineer and her other thing is photography.
My other thing is writing more code. I never did this with any of my previous jobs (save dish washer… I did wash dishes when I was at home but I wasn’t trying out new, cooler ways to wash them).
… So where was I? Oh, right code junkie. So, I really like to write code and what I’ve decided to do is start a new small project each week and try to use some new chunk of .Net or a new library and I’ll post the end results of my efforts here for you all.
All of these projects will be offered under the CodeImpossible “works on my machine” code quality guarantee. But I’ll never post something that flat-out doesn’t work.
Sound good? Cool, let’s kick it off – as the first entry into this space I’d like to present TweetCommander.
TweetCommander is a small .Net v3.5 console application that lies in wait, watching a twitter account for any new Direct Messages from another user (we’ll call this person the “owner”).
When a direct message from the owner is found, TweetCommander will check to see if it contains certain text, and depending on that text, will perform a series of actions on the machine it is running on.
TweetCommander will support three commands: “current_screenshot”, “exit”, and “set_interval”.
- Sending “current_screenshot” will tell TweetCommander to take a screen capture of the Windows desktop, upload it to TwitPic, and then send the url for that image to the owner user in a Direct Message.
- Sending “exit” will cause TweetCommander to exit
- The “set_interval” is followed by a number that represents the number of seconds TweetCommander should wait between requests for new direct messages from twitter. This is more to avoid the API limit than anything else.
Settings
TweetCommander will need to store the Twitter ID for the last successfully processed Direct Message somewhere so we aren’t constantly processing the same commands over and over again. The end user won’t need to be aware of this value but it’s worth mentioning anyway.
We’ll also need to store the wait interval so we don’t lose this information if we need to restart TweetCommander for whatever reason.
Okay, so here is the settings file I have so far:

Working with Twitter
All right so now we need to be able to interact with Twitter. Now, I don’t want to write my own API library so I’ll go out and get the latest copy of TweetSharp which will give me a nice, readable interface to twitter’s API. After getting this built I’ll be able to get the most recent direct messages using the following code:
var directMessages = FluentTwitter.CreateRequest()
.AuthenticateAs(
TWITTERACCOUNT_USERNAME,
TWITTERACCOUNT_PASSWORD)
.DirectMessages()
.Received()
.Since(Properties
.Settings
.Default
.LastProcessedCommandID)
.AsJson()
.Request()
.AsDirectMessages();
Thats pretty freakin’ sweet I must say. Tweet# really takes the brain work out of working with twitter and there is no way to look at that code and not understand what it is doing immediately. Epic win.
To get this running on your machine, just grab the source from my box.net folder, and change these values at the top of the Program.cs file:
// this is our "owner account" we will only act upon direct messages
// send from this user
private static string TWITTEROWNER = "codeimpossible";
// this is our listener accounts username
private static string TWITTERACCOUNT_USERNAME = "someuser";
// this is our listener accounts password
private static string TWITTERACCOUNT_PASSWORD = "somepassword";
Note: The solution file for this contains a reference to a compiled version of the Tweet# library that contains a quick patch I made for an issue that affects uploading an image to Twitpic. However this issue has been fixed officially in the most recent source.
So, yeah I originally started writing this article back in september when I was about mid-way through JSOQ. Some things have changed since then and I’ve tried to keep the post up-to-date. Enjoy!
Also, another similar library has come out recently: JSINQ which is a really feature-rich LINQ to Object implementation in Javascript. Really bad-ass stuff.
The Elevator Pitch:
JSOQ is an open-source pet project of mine that lets you easily access the data that you need from large collections in JavaScript. It was developed over a two week period in September of 2008 and is currently at version 1.1.
Everyone probably just shrugged and said “so??” Well let me show you.
Lets assume that you have the following JSON string from another web application like, say twitter search results for example:
{"results":[
{
"text":"Just another day at the office",
"to_user_id":null,
"from_user":"codeimpossible",
"id":1194587890,
"from_user_id":1316793,
"iso_language_code":"en",
"profile_image_url":"www.path.to.image.us",
"created_at":"Tue, 10 Feb 2009 05:47:33 +0000"
},
{
"text":"@spolsky thats easy! http:\/\/tinyurl.com\/2z42bs",
"to_user_id":1357501,
"to_user":"spolsky",
"from_user":"codeimpossible",
"id":1194582705,
"from_user_id":1316793,
"iso_language_code":"en",
"profile_image_url":"www.path.to.image.us",
"created_at":"Tue, 10 Feb 2009 05:44:51 +0000"
}
]}
In the sample above we only have two results but JSOQ has been tested to work with collections of tens of thousands of items.
The JSON object we have here is perfectly fine if we watned to show a whole bunch of data to the end user, but what if I only wanted to show the search results after a certain date? Or what if I only wanted the text from each message that was typed to this “spolsky” character?
To accomplish this in “straight” javascript I’d be looking at writing a lot of loops and if/else code and then I would still have all these extra properties that I don’t even need.
JSOQ allows me to query a javascript object or collection of objects using a specified criteria and return only the properties/methods I want.
So lets go with the second option. Let’s get all the .text members from all the messages that were sent to the “spolsky” user.
var search_results = eval(our_json_result);
var result = null;
with(jsoq) {
result = From(search_results.results)
.query('text')
.where(function(i) {
return i.to_user === "spolsky";
});
}
And now our result object is filled with a bunch of other objects that would look something like:
{
text: "@spolsky thats easy! http:\/\/tinyurl.com\/2z42bs"
}
Pretty simple eh? For more information on JSOQ please check back here in the near future or contribute to the google code repository.
What a great way to end the week. I logged in to my dashboard tonight to prep a post for tomorrow and found that my hosted wp.com blog has now been upgraded to WordPress 2.5. Excellent! Right now I am really digging the new look of the admin section (full-screen editing ftw!) and I can’t wait to see how the blog performs with the new performance enhancements.
Expect a lot more posts about WP 2.5 over the next couple of days.
I am so excited about this release of wordpress. Whats new? load-time improvements, built-in Gravatar and gallery support (YES!!!), built-in plugin upgrade feature oh, yeah and the entirely new admin interface and customizable dashboard screen. What am I most excited about? Well aside from the already mentioned bitchin’ admin interface I’m really excited about the code improvements (more detailed comments and database optimizations FTW).
When can wp.com users (like yours truly) expect some 2.5 action? Within the next week or so you should see the new interface in your admin consoles.
Okay, so the blog looks like it is back up and running. The front-end has been up for about 2-3 hours now but the admin section has been a royal pain in the ass to get back to a livable state. What happened was that earlier today I noticed that several of my toolbar items had disappeared from my edit page (most notably the “More” page break button) and I could no longer toggle between Visual and Code view.
After researching the problem it looked like I had my work cut out for me. To make a long story short I ended up doing all of the following steps:
- Upgraded my wordpress installation
- down graded my wordpress installation
- added / removed a new MCE
- added / removed another MCE
- edited about 5 -6 wordpress php files
- reverted those 5 – 6 wordpress php files
- upgraded word press again
Until, finally, I gave up and implemented the ugliest hack ever (I am not sure why wordpress seemingly ‘forgot’ my toolbar settings) which I discovered over at this site (last post to the thread). If anyone has figured out how to fix this (in such a way that it doesn’t feel so klooged together) then please speak up and let me know.

