summary refs log tree commit diff stats
path: root/toldya.cpp
blob: 0910945127e3c6b0749df12b5440e492bdd47c5b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <twitter.h>
#include <yaml-cpp/yaml.h>
#include <mutex>
#include <thread>
#include <ctime>
#include <chrono>
#include <iostream>
#include <algorithm>
#include <random>

int main(int argc, char** argv)
{
  if (argc != 2)
  {
    std::cout << "usage: toldya [configfile]" << std::endl;
    return -1;
  }

  std::random_device randomDevice;
  std::mt19937 rng(randomDevice());

  std::string configfile(argv[1]);
  YAML::Node config = YAML::LoadFile(configfile);

  twitter::auth auth(
    config["consumer_key"].as<std::string>(),
    config["consumer_secret"].as<std::string>(),
    config["access_key"].as<std::string>(),
    config["access_secret"].as<std::string>());

  std::vector<std::string> captions {
    "It begins.",
    "Yikes.",
    "Frightening.",
    "This is how it starts..."
  };

  std::map<twitter::user_id, std::vector<twitter::tweet>> potential;
  std::set<twitter::tweet_id> tweetIds;

  twitter::client client(auth);
  std::set<twitter::user_id> friends = client.getFriends();

  for (;;)
  {
    // Poll every 5 minutes
    auto midtime = time(NULL);
    auto midtm = localtime(&midtime);

    // At 9am, do the daily tweet
    bool shouldTweet = false;
    if (midtm->tm_hour == 8 && midtm->tm_min >= 55)
    {
      shouldTweet = true;

      std::cout << "Sleeping for 5 minutes (will tweet)..." << std::endl;
    } else {
      std::cout << "Sleeping for 5 minutes..." << std::endl;
    }

    std::this_thread::sleep_for(std::chrono::minutes(5));

    std::list<twitter::tweet> newTweets = client.getHomeTimeline().poll();
    for (twitter::tweet& nt : newTweets)
    {
      // Only monitor people you are following
      // Ignore retweets
      // Ignore messages
      if (
        (friends.count(nt.getAuthor().getID()) == 1)
        && (!nt.isRetweet())
        && (nt.getText().front() != '@')
      )
      {
        std::cout << nt.getID() << ": " << nt.getText() << std::endl;

        tweetIds.insert(nt.getID());
        potential[nt.getAuthor().getID()].emplace_back(std::move(nt));
      }
    }

    newTweets.clear();

    // The rest of the loop is once-a-day
    if (!shouldTweet)
    {
      continue;
    }

    // Unfollow people who have unfollowed us
    try
    {
      friends = client.getFriends();

      std::set<twitter::user_id> followers = client.getFollowers();

      std::list<twitter::user_id> oldFriends;
      std::set_difference(
        std::begin(friends),
        std::end(friends),
        std::begin(followers),
        std::end(followers),
        std::back_inserter(oldFriends));

      std::set<twitter::user_id> newFollowers;
      std::set_difference(
        std::begin(followers),
        std::end(followers),
        std::begin(friends),
        std::end(friends),
        std::inserter(newFollowers, std::begin(newFollowers)));

      std::set<twitter::user_id> oldFriendsSet;
      for (twitter::user_id f : oldFriends)
      {
        oldFriendsSet.insert(f);

        try
        {
          client.unfollow(f);
        } catch (const twitter::twitter_error& error)
        {
          std::cout << "Twitter error while unfollowing: " << error.what()
            << std::endl;
        }
      }

      std::list<twitter::user> newFollowerObjs =
        client.hydrateUsers(std::move(newFollowers));

      for (const twitter::user& f : newFollowerObjs)
      {
        if (!f.isProtected())
        {
          try
          {
            client.follow(f);
          } catch (const twitter::twitter_error& error)
          {
            std::cout << "Twitter error while following: " << error.what()
              << std::endl;
          }
        }
      }

      // Hydrate the tweets we've received to make sure that none of them have
      // been deleted.
      std::list<twitter::tweet> hydrated = client.hydrateTweets(tweetIds);
      tweetIds.clear();

      std::set<twitter::tweet_id> hydratedIds;
      for (twitter::tweet& tw : hydrated)
      {
        hydratedIds.insert(tw.getID());
      }

      hydrated.clear();

      // Filter the potential tweets for users that are still following us, and
      // and for tweets that haven't been deleted.
      std::map<twitter::user_id, std::vector<twitter::tweet>> toKeep;

      for (auto& p : potential)
      {
        // The author has not unfollowed
        if (!oldFriendsSet.count(p.first))
        {
          std::vector<twitter::tweet> userTweets;

          for (twitter::tweet& pt : p.second)
          {
            // The tweet was not deleted
            if (hydratedIds.count(pt.getID()))
            {
              userTweets.push_back(std::move(pt));
            }
          }

          if (!userTweets.empty())
          {
            toKeep[p.first] = std::move(userTweets);
          }
        }
      }

      potential = std::move(toKeep);
    } catch (const twitter::twitter_error& error)
    {
      std::cout << "Twitter error while getting friends/followers: "
        << error.what() << std::endl;
    }

    // Tweet!
    if (!potential.empty())
    {
      std::uniform_int_distribution<size_t> userDist(0, potential.size() - 1);
      const std::vector<twitter::tweet>& toQuoteUser =
        std::next(std::begin(potential), userDist(rng))->second;

      std::uniform_int_distribution<size_t> postDist(0, toQuoteUser.size() - 1);
      const twitter::tweet& toQuote = toQuoteUser.at(postDist(rng));

      std::uniform_int_distribution<size_t> captionDist(0, captions.size() - 1);
      const std::string& caption = captions.at(captionDist(rng));

      std::string doc = caption + " " + toQuote.getURL();

      try
      {
        client.updateStatus(doc);

        std::cout << "Tweeted!" << std::endl;
      } catch (const twitter::twitter_error& error)
      {
        std::cout << "Error tweeting: " << error.what() << std::endl;
      }

      potential.clear();
    }
  }
}