From 18742d79e1de863889521c492e938491489316fe Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 3 Feb 2017 13:56:19 -0500 Subject: Created bot --- advice.cpp | 359 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 359 insertions(+) create mode 100644 advice.cpp (limited to 'advice.cpp') diff --git a/advice.cpp b/advice.cpp new file mode 100644 index 0000000..320f719 --- /dev/null +++ b/advice.cpp @@ -0,0 +1,359 @@ +#include "advice.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +advice::advice( + std::string configFile, + std::mt19937& rng) : + rng_(rng) +{ + // Load the config file. + YAML::Node config = YAML::LoadFile(configFile); + + // Set up the Twitter client. + twitter::auth auth; + auth.setConsumerKey(config["consumer_key"].as()); + auth.setConsumerSecret(config["consumer_secret"].as()); + auth.setAccessKey(config["access_key"].as()); + auth.setAccessSecret(config["access_secret"].as()); + + client_ = std::unique_ptr(new twitter::client(auth)); + + // Set up the verbly database. + database_ = std::unique_ptr(new verbly::database(config["verbly_datafile"].as())); + + // Set up the sentence generator. + generator_ = std::unique_ptr(new sentence(*database_, rng_)); +} + +verbly::word advice::generateImageNoun() const +{ + verbly::filter whitelist = + (verbly::notion::wnid == 109287968) // Geological formations + || (verbly::notion::wnid == 109208496) // Asterisms (collections of stars) + || (verbly::notion::wnid == 109239740) // Celestial bodies + || (verbly::notion::wnid == 109277686) // Exterrestrial objects (comets and meteroids) + || (verbly::notion::wnid == 109403211) // Radiators (supposedly natural radiators but actually these are just pictures of radiators) + || (verbly::notion::wnid == 109416076) // Rocks + || (verbly::notion::wnid == 105442131) // Chromosomes + || (verbly::notion::wnid == 100324978) // Tightrope walking + || (verbly::notion::wnid == 100326094) // Rock climbing + || (verbly::notion::wnid == 100433458) // Contact sports + || (verbly::notion::wnid == 100433802) // Gymnastics + || (verbly::notion::wnid == 100439826) // Track and field + || (verbly::notion::wnid == 100440747) // Skiing + || (verbly::notion::wnid == 100441824) // Water sport + || (verbly::notion::wnid == 100445351) // Rowing + || (verbly::notion::wnid == 100446980) // Archery + // TODO: add more sports + || (verbly::notion::wnid == 100021939) // Artifacts + || (verbly::notion::wnid == 101471682) // Vertebrates + ; + + verbly::filter blacklist = + (verbly::notion::wnid == 106883725) // swastika + || (verbly::notion::wnid == 104416901) // tetraskele + || (verbly::notion::wnid == 102512053) // fish + || (verbly::notion::wnid == 103575691) // instrument of execution + ; + + verbly::query pictureQuery = database_->words( + (verbly::notion::fullHypernyms %= whitelist) + && !(verbly::notion::fullHypernyms %= blacklist) + && (verbly::notion::partOfSpeech == verbly::part_of_speech::noun) + && (verbly::notion::numOfImages >= 1)); + + return pictureQuery.first(); +} + +Magick::Image advice::getImageForNoun(verbly::word pictured) const +{ + // Accept string from Google Chrome + std::string accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; + curl::curl_header headers; + headers.add(accept); + + int backoff = 0; + + std::cout << "Generating noun..." << std::endl; + std::cout << "Noun: " << pictured.getBaseForm() << std::endl; + std::cout << "Getting URLs..." << std::endl; + + std::string lstdata; + while (lstdata.empty()) + { + std::ostringstream lstbuf; + curl::curl_ios lstios(lstbuf); + curl::curl_easy lsthandle(lstios); + std::string lsturl = pictured.getNotion().getImageNetUrl(); + lsthandle.add(lsturl.c_str()); + + try + { + lsthandle.perform(); + } catch (const curl::curl_easy_exception& e) + { + e.print_traceback(); + + backoff++; + std::cout << "Waiting for " << backoff << " seconds..." << std::endl; + + std::this_thread::sleep_for(std::chrono::seconds(backoff)); + + continue; + } + + backoff = 0; + + if (lsthandle.get_info().get() != 200) + { + throw could_not_get_images(); + } + + std::cout << "Got URLs." << std::endl; + lstdata = lstbuf.str(); + } + + std::vector lstvec = verbly::split>(lstdata, "\r\n"); + if (lstvec.empty()) + { + throw could_not_get_images(); + } + + std::shuffle(std::begin(lstvec), std::end(lstvec), rng_); + + std::deque urls; + for (std::string& url : lstvec) + { + urls.push_back(url); + } + + bool found = false; + Magick::Blob img; + Magick::Image pic; + + while (!found && !urls.empty()) + { + std::string url = urls.front(); + urls.pop_front(); + + std::ostringstream imgbuf; + curl::curl_ios imgios(imgbuf); + curl::curl_easy imghandle(imgios); + + imghandle.add(headers.get()); + imghandle.add(url.c_str()); + imghandle.add(30); + + try + { + imghandle.perform(); + } catch (curl::curl_easy_exception error) { + error.print_traceback(); + + continue; + } + + if (imghandle.get_info().get() != 200) + { + continue; + } + + std::string content_type = imghandle.get_info().get(); + if (content_type.substr(0, 6) != "image/") + { + continue; + } + + std::string imgstr = imgbuf.str(); + img = Magick::Blob(imgstr.c_str(), imgstr.length()); + pic.read(img); + if (pic.rows() == 0) + { + continue; + } + + // Too small! + if (pic.columns() < 400) + { + continue; + } + + std::cout << url << std::endl; + found = true; + } + + if (!found) + { + throw could_not_get_images(); + } + + return pic; +} + +Magick::Image advice::layoutImage(Magick::Image pic, std::string title) const +{ + // Want a 16:9 aspect + int idealwidth = pic.rows()*(16.0/9.0); + if (idealwidth > pic.columns()) + { + // If the image is narrower than the ideal width, use full width. + int newheight = pic.columns()*(9.0/16.0); + + // Just take a slice out of the middle of the image. + int cropy = ((double)(pic.rows() - newheight))/2.0; + + pic.crop(Magick::Geometry(pic.columns(), newheight, 0, cropy)); + } else { + // If the image is wider than the ideal width, use full height. + // Just take a slice out of the middle of the image. + int cropx = ((double)(pic.columns() - idealwidth))/2.0; + + pic.crop(Magick::Geometry(idealwidth, pic.rows(), cropx, 0)); + } + + pic.zoom(Magick::Geometry(400, 225)); + + // Layout the text. + std::list words = verbly::split>(title, " "); + std::vector lines; + std::list cur; + Magick::TypeMetric metric; + pic.fontPointsize(20); + pic.font("@coolvetica.ttf"); + + while (!words.empty()) + { + cur.push_back(words.front()); + + std::string prefixText = verbly::implode(std::begin(cur), std::end(cur), " "); + pic.fontTypeMetrics(prefixText, &metric); + + if (metric.textWidth() > 380) + { + if (cur.size() == 1) + { + words.pop_front(); + } else { + cur.pop_back(); + } + + prefixText = verbly::implode(std::begin(cur), std::end(cur), " "); + lines.push_back(prefixText); + cur.clear(); + } else { + words.pop_front(); + } + } + + if (!cur.empty()) + { + std::string prefixText = verbly::implode(std::begin(cur), std::end(cur), " "); + lines.push_back(prefixText); + } + + int lineHeight = metric.textHeight()-2; + int blockHeight = lineHeight * lines.size() + 18; + std::cout << "line " << lineHeight << "; block " << blockHeight << std::endl; + + std::list drawList; + drawList.push_back(Magick::DrawableFillColor("black")); + drawList.push_back(Magick::DrawableFillOpacity(0.5)); + drawList.push_back(Magick::DrawableStrokeColor("transparent")); + drawList.push_back(Magick::DrawableRectangle(0, 225-blockHeight-20, 400, 255)); // 0, 225-60, 400, 255 + pic.draw(drawList); + + drawList.clear(); + drawList.push_back(Magick::DrawableFont("@coolvetica.ttf")); + drawList.push_back(Magick::DrawableFillColor("white")); + drawList.push_back(Magick::DrawablePointSize(14)); + drawList.push_back(Magick::DrawableText(10, 225-blockHeight+4, "How to")); // 10, 255-62-4 + pic.draw(drawList); + + for (int i=0; igetConfiguration().getCharactersReservedPerMedia(); + if (title.length() > tweetLim) + { + tweetText = title.substr(0, tweetLim - 1) + "…"; + } else { + tweetText = title; + } + + long media_id = client_->uploadMedia("image/png", (const char*) outputimg.data(), outputimg.length()); + client_->updateStatus(tweetText, {media_id}); +} + +void advice::run() const +{ + for (;;) + { + try + { + // Pick a noun to use for the picture. + verbly::word pictured = generateImageNoun(); + + // Find an image of the picked noun. + Magick::Image pic = getImageForNoun(pictured); + + // Generate the image text. + std::string title = generator_->generate(); + + // Layout the image. + Magick::Image output = layoutImage(std::move(pic), title); + + // Tweet the image. + sendTweet(std::move(output), title); + + std::cout << "Done!" << std::endl << "Waiting..." << std::endl << std::endl; + + // Wait. + std::this_thread::sleep_for(std::chrono::hours(1)); + } catch (const could_not_get_images& ex) + { + std::cout << ex.what() << std::endl; + } catch (const Magick::ErrorImage& ex) + { + std::cout << "Image error: " << ex.what() << std::endl; + } catch (const twitter::twitter_error& ex) + { + std::cout << "Twitter error: " << ex.what() << std::endl; + } + } +} -- cgit 1.4.1