<< Twitter4J 1.1.0 released | Home | Twitter4J 1.1.1 released >>

Decoding the Mars Phoenix's last Tweet using Twitter4J on IntelliJ IDEA 8

Mars Phoenix had been Tweeting about everything she discovered on the red planet. On November 11th, she posted her last tweet in a cryptic binary form which ends with a heart emoticon. How romantic it is!

Some of those people who follow her tweets might be able to understand it natively, but my decimal based brain couldn't. So I decided to write down a tiny code that decodes it using the fresh, and yummy IntelliJ IDEA 8.

Here's the steps I followed.

1. Create a project
From the File menu, you can create a mavenized project very easily.
[File > New Project...]
[Create project from scratch > Next]

Project wizard

Name: marsphoenix
Select type: Maven Module

Projec wizard-2

Press [Next] to proceed.

You can choose an archetype at this point if you like.

Press [Finish] to finish the project wizard.

A mavenized project was created.

a typical mavenized empty project

2. Create a class
right-click marsphoenix/src/main/java node and choose [New > Class]

I named it DecodeLastTweet.

entering the class name

3. Using Twitter4J
I feel it's not interesting if I hard code the message as a String literal. So why don't we grab it using Twitter4J?

Twitter4J is super easy to use. You can just pass your userid and password to create a Twitter instance as follows:

    public static void main(String[] args){
Twitter twitter = new Twitter("userid","password");

Oops, Twitter doesn't resolve.

red alarming font

Of course the reason why Twitter cannot be resolved is because the class hasn't been imported and it's not even declared in the dependencies in the pom.xml. In such a case, all you have to do is just moving the cursor to the appealing part. Then you'll see a bulb, which indicates that IntelliJ is offering you to do something for you.

an eye-catching bulb

Click the bulb to see the options:

[IDEA] What do you want me to do?

The best choice should be "Add Maven Dependency".

Dependency selection dialog

Adding the dependency to the latest version of Twitter4J is as simple as just selecting it. Then IDEA will automagically add dependency declaration to the pom.xml, and import statement for twitter4j.Twitter class.

automatically managed pom.xml and import statement

Pretty smart.
Let's move on to next.

4. Getting the status
The status id of her last tweet is 999383469.
So the code to get it should be like this:
    Status status = twitter.show(999383469);

And of course, IDEA is smart enough to add the import statement for twitter4j.Status automatically. Since show method throws TwitterException, you are either to choose rethrowing the Exception, or enclose the method with a try - catch block.

To catch, or not to catch, that is the problem.

This isn't a serious code and I chose the latter one - "Add Exception(s) to Method Signature&qout; this time.

throws signature added

5. Finish the code!
Lastly, I added following steps:
- split the tweet with space
- decode each binary strings to int type
- cast them to char type
- print them

The complete code looks like this:
public class DecodeLastTweet {
public static void main(String[] args) throws TwitterException {
Twitter twitter = new Twitter("yourid", "password");
Status status = twitter.show(999383469);
String text = status.getText();
String[] split = text.split(" ");
for (String token : split) {
try {
System.out.print((char) Integer.parseInt(token, 2));
} catch (NumberFormatException ignore) {}

the complete code

6. Run it
You can run the class by just pressing the "Play" button.

IDEA is showing the decoded message

- Conclusion
I intentionally hidden the decoded message.
You can download the BSD licensed code, build it, and run it on your own box if you want.

I also hope you glanced how productive the latest IntelliJ IDEA is. As you know, IDEA is a commercial product.
It's not free, but life is short. Don't hesitate to invest on your productivity.
I believe that very soon after you bought the product, you'll reach the break-even point because of the super-productiveness.

just FYI: Jetbrains is also offering a free license for open source developers.

- Twitter4J 1.1.0 released - Samuraism
- decoding the Mars Phoenix's last Tweet using Python and python-twitter
- JetBrains IntelliJ IDEA Blog » Blog Archive » Meet JetBrains’ Release of the Year: IntelliJ IDEA 8
- IntelliJ IDEA, Why I like | Appliweb

Add a comment Send a TrackBack