about summary refs log tree commit diff stats
path: root/snitch.cpp
blob: 3758dc06ad6139b9894cf48e68841b7b627d8ed1 (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
#include <yaml-cpp/yaml.h>
#include <twitter.h>
#include <fstream>
#include <thread>
#include <chrono>
#include <iostream>
#include <algorithm>

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

  std::string configfile(argv[1]);
  YAML::Node config = YAML::LoadFile(configfile);
    
  twitter::auth auth;
  auth.setConsumerKey(config["consumer_key"].as<std::string>());
  auth.setConsumerSecret(config["consumer_secret"].as<std::string>());
  auth.setAccessKey(config["access_key"].as<std::string>());
  auth.setAccessSecret(config["access_secret"].as<std::string>());
  
  std::ifstream img_file(config["image"].as<std::string>());
  img_file.seekg(0, std::ios::end);
  size_t img_len = img_file.tellg();
  char* img_buf = new char[img_len];
  img_file.seekg(0, std::ios::beg);
  img_file.read(img_buf, img_len);
  img_file.close();
  
  std::vector<std::string> triggers {
    "calling the cops",
    "calling the police"
  };
  
  // Initialize the client
  twitter::client client(auth);
  std::this_thread::sleep_for(std::chrono::minutes(1));
  
  // Start streaming
  std::cout << "Starting streaming" << std::endl;
  std::set<twitter::user_id> streamed_friends;
  twitter::stream userStream(client, [&] (const twitter::notification& n) {
    if (n.getType() == twitter::notification::type::friends)
    {
      streamed_friends = n.getFriends();
    } else if (n.getType() == twitter::notification::type::follow)
    {
      streamed_friends.insert(n.getUser().getID());
    } else if (n.getType() == twitter::notification::type::unfollow)
    {
      streamed_friends.erase(n.getUser().getID());
    } else if (n.getType() == twitter::notification::type::tweet)
    {
      // Only monitor people you are following
      if (streamed_friends.count(n.getTweet().getAuthor().getID()) == 1)
      {
        std::string orig = n.getTweet().getText();
        std::string canonical;
        std::transform(std::begin(orig), std::end(orig), std::back_inserter(canonical), [] (char ch) {
          return std::tolower(ch);
        });
        
        for (auto trigger : triggers)
        {
          if (canonical.find(trigger) != std::string::npos)
          {
            std::cout << "Calling the cops on @" << n.getTweet().getAuthor().getScreenName() << std::endl;
        
            try
            {
              long media_id = client.uploadMedia("image/jpeg", (const char*) img_buf, img_len);
              client.replyToTweet(n.getTweet().generateReplyPrefill(), n.getTweet().getID(), {media_id});
            } catch (const twitter::twitter_error& e)
            {
              std::cout << "Twitter error: " << e.what() << std::endl;
            }
            
            break;
          }
        }
      }
    } else if (n.getType() == twitter::notification::type::followed)
    {
      try
      {
        n.getUser().follow();
      } catch (const twitter::twitter_error& e)
      {
        std::cout << "Twitter error while following @" << n.getUser().getScreenName() << ": " << e.what() << std::endl;
      }
    }
  }, true, true);

  for (;;)
  {
    std::this_thread::sleep_for(std::chrono::minutes(1));
    
    try
    {
      std::set<twitter::user_id> friends = client.getUser().getFriends();
      std::set<twitter::user_id> followers = client.getUser().getFollowers();

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

      for (auto f : old_friends)
      {
        client.unfollow(f);
      }
      
      for (auto f : new_followers)
      {
        client.follow(f);
      }
    } catch (const twitter::twitter_error& e)
    {
      std::cout << "Twitter error: " << e.what() << std::endl;
    }
    
    std::this_thread::sleep_for(std::chrono::hours(4));
  }
}