Friday, January 04, 2013

Now You Know What I Did Last Weekend: @redditJava




I am a regular reader of Hacker news: It’s a great place to discover interesting stuffs. But I don’t go checking http://news.ycombinator.com every now and then, neither do I subscribe to their RSS feed. I get most of the stuffs I read on Hacker news via twitter. There are Hacker news’ bots that I follow on twitter which provides the utility of having *hot* articles tweeted once they have attained a certain number of points or comments. Namely: @newsyc150, @newsyc100, @newsyc50 and @newsyc20.

So recently, when I decided to pay a little more attention to Reddit, the first thing I looked for were twitter bots I can follow that would feed me with interesting contents from Reddit. One nice thing about Reddit, unlike Hacker news, is the fact that Reddit have sub categories or channels which are called subreddits which cater to specific topics. You got subreddits for Politics, Gaming, Movies, Music…the list is almost endless. So I basically was on the lookout for twitter bots for subreddits I may be interested in.

But my searched returned null.

Since I could not find one, I decided to make one for myself. And I decided to make one for the Java subreddits since I recently started playing with the language again. (By the way having the bot track other subreddits is simply a matter of configuration).

The idea is simple enough: get stuffs from Reddit, have a way of getting only the interesting ones based on the number of points and comments, and then tweet only those. Also, make sure you also don’t have the same thing tweeted twice. The content is there, the APIs are there, so I set about getting this done within a weekend.

For this, I used Node.js. Reason? Well, I spend a lot of time with Javascript now, notable because I work with the language at work, building the world smarter customer service app, which enables business to keep more happy customer: Casengo; and since Javascript is very much capable, why not?

It was fun hacking this bot together the last weekend as I also took time to play around with stuffs: For storing information to prevent double tweets, mongoDB was used; this allowed me to get pretty conversant with the idea of document based DB and how it works in mongoDB. I also stumbled on node-supervisor. A handy Node Module which prevented the need to manually stop and start the server on every change in the code. Use node-supervisor to run a node script, it watches for any changes in your file; on change, it automatically reloads the script. This definitely made the development flow smoother.

I also checked up on writing Node Modules while following generally accepted guidelines especially if you want your module to be compatible npm. Also learnt one or two things about setting up mongoDB on AWS’s EC2.

Apart from the obvious Twitter and Reddit API’s I equally got to play around with bit.ly and goo.gl API’s.

And finally once deployed, I used forever to keep the script running. Forever ensures that a node process continuously runs and automatically restarts itself when it exits unexpectedly.

Javascript is definitely a pleasant language to work with. But you could trip if you fail to wrap your head around some of its unique and basic characteristics. First all, it is a Prototyped based OOP language (not class based) it is also event driven and easily supports asynchronous programming. This alone could result in some unexpected behaviour if you do not put these unique characteristics in mind. An example, where one can easily be tripped would be handling asynchronous API calls inside a for loop. (More on this in coming post) You also have things like variable hoisting, scoping, closures, the abstruse this etc., all which may make the language appear weird at first approach.

It was a weekend well spent and would spend some more free time modifying things and perhaps adding more subreddits that catches my fancy. The bot is already up and running by the way and you can follow on twitter here www.twitter.com/redditjava and the code is hosted on github here https://github.com/dadepo/redditTwitterBot.git