Archive for the Category Programming

 
 

hashr for Google Chrome

Yesterday I’ve released hashr extension for Google Chrome. This is the first version, and it was just to try out the extensions platform for Google’s browser.

So basically this extension does the same as the Firefox addon, and the website, but in Chrome. Here’s a screenshot of it in action:

hashr chrome extension

You can find more screenshots, info and install it at the Extension Gallery page. Or you can just check out the source at GitHub.

Tinyscrobbler

On my spare time I’ve been learning the Ruby language. And Tinyscrobbler is the first result.

Tinyscrobbler is a lightweight Last.fm scrobbler library. It can be used to implement the track scrobbling feature in a ruby player app.

You can find the source (GitHub) here and install the ruby gem from Gemcutter.

If you opt to install from Gemcutter, you need to add it to your gem sources. Just type the following in your terminal (depending on your system you might need to execute these commands with sudo):

$ gem sources -a http://gemcutter.org

This will add gemcutter to your gem sources. Finally to install the gem just type:

$ gem install tinyscrobbler

Tinyscrobbler contains a helper class to read the audio files metadata, but for now it only supports .mp3 files. I’ll be working on extending the support to other file formats like .ogg, .mp4a and .wma.

New website and api for hashr

Finally I just got some time to update the hashr website.

I decided to give it some color and some usability improvements. The resulting hash now shows up in a readonly text box so that when you choose long hash algorithms like whirlpool the hash doesn’t break the website design.

Also related with this change it’s the copy button. I know, I hate flash too, but unfortunately due to security issues like clipboard-jacking, there’s no cross-browser compatible way to place text in user’s clipboard unless you use flash and the user clicks that.

Anyway, the button is there to make it easy to copy the hash. But you can still do it the usual way.

Apart from the interface changes, hashr now also has a stats page. This page for now only shows a chart of the top 10 most used hashing algorithms in the website and api and the total generated hashes. But it will have more charts in the future.

And finally I rewrote the api and made it public with some documentation. It now supports xml, json and text responses.

The Firefox extension is still using the old api, but soon I’ll upload to Mozilla Addons a new version using the new one.

Codebits project – Got Gigs?

Got Gigs?

In the last 3 days I’ve been at Sapo Codebits. It was the first time I attended the event and I can only say that I’ll do my best to be at the next one. Three days of absolute awesomeness.

The programming contest was sort of an endurance test. Thirty two hours awake, most of them coding like mad (with some PES2009, lots of pizza and redbull in between).

And the final output of the contest was an iPhone app called Got Giggs?. The project consists of not only an iPhone app, but also a web app that fetches concerts from a given country/city from the Last.fm api and a public api that serves them from our database in various formats including JSON, XML etc.

The iPhone app was built using PhoneGap. This open source tool let’s you build apps for mobile devices like iPhone, Blackberry, Android, etc. using HTML and Javascript. The concept is nice, but the lack of decent documentation and examples makes it a harder challenge than it seems at first hand. But with some persistence and lack of sleep we came up with the app almost complete:

Featured | Soon | All Concerts | My Bands | Settings

The presentation didn’t ran quite well. First I got nervous when I opened the macbook pro on stage and found out that I only had less than 20 minutes of battery. This laptop normally shuts down to sleep on the 10 minute mark.

I had 90 seconds to present the project, but it seemed more like 10 seconds to me. I didn’t even had the chance to explain half of the app’s features. That’s for being a noob presenting projects in public. But well, lesson learned.

Anyway, this project won’t stop here. We will continue developing it to reach a final product. The name will be different and will target only musical events in Portugal. So if you’re interested in this web app/ iPhone app, check this blog for more updates.

Overall this experience was very enlightening and definitely worth repeating. Next year will be better ;)

Using git with dropbox

This is just another way to give some use to your dropbox account. Although I use git in this article, you can pretty much do it with any other SCM tool.

First of all you obviously need a dropbox account. If you don’t have one already, go register for one and get 2Gb of free online storage.

Now that you have an account, create a folder on your dropbox and on the web interface and share the folder with some friends if you wish to do so.

All is ready now to start creating the repo. For this post I’m assuming you already have a local git repository of a project called my_killer_app and that you are working on a unix based operating system like OS X or Linux.

Open up a terminal, and change directory to your project folder:

cd ~/Sites/my_killer_app

The next step is to clone your existing local repo into the shared dropbox folder:

git clone --bare . ~/Dropbox/shared_folder/my_killer_app.git

The --bare option tells git to not include the project files. Only those files needed to track the versioning are cloned (mainly those present in the .git/ folder).

Now you have sort of a remote repository. Although it’s on your machine, it’s remote to everyone else sharing the folder. But to make things work we need to add this “remote” location and give it an alias:

git remote add my_killer_app ~/Dropbox/shared_folder/my_killer_app.git

There! It’s done. Now you can push your changes to the repository. And pull the changes on another machine with your dropbox account. Also people sharing the folder will be able to do the same.

Just for the sake of completeness, here’s how you would make changes and commit them to the “remote” server:

You made changes to the code, now it’s time to add and commit:

git commit -a -m "another commit example"

Nice! Now let push them to the “remote” server:

git push my_killer_app master

Piece of cake.

And here’s how a different user sharing the folder would do to collaborate on your project:

Clone the repository:

git clone ~/Dropbox/shared_folder/my_killer_app.git

Add the alias to remote repository:

git remote add my_killer_app ~/Dropbox/shared_folder/my_killer_app.git

And that’s it! Now it’s pull, commit, push. If you need more info on git usage you can check this manual.

Playing with Twitter Stream API and Text-to-Speech

There’s a new Twitter API in town: The Twitter Stream API. But before you get all excited building a new twitter stream client, it’s important for you to know that this is in alpha version for now.

Since I wanted to test this new API and didn’t knew how to test it, I decided to do something silly. I wandered how it would be like to have someone read me in realtime all the tweets about a given keyword. This is the part where the Mac OS X built-in text-to-speech comes in.

The result was a PHP script that called the OS X "say" command to read the realtime Twitter stream.

The code is very simple:

<?php
$username = 'TWITTER_USER';
$password = 'TWITTER_PASSWORD';
$keyword = 'twitter';

$fp = fopen("http://$username:$password@stream.twitter.com/1/statuses/filter.json?track=$keyword", 'r');

while($data = fgets($fp))
{
	$res = json_decode($data, true);

	$user = $res['user']['screen_name'];
	$tweet = $res['text'];

	echo ("$user says: $tweet");
	exec (escapeshellcmd("say $user says: $tweet"));
}

fclose($fp);
?>

Note the escapeshellcmd function that saves you from being owned in case someone “accidently” tweets something like ";rm -rf ~/".

You can just run this script in terminal, but first change the username and password to match your twitter account, and keyword to something you want to search.

Now you can annoy everyone at your workplace, with a voice reading out loud all your (not so) interesting tweets.

Updating hashr to work on Firefox 3.5

I got an email today from a hashr user complaining that the add-on was not working on Firefox 3.5. I had noticed it a few days ago, but completely forgot about it.

I’m currently updating the add-on to 3.5, improving the user interface and maybe add some new feature. But for now if you wish to use the extension with Firefox 3.5 you can use a little hack that works with no problems.

Just locate your Firefox profile folder, and then open the extensions folder inside it. There will be one folder with the word “hashr” in the name. Open it an edit the file install.rdf.

Inside the file locate the following lines:

<em:minVersion>1.5</em:minVersion>
<em:maxVersion>3.0.*</em:maxVersion>

And change it to:

<em:minVersion>1.5</em:minVersion>
<em:maxVersion>3.5.*</em:maxVersion>

Save the file and restart Firefox. It should now be working perfectly. If not, check the add-ons manager to see if it’s disabled.

Edit: Mozilla has finally approved the new version of hashr, so this hack is no longer needed. You can find the addon here: https://addons.mozilla.org/en-US/firefox/addon/8539.

TwitterPHP 0.5 released and lessons learned…

I’ve released this Friday a new version of the TwitterPHP library. In this version I’ve done some code optimization, removed some files and changed to Doxygen for class documentation.

First, all methods were returning a SimpleXML object, which is a mistake in terms of flexibility. I’ve changed them all to just return a XML string. Now its up to the user (programmer) to choose the way to access/parse the XML.

Second, TwitterPHP is a class, you can instantiate it several times, for let’s say, accessing multiple accounts. At least you should be able to do that, but since I was reading the Twitter username and password from a configuration file (naive, I know), multiple instances could only use the same account. Now the problem is solved by simply adding the username and password as arguments of the constructor method and removing the configuration file.

Third and last, I switched from PHPDocumentor to Doxygen. This was mainly because I found out that Doxygen is much more flexible, I can use it with any other project with any other language. And the resulting documentation is more fast and clean than PHPDocumentor.

So you can take a look at the new version of TwitterPHP here and the documentation generated by Doxygen here.

In future releases I’m planning to add OAuth support and implement more Twitter API actions.

Object Oriented Javascript

Recently, I’ve been playing with Object Oriented Javascript. This language, besides it’s chaotic nature, can be very powerful when used with some best practices (humm, that’s funny, I’d swear I’ve heard this before).

One of the most interesting things about Javascript OO is the way to define a Class. Actually there is no keyword “class” at all. So here’s an example on how to create a class Book in Javascript:

/**this will be the constructor method,
that automatically creates the class**/

function Book (isbn, title, author) {
    this.isbn = isbn;
    this.title = title;
    this.author = author;
}

/** And now the getters **/

Book.prototype.getISBN = function () {
    return this.isbn;
}

Book.prototype.getTitle = function () {
    return this.title;
}

Book.prototype.getAuthor = function () {
    return this.author;
}

If your class has attributes that are not passed as constructor arguments, you can declare the attribute just like we did for the methods:

Book.prototype.numTimesRead = 5;

//a setter
Book.prototype.setNumTimeRead = function (n) {
    this.numTimesRead = n;
}

Now if we wish to instantiate our class, it’s identical to most OO languages:

var mybook = new Book("123", "Dive into Python", "Mark Pilgrim");

//I tried to output this the ugliest way possible :P
document.getElementsByTagName("body").innerHTML = "Title: "+mybook.getTitle();

This will (pwn your body element) print the Book title, you can add any other info as you wish.

You may find more on object oriented programming with Javascript at the Mozilla’s Developer site.

Extreme Programming

Some time ago, I’ve written a post talking about Agile Software Development methodologies, where I mentioned Extreme Programming (aka XP).

I was thinking about writing a post on Extreme Programming, but I’ve just found a very good video of a lecture on the subject by Richard Buckland, that explains it the best way possible and with some humor. So here’s the video:

This is a highly recommended video for those that want to learn about Extreme Programming and Unit Tests.

Video Link: http://youtube.com/watch?v=XP4o0ArkP4s