понеділок, 16 січня 2012 р.

How much time you wasted in vkontakte.ru

Social network vk.com has beautiful api for different types of applications such as iframe, standalone and flash applications. Here you can find pretty documentation for it. But almost each method requires user authentication in vk.com, except several methods.

Here I came across with getProfiles() method. So let's describe one and create simple application for information acquisition via this method.

First of all we should look at getProfiles() method and build correct request to vk.com server.So there are several ways to send request to vkontakte api, but in this post we use api for standalone applications.

Thus we should send request to https://api.vkontakte.ru/ with ruquied parametrs.

In my opinion the most interesting method in this api is method that doesn't require user authentification. Everyoune can send such request to RESR api and obtaint basic information about persona. As for me I have an idea detect how much time ny friends spend (or waste) visiting vk.com.

Having read vk.com api doumantation I desided get unique itendifiers of my friends via friends.get method and then check if anyone is online among them via getProfiles(). The most interesting this that getProfiles does't reqiues user authentification, and in addition it requesres just one parametr - user id.

// here is genaral template for accessing vk.com api methods
https://api.vkontakte.ru/method/METHOD_NAME?PARAMETERS&access_token=ACCESS_TOKEN


субота, 7 січня 2012 р.

How to parse HTML in Java?

Recently, I started to design interesting application for parsing HTML pages. Having looked for compliment libraries I came across HTML parser library.
It looked like nice solution for my task, so I started to implement simple application to parse information from my own blog.
It was quite easy. Parsing in HTML Parser library can be applied in several simple steps.
The primary class of this library is Parser one. Parser can be created via constructors that take String, URL or Lexer (utility class from the HTML parser library).
    // Here we create instance of parser
 Parser parser = new Parser ("http://example.com");
 NodeList list = parser.parse (null);
 // here we obtain list of nodes from the page

If user wants to filter posts by tag name, attribute name or attribute value, he should apply the second principle instance of HTML parser library - Filter. User can combine different filters in accordance to required data to parse. Here you can observe documentation for it.

    // elements() gets all elements form the page
    NodeIterator ni = parser.elements();
    // here we set combined filter by tag name
    // and attribute name 
    NodeFilter nodeFilter =
        new AndFilter (
            new TagNameFilter ("Some tag name here"),
            new HasChildFilter (
                new HasAttributeFilter("border","0")
        )
    );
    // and via collectInfo() method we collect info to instance of nodeList
    for (NodeIterator nodeIterator = parser.elements(); nodeIterator.hasMoreNodes(); ) {
        nodeIterator.nextNode().collectInto(nodeList, nodeFilter);
    }

Talking about filters we should put our attention on official documentation of filters package.

So user can find some information on html page selected write node and collect required information to his program. It's important to say that HTML parser library has much more features then I described. Here you've read simple example about parsing HTML in Java.