summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2016-06-12 20:09:45 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2016-06-12 20:09:45 -0400
commit9b5d284e8fa8af25b613ce362656df6db379f322 (patch)
treebc13deac904d3ff5ea24d92df29bd059a654e493
downloadtroublemaker-9b5d284e8fa8af25b613ce362656df6db379f322.tar.gz
troublemaker-9b5d284e8fa8af25b613ce362656df6db379f322.tar.bz2
troublemaker-9b5d284e8fa8af25b613ce362656df6db379f322.zip
Initial commit
-rw-r--r--.gitmodules6
-rw-r--r--CMakeLists.txt15
-rw-r--r--troublemaker.cpp176
m---------vendor/libtwittercpp0
m---------vendor/yaml-cpp0
5 files changed, 197 insertions, 0 deletions
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..8544219 --- /dev/null +++ b/.gitmodules
@@ -0,0 +1,6 @@
1[submodule "vendor/libtwittercpp"]
2 path = vendor/libtwittercpp
3 url = https://github.com/hatkirby/libtwittercpp
4[submodule "vendor/yaml-cpp"]
5 path = vendor/yaml-cpp
6 url = https://github.com/jbeder/yaml-cpp
diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..1dd323c --- /dev/null +++ b/CMakeLists.txt
@@ -0,0 +1,15 @@
1cmake_minimum_required (VERSION 3.1)
2project (troublemaker)
3
4set(CMAKE_BUILD_TYPE Debug)
5
6add_subdirectory(vendor/libtwittercpp)
7include_directories(vendor/libtwittercpp/src)
8
9add_subdirectory(vendor/yaml-cpp EXCLUDE_FROM_ALL)
10include_directories(vendor/yaml-cpp/include)
11
12add_executable(troublemaker troublemaker.cpp)
13set_property(TARGET troublemaker PROPERTY CXX_STANDARD 11)
14set_property(TARGET troublemaker PROPERTY CXX_STANDARD_REQUIRED ON)
15target_link_libraries(troublemaker twitter++ yaml-cpp)
diff --git a/troublemaker.cpp b/troublemaker.cpp new file mode 100644 index 0000000..31381dc --- /dev/null +++ b/troublemaker.cpp
@@ -0,0 +1,176 @@
1#include <twitter.h>
2#include <mutex>
3#include <thread>
4#include <chrono>
5#include <cstdlib>
6#include <ctime>
7#include <map>
8#include <set>
9#include <algorithm>
10#include <iostream>
11#include <list>
12#include <yaml-cpp/yaml.h>
13
14struct userstats {
15 int total = 0;
16 int days = 1;
17 int day1 = 0;
18 int day2 = 0;
19 int day3 = 0;
20 int day4 = 0;
21 int day5 = 0;
22 int day6 = 0;
23 int day7 = 0;
24};
25
26int main(int argc, char** argv)
27{
28 srand(time(NULL));
29 rand(); rand(); rand(); rand();
30
31 YAML::Node config = YAML::LoadFile("config.yml");
32
33 twitter::auth auth;
34 auth.setConsumerKey(config["consumer_key"].as<std::string>());
35 auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
36 auth.setAccessKey(config["access_key"].as<std::string>());
37 auth.setAccessSecret(config["access_secret"].as<std::string>());
38
39 std::set<twitter::user_id> friends;
40 std::mutex friends_mutex;
41
42 std::map<twitter::user_id, userstats> stats;
43 std::mutex stats_mutex;
44
45 twitter::client client(auth);
46 client.setUserStreamNotifyCallback([&] (twitter::notification n) {
47 std::lock_guard<std::mutex> friend_guard(friends_mutex);
48
49 if (n.getType() == twitter::notification::type::friends)
50 {
51 friends = n.getFriends();
52 } else if (n.getType() == twitter::notification::type::follow)
53 {
54 friends.insert(n.getUser().getID());
55 } else if (n.getType() == twitter::notification::type::unfollow)
56 {
57 friends.erase(n.getUser().getID());
58 } else if (n.getType() == twitter::notification::type::tweet)
59 {
60 if (
61 (friends.count(n.getTweet().getAuthor().getID()) == 1) // Only monitor people you are following
62 && (!n.getTweet().isRetweet()) // Ignore retweets
63 && (n.getTweet().getText().front() != '@') // Ignore messages
64 )
65 {
66 std::lock_guard<std::mutex> stats_guard(stats_mutex);
67
68 userstats& s = stats[n.getTweet().getAuthor().getID()];
69 s.total++;
70 s.day1++;
71
72 if (s.days >= 7)
73 {
74 int outof = s.total;
75 if (outof < 200)
76 {
77 outof = 200;
78 }
79
80 if (rand() % outof == 0)
81 {
82 std::cout << "@" << n.getTweet().getAuthor().getScreenName() << "'s one of " << outof << "!" << std::endl;
83
84 std::string doc = client.generateReplyPrefill(n.getTweet()) + "is this a subtweet?";
85 twitter::tweet theTweet;
86 twitter::response resp = client.updateStatus(doc, theTweet, n.getTweet());
87 if (resp != twitter::response::ok)
88 {
89 std::cout << "Error tweeting witty joke: " << resp << std::endl;
90 }
91 }
92 }
93 }
94 } else if (n.getType() == twitter::notification::type::followed)
95 {
96 twitter::response resp = client.follow(n.getUser());
97 if (resp != twitter::response::ok)
98 {
99 std::cout << "Twitter error while following @" << n.getUser().getScreenName() << ": " << resp << std::endl;
100 }
101 }
102 });
103
104 std::this_thread::sleep_for(std::chrono::minutes(1));
105
106 std::cout << "Starting streaming" << std::endl;
107 client.startUserStream();
108
109 for (;;)
110 {
111 std::this_thread::sleep_for(std::chrono::hours(24));
112
113 // Unfollow people who have unfollowed us
114 std::set<twitter::user_id> friends;
115 std::set<twitter::user_id> followers;
116 twitter::response resp = client.getFriends(friends);
117 if (resp == twitter::response::ok)
118 {
119 resp = client.getFollowers(followers);
120 if (resp == twitter::response::ok)
121 {
122 std::list<twitter::user_id> old_friends, new_followers;
123 std::set_difference(std::begin(friends), std::end(friends), std::begin(followers), std::end(followers), std::back_inserter(old_friends));
124 std::set_difference(std::begin(followers), std::end(followers), std::begin(friends), std::end(friends), std::back_inserter(new_followers));
125
126 for (auto f : old_friends)
127 {
128 std::lock_guard<std::mutex> friend_guard(friends_mutex);
129 friends.erase(f);
130
131 resp = client.unfollow(f);
132 if (resp != twitter::response::ok)
133 {
134 std::cout << "Twitter error while unfollowing" << std::endl;
135 }
136 }
137
138 for (auto f : new_followers)
139 {
140 resp = client.follow(f);
141 if (resp != twitter::response::ok)
142 {
143 std::cout << "Twitter error while following" << std::endl;
144 }
145 }
146 } else {
147 std::cout << "Twitter error while getting followers: " << resp << std::endl;
148 }
149 } else {
150 std::cout << "Twitter error while getting friends: " << resp << std::endl;
151 }
152
153 // This is all just for stats rotation
154 {
155 std::lock_guard<std::mutex> stats_guard(stats_mutex);
156 for (auto mapping : stats)
157 {
158 auto& s = mapping.second;
159 if (s.days < 7)
160 {
161 s.days++;
162 }
163
164 s.day7 = s.day6;
165 s.day6 = s.day5;
166 s.day5 = s.day4;
167 s.day4 = s.day3;
168 s.day3 = s.day2;
169 s.day2 = s.day1;
170 s.day1 = 0;
171
172 s.total = s.day2 + s.day3 + s.day4 + s.day5 + s.day6 + s.day7;
173 }
174 }
175 }
176}
diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000
Subproject c3cc76301d64320791f7097709ffcd36d720626
diff --git a/vendor/yaml-cpp b/vendor/yaml-cpp new file mode 160000
Subproject 728e26e42645d4d70ca65522990f915f47b47a5