2018-02-26 07:57:30 +01:00
|
|
|
/* This file is part of mastodon-cpp.
|
2018-02-26 09:06:55 +01:00
|
|
|
* Streaming API example
|
2018-02-26 07:57:30 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <thread>
|
|
|
|
#include <chrono>
|
|
|
|
#include <memory>
|
2018-05-17 18:39:42 +02:00
|
|
|
#include <mutex>
|
2018-03-21 20:12:45 +01:00
|
|
|
#ifdef MASTODON_CPP
|
|
|
|
#include "mastodon-cpp.hpp"
|
|
|
|
#else
|
|
|
|
#include <mastodon-cpp/mastodon-cpp.hpp>
|
|
|
|
#endif
|
2018-02-26 07:57:30 +01:00
|
|
|
|
|
|
|
using Mastodon::API;
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
if (argc < 3)
|
|
|
|
{
|
|
|
|
std::cerr << "usage: " << argv[0] << " <instance> <access_token>\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string answer;
|
|
|
|
static std::unique_ptr<Mastodon::API::http> ptr;
|
|
|
|
|
|
|
|
std::cout << "Dumping public timeline...\n";
|
|
|
|
std::thread pub([=]
|
|
|
|
{
|
|
|
|
Mastodon::API masto(argv[1], argv[2]);
|
|
|
|
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
|
|
|
|
masto.get_stream(API::v1::streaming_public, answer, ptr);
|
|
|
|
});
|
|
|
|
|
|
|
|
std::uint8_t counter = 0;
|
|
|
|
while (true)
|
|
|
|
{
|
2018-05-17 18:39:42 +02:00
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(2));
|
|
|
|
if (ptr != nullptr)
|
2018-02-26 07:57:30 +01:00
|
|
|
{
|
2018-05-17 18:39:42 +02:00
|
|
|
std::lock_guard<std::mutex> lock(ptr->get_mutex());
|
|
|
|
++counter;
|
|
|
|
std::cout << answer;
|
|
|
|
answer.clear();
|
|
|
|
if (counter == 10)
|
|
|
|
{
|
|
|
|
std::cerr << "Cancelling...\n";
|
|
|
|
ptr->cancel_stream();
|
|
|
|
break;
|
|
|
|
}
|
2018-02-26 07:57:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub.join();
|
|
|
|
std::cout << '\n';
|
|
|
|
|
|
|
|
std::cout << "Dumping the #np tag...\n";
|
|
|
|
answer = "";
|
|
|
|
std::thread tag([=]
|
|
|
|
{
|
|
|
|
Mastodon::API masto(argv[1], argv[2]);
|
|
|
|
masto.set_useragent("mastodon-cpp-example/1.3.3.7");
|
2018-04-19 00:36:39 +02:00
|
|
|
masto.get_stream(API::v1::streaming_hashtag, {{ "hashtag", {"np"} }},
|
|
|
|
answer, ptr);
|
2018-02-26 07:57:30 +01:00
|
|
|
});
|
|
|
|
std::this_thread::sleep_for(std::chrono::seconds(20));
|
2018-05-13 15:41:19 +02:00
|
|
|
ptr->cancel_stream();
|
2018-02-26 07:57:30 +01:00
|
|
|
std::cout << answer;
|
|
|
|
std::cout << '\n';
|
|
|
|
tag.join();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|