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

int main(int argc, char** argv)
{
  if (argc != 2)
  {
    std::cout << "usage: manifesto [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>());
  
  twitter::client client(auth);
  
  long loc = 0;
  if (config["current_location"])
  {
    loc = config["current_location"].as<long>();
  }
  
  std::list<std::string> tweets;
  {
    std::ifstream corpus(config["corpus"].as<std::string>());
    corpus.ignore(loc);
  
    char buffer[140];
    while (corpus)
    {
      corpus.read(buffer, 140);
      tweets.push_back(std::string(buffer, 140));
    }
  }
  
  while (!tweets.empty())
  {
    try
    {
      std::string nextTweet = tweets.front();
      client.updateStatus(nextTweet);
      
      std::cout << "Tweeted!" << std::endl;

      loc += 140;
      {
        std::ofstream fout(configfile);
        config["current_location"] = loc;
        fout << config;
      }

      tweets.pop_front();
    } catch (const twitter::twitter_error& e)
    {
      std::cout << "Twitter error: " << e.what() << std::endl;
    }
    
    std::cout << "Waiting..." << std::endl;
    
    std::this_thread::sleep_for(std::chrono::hours(6));
  }
}