Code Monkey home page Code Monkey logo

twitter-api-c-library's Introduction

C++ Twitter API Library

A C++17 library for access to Twitter's Standard v1.1 REST and Streaming APIs.

This library is not production ready! It was built in order to learn about networking and Web APIs.

Example

#include <iostream>
#include <twitterlib/twitterlib.hpp>

int main()
{
    using namespace twitter;

    auto const keys = oauth::read_credentials("keys");

    // REST API
    // -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

    // Returns JSON response from twitter.
    std::cout << get_account_settings(keys) << std::endl;

    // Update account's status.
    update_status(keys, {"Hello, Twitter!"});

    // Add a reply to an existing Tweet.
    update_status(keys, [] {
        auto p                  = Update_status_parameters{};
        p.message               = "A Reply Tweet";
        p.in_reply_to_status_id = 1405048013278519299;
        return p;
    }());

    // Streaming API
    // -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

    // Track Tweets relating to water.
    auto const parameters = [] {
        auto p = Stream_parameters{};
        p.track.push_back("water");
        p.track.push_back("ocean");
        p.track.push_back("rain");
        p.track.push_back("๐Ÿ’ง");
        p.track.push_back("๐ŸŒŠ");
        p.track.push_back("โ˜”");
        return p;
    }();

    // Invoked each time the search stream receives data.
    auto const show_tweet = [](auto const& response) {
        std::cout << parse_tweet(network::to_ptree(response)).text << "\n\n"
                  << std::flush;
    };

    auto const request = build_filtered_stream_request(keys, parameters);
    auto const stream  = network::Stream::launch(request, show_tweet);

    // Blocking call to allow async stream to be processed indefinitely.
    network::wait();

    return 0;
}

Build

CMake is the supported build generator, it generates the twitterlib library target that can be linked against.

Other generated target are twitterlib.demos and twitterlib.tests.

It is recommended to use this library as a git submodule in your project, for easier version control, though there is an installation target generated.

Usage

Register for a Twitter Developer Account. Create a new application via the Twitter App Dashboard. Take a note of the consumer keys and account secrets on the "Keys and Tokens" tab for your app.

Create a text file called "keys" containing your Twitter API consumer keys and secrets:

consumer_key *KEY*
consumer_token *TOKEN*
user_token *USERTOKEN*
token_secret *TOKENSECRET*

Library Dependencies

  • Boost ASIO >= 1.58
  • Boost Property Tree >= 1.58
  • OpenSSL >= 1.1
  • zlib

twitter-api-c-library's People

Contributors

a-n-t-h-o-n-y avatar kiayria avatar computergeek1507 avatar andypiper avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.