Last active
August 29, 2015 14:11
-
-
Save Tosainu/2650a27e4b24cc1958e4 to your computer and use it in GitHub Desktop.
twitppを使ったC++11製update_name. Tosainu/twitppとdropbox/json11, 最新のBoostと(gcc|clang)が要ります.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
update_name: update_name.cc json11/json11.cpp | |
clang++ -Wall -Wextra -std=c++14 -ltwitpp $^ -o $@ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <regex> | |
#include <boost/optional.hpp> | |
#include <twitpp/twitpp.hh> | |
#include "json11/json11.hpp" | |
boost::optional<json11::Json> json_parse(const std::string& str) { | |
std::string err; | |
auto json = json11::Json::parse(str, err); | |
return err.empty() ? json : boost::none; | |
} | |
auto main() -> int { | |
const std::regex update_name_regex("^@myon___\\supdate_name\\s(.+)$"); | |
twitpp::oauth::client client(twitpp::oauth::account("CK", "CS", "AT", "AS")); | |
client.stream_get("https://userstream.twitter.com/1.1/user.json", [&](auto& response) { | |
if (response.status_code != 200) { | |
std::cerr << "Error!!" << std::endl; | |
return; | |
} | |
if (const auto tweet = json_parse(response.body)) { | |
response.body.clear(); | |
if ((*tweet)["created_at"].is_null()) return; | |
if (!(*tweet)["retweeted_status"].is_null()) return; | |
auto id = (*tweet)["id_str"].string_value(); | |
auto screen_name = (*tweet)["user"]["screen_name"].string_value(); | |
auto text = (*tweet)["text"].string_value(); | |
std::smatch res; | |
if (std::regex_match(text, res, update_name_regex)) { | |
if (client.post("https://api.twitter.com/1.1/account/update_profile.json", {{"name", res.str(1)}}).status_code == 200) { | |
client.post("https://api.twitter.com/1.1/statuses/update.json", { | |
{"in_reply_to_status_id", id}, | |
{"status", "@" + screen_name + " [" + res.str(1) + "] になりました."} | |
}); | |
} | |
} | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment