From d4bdb4568901b72b72c427a4477ad4ce320382e5 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 2 Mar 2018 20:23:36 -0500 Subject: Re-enabled posting to Twitter --- lunatic.cpp | 191 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 109 insertions(+), 82 deletions(-) (limited to 'lunatic.cpp') diff --git a/lunatic.cpp b/lunatic.cpp index 5dd198c..daebc73 100644 --- a/lunatic.cpp +++ b/lunatic.cpp @@ -54,7 +54,7 @@ int main(int argc, char** argv) auth.setAccessKey(config["access_key"].as()); auth.setAccessSecret(config["access_secret"].as()); - //twitter::client client(auth); + twitter::client client(auth); database db(config["database"].as()); @@ -97,132 +97,159 @@ int main(int argc, char** argv) Magick::Image moonColor; moonColor.read("res/" + ach.color + ".png"); + // Start with the Odyssey text overlay + Magick::Image overlay; try { - // Start with the Odyssey text overlay - Magick::Image overlay; overlay.read("res/overlay.png"); + } catch (const Magick::WarningCoder& ex) + { + // Ok + } + + // Add the moon image + overlay.composite(moonColor, 672, 85, Magick::OverCompositeOp); - // Add the moon image - overlay.composite(moonColor, 672, 85, Magick::OverCompositeOp); + // Add the name of the achievement + overlay.fontPointsize(54); + overlay.fillColor("white"); + overlay.font("@" + config["title_font"].as()); - // Add the name of the achievement - overlay.fontPointsize(54); - overlay.fillColor("white"); - overlay.font("@" + config["title_font"].as()); + std::list words = split>( + ach.title, + " "); + std::ostringstream wrappingStream; + std::string curline; + int lines = 1; - std::list words = split>( - ach.title, - " "); - std::ostringstream wrappingStream; - std::string curline; - int lines = 1; + Magick::TypeMetric metric; + while (!words.empty()) + { + std::string temp = curline; - Magick::TypeMetric metric; - while (!words.empty()) + if (!curline.empty()) { - std::string temp = curline; + temp += " "; + } - if (!curline.empty()) - { - temp += " "; - } + temp += words.front(); - temp += words.front(); + overlay.fontTypeMetrics(temp, &metric); - overlay.fontTypeMetrics(temp, &metric); + if (metric.textWidth() > 1200) + { + wrappingStream << std::endl; + curline = words.front(); - if (metric.textWidth() > 1200) + lines++; + } else { + if (!curline.empty()) { - wrappingStream << std::endl; - curline = words.front(); - - lines++; - } else { - if (!curline.empty()) - { - wrappingStream << " "; - } - - curline = temp; + wrappingStream << " "; } - wrappingStream << words.front(); - words.pop_front(); + curline = temp; } - std::string wrapped = wrappingStream.str(); + wrappingStream << words.front(); + words.pop_front(); + } - overlay.annotate( - wrapped, - Magick::Geometry(1600, 228, 0, 710), - Magick::GravityType::NorthGravity); + std::string wrapped = wrappingStream.str(); - // Add the achievement date - did theDid = db.getRandomDidForAchievement(ach.achievementId); + overlay.annotate( + wrapped, + Magick::Geometry(1600, 228, 0, 710), + Magick::GravityType::NorthGravity); - overlay.fontTypeMetrics(wrapped, &metric); + // Add the achievement date + did theDid = db.getRandomDidForAchievement(ach.achievementId); - overlay.fontPointsize(20); - overlay.font("@" + config["date_font"].as()); - overlay.annotate( - theDid.date, - Magick::Geometry(1600, 228, 0, 710 + metric.textHeight() * lines - 22), - Magick::GravityType::NorthGravity); + overlay.fontTypeMetrics(wrapped, &metric); - // Make a shadow copy - Magick::Image shadow(overlay); - shadow.negate(); - shadow.blur(0, 12); + overlay.fontPointsize(20); + overlay.font("@" + config["date_font"].as()); + overlay.annotate( + theDid.date, + Magick::Geometry(1600, 228, 0, 710 + metric.textHeight() * lines - 22), + Magick::GravityType::NorthGravity); - // Read the game image, using a default if the game has no images - Magick::Image image; + // Make a shadow copy + Magick::Image shadow(overlay); + shadow.negate(); + shadow.blur(0, 12); - if (db.doesGameHaveImages(ach.gameId)) - { - std::string imageName = db.getRandomImageForGame(ach.gameId); - std::string imagePath = config["images"].as() - + "/" + imageName; + // Read the game image, using a default if the game has no images + Magick::Image image; + if (db.doesGameHaveImages(ach.gameId)) + { + std::string imageName = db.getRandomImageForGame(ach.gameId); + std::string imagePath = config["images"].as() + + "/" + imageName; + + try + { image.read(imagePath); + } catch (const Magick::WarningCoder& ex) + { + // Ok + } - // Stretch and pixelate it - image.transform("1600x900!"); - image.scale("80x45"); - image.scale("1600x900"); - } else { + // Stretch and pixelate it + image.transform("1600x900!"); + image.scale("80x45"); + image.scale("1600x900"); + } else { + try + { image.read("res/default.png"); - image.transform("1600x900!"); + } catch (const Magick::WarningCoder& ex) + { + // Ok } - // Add the generated overlay to it - image.composite(shadow, 0, 0, Magick::OverCompositeOp); - image.composite(overlay, 0, 0, Magick::OverCompositeOp); + image.transform("1600x900!"); + } + + // Add the generated overlay to it + image.composite(shadow, 0, 0, Magick::OverCompositeOp); + image.composite(overlay, 0, 0, Magick::OverCompositeOp); - // Output for debug - image.magick("png"); - image.write("output.png"); + // Output image + Magick::Blob outputBlob; + image.magick("png"); + + try + { + image.write(&outputBlob); } catch (const Magick::WarningCoder& ex) { // Ok } std::string header = "YOU GOT A MOON!"; - std::string action = header + "\n" + ach.title; action.resize(140); - /*try + std::cout << action << std::endl; + + try { - client.updateStatus(action); + long media_id = client.uploadMedia( + "image/png", + static_cast(outputBlob.data()), + outputBlob.length()); + + client.updateStatus(action, {media_id}); + + std::cout << "Tweeted!" << std::endl; } catch (const twitter::twitter_error& e) { std::cout << "Twitter error: " << e.what() << std::endl; - }*/ - - std::cout << action << std::endl; - std::cout << "Waiting" << std::endl; + } + std::cout << "Waiting..." << std::endl; std::this_thread::sleep_for(std::chrono::hours(1)); std::cout << std::endl; -- cgit 1.4.1