Twitter search — cpp-netlib v0.11.0

### Navigation

- next
- previous |
- cpp-netlib v0.11.0 »
- Examples »

# Twitter search

This example uses Twitter’s search API to list recent tweets given a user query. New features introduced here include the URI builder and `uri::encoded` function.

## The code

    #include <boost/network/protocol/http/client.hpp>
    #include "rapidjson/rapidjson.h"
    #include "rapidjson/document.h"
    #include <iostream>

    int main(int argc, char *argv[]) {
        using namespace boost::network;
        using namespace rapidjson;

        if (argc != 2) {
            std::cout << "Usage: " << argv[0] << " <query>" << std::endl;
            return 1;
        }

        try {
            http::client client;

            uri::uri base_uri("http://search.twitter.com/search.json");

            std::cout << "Searching Twitter for query: " << argv[1] << std::endl;
            uri::uri search;
            search << base_uri << uri::query("q", uri::encoded(argv[1]));
            http::client::request request(search);
            http::client::response response = client.get(request);

            Document d;
            if (!d.Parse<0>(response.body().c_str()).HasParseError()) {
                const Value &results = d["results"];
                for (SizeType i = 0; i < results.Size(); ++i)
                {
                    const Value &user = results[i]["from_user_name"];
                    const Value &text = results[i]["text"];
                    std::cout << "From: " << user.GetString() << std::endl
                              << "  " << text.GetString() << std::endl
                              << std::endl;
                }
            }
        }
        catch (std::exception &e) {
            std::cerr << e.what() << std::endl;
        }

        return 0;
    }

Note

To parse the results of these queries, this example uses rapidjson, a header-only library that is released under the MIT License.

## Building and running `twitter_search`

    $ cd ~/cpp-netlib-build
    $ make twitter_search

Twitter provides a powerful set of operators to modify the behaviour of search queries. Some examples are provided below:

    $ ./example/twitter_search "Lady Gaga"

Returns any results that contain the exact phrase “Lady Gaga”.

    $ ./example/twitter_search "#olympics"

Returns any results with the #olympics hash tag.

    $ ./example/twitter_search "flight :("

Returns any results that contain “flight” and have a negative attitude.

More examples can be found on Twitter’s search API page.

## Diving into the code

    uri::uri base_uri("http://search.twitter.com/search.json");

    std::cout << "Searching Twitter for query: " << argv[1] << std::endl;
    uri::uri search;
    search << base_uri << uri::query("q", uri::encoded(argv[1]));

The `cpp-netlib` URI builder uses a stream-like syntax to allow developers to construct more complex URIs. The example above re-uses the same base URI and allows the command line argument to be used as part of the URI query. The builder also supports percent encoding using the `encoded` directive.

### Navigation

- next
- previous |
- cpp-netlib v0.11.0 »
- Examples »