summary refs log tree commit diff stats
path: root/sap.cpp
blob: 8805d707e18889b76eeec1929b11ed98361292a6 (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
#include "sap.h"
#include <yaml-cpp/yaml.h>
#include <thread>
#include <chrono>
#include <fstream>
#include <iostream>
#include <json.hpp>
#include <curl/curl.h>

/* - random frames from Spongebob (using ffmpeg)
 * with strange text overlaid, possibly rawr'd from
 * spongebob transcripts combined with drug trip
 * experiences? either that or something with verb
 * frames
 */

sap::sap(
  std::string configFile,
  std::mt19937& rng) :
    rng_(rng)
{
  // Load the config file.
  YAML::Node config = YAML::LoadFile(configFile);
  tempfile_ = config["tempfile"].as<std::string>();
  blog_name_ = config["blog_name"].as<std::string>();

  // Set up the Tumblr client.
  tumblr_consumer_ = std::make_unique<OAuth::Consumer>(config["consumer_key"].as<std::string>(), config["consumer_secret"].as<std::string>());
  tumblr_token_ = std::make_unique<OAuth::Token>(config["access_token"].as<std::string>(), config["access_token_secret"].as<std::string>());
  tumblr_client_ = std::make_unique<OAuth::Client>(tumblr_consumer_.get(), tumblr_token_.get());

  // Set up the text generator.
  for (const YAML::Node& corpusname : config["corpuses"])
  {
    std::ifstream infile(corpusname.as<std::string>());
    std::string corpus;
    std::string line;
    while (getline(infile, line))
    {
      if (line.back() == '\r')
      {
        line.pop_back();
      }

      corpus += line + " ";
    }

    kgramstats_.addCorpus(corpus);
  }

  kgramstats_.compile(5);
  kgramstats_.setMinCorpora(config["corpuses"].size());

  // Set up the layout designer.
  layout_ = std::unique_ptr<designer>(
    new designer(config["fonts"].as<std::string>()));

  // Set up the frame picker.
  director_ = std::unique_ptr<director>(
    new director(config["videos"].as<std::string>()));
}

void sap::run() const
{
  for (;;)
  {
    std::cout << "Generating post..." << std::endl;

    try
    {
      // Pick the video frame.
      Magick::Image image = director_->generate(rng_);

      // Generate the text.
      std::uniform_int_distribution<size_t> lenDist(5, 19);
      std::string action = kgramstats_.randomSentence(lenDist(rng_), rng_);

      // Lay the text on the video frame.
      Magick::Image textimage =
        layout_->generate(image.columns(), image.rows(), action, rng_);
      image.composite(textimage, 0, 0, Magick::OverCompositeOp);

      // Post to tumblr.
      std::cout << "Posting to Tumblr..." << std::endl;

      postToTumblr(action, std::move(image));

      std::cout << std::endl << "Posted!" << std::endl;

      // Wait.
      std::this_thread::sleep_for(std::chrono::hours(11));
    } catch (const Magick::ErrorImage& ex)
    {
      std::cout << "Image error: " << ex.what() << std::endl;
    }

    std::cout << std::endl;
  }
}

void sap::postToTumblr(const std::string& post_text, Magick::Image image) const
{
  image.magick("jpeg");
  image.write(tempfile_);

  nlohmann::json jsonMedia;
  jsonMedia["type"] = "image/jpeg";
  jsonMedia["identifier"] = "some-identifier";
  jsonMedia["width"] = image.columns();
  jsonMedia["height"] = image.rows();
  jsonMedia["alt_text"] = post_text;
  jsonMedia["caption"] = post_text;

  nlohmann::json contentBlock;
  contentBlock["type"] = "image";
  contentBlock["media"] = jsonMedia;

  nlohmann::json postBody;
  postBody["content"].push_back(contentBlock);
  postBody["tags"] = "spongebob,generated";

  std::string postText = postBody.dump();
  std::cout << postText << std::endl;

  Magick::Blob blob;
  image.write(&blob);

  std::string url = "https://api.tumblr.com/v2/blog/" + blog_name_ + "/posts";

  std::string oauthHeader = tumblr_client_->getFormattedHttpHeader(OAuth::Http::Post, url, "");

  struct curl_slist* header_list = NULL;
  header_list = curl_slist_append(header_list, oauthHeader.c_str());

  CURL* easy = curl_easy_init();
  curl_mime* mime = curl_mime_init(easy);
  curl_mimepart* json_part = curl_mime_addpart(mime);
  curl_mime_data(json_part, postText.c_str(), CURL_ZERO_TERMINATED);
  curl_mime_name(json_part, "json");
  curl_mime_type(json_part, "application/json");

  curl_mimepart* image_part = curl_mime_addpart(mime);
  curl_mime_data(image_part, reinterpret_cast<const char*>(blob.data()), blob.length());
  curl_mime_name(image_part, "some-identifier");
  curl_mime_type(image_part, "image/jpeg");
  curl_mime_filename(image_part, "image.jpg");

  curl_easy_setopt(easy, CURLOPT_MIMEPOST, mime);
  curl_easy_setopt(easy, CURLOPT_HTTPHEADER, header_list);
  curl_easy_setopt(easy, CURLOPT_URL, url.c_str());

  CURLcode error = curl_easy_perform(easy);
  if (error != CURLE_OK) {
    std::cout << curl_easy_strerror(error) << std::endl;
  }

  curl_easy_cleanup(easy);
  curl_slist_free_all(header_list);
  curl_mime_free(mime);
}