From b77ab7cb283e64fb8b23f4892115644e84596842 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 23 Feb 2018 09:18:01 -0500 Subject: Created bot --- .gitignore | 6 ++++ .gitmodules | 3 ++ CMakeLists.txt | 16 ++++++++++ lunatic.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ scrape.rb | 38 ++++++++++++++++++++++++ vendor/libtwittercpp | 1 + 6 files changed, 148 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 lunatic.cpp create mode 100644 scrape.rb create mode 160000 vendor/libtwittercpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ae9dcf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +.AppleDouble +.DS_Store +CMakeCache.txt +Makefile +cmake_install.cmake +CMakeFiles diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b7a969a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vendor/libtwittercpp"] + path = vendor/libtwittercpp + url = git@github.com:hatkirby/libtwittercpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..55a671b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required (VERSION 3.1) +project (lunatic) + +add_subdirectory(vendor/libtwittercpp) + +find_package(PkgConfig) +pkg_check_modules(yaml-cpp yaml-cpp REQUIRED) + +include_directories( + vendor/libtwittercpp/src + ${yaml-cpp_INCLUDE_DIRS}) + +add_executable(lunatic lunatic.cpp) +set_property(TARGET lunatic PROPERTY CXX_STANDARD 11) +set_property(TARGET lunatic PROPERTY CXX_STANDARD_REQUIRED ON) +target_link_libraries(lunatic ${yaml-cpp_LIBRARIES} twitter++) \ No newline at end of file diff --git a/lunatic.cpp b/lunatic.cpp new file mode 100644 index 0000000..2438788 --- /dev/null +++ b/lunatic.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + if (argc != 2) + { + std::cout << "usage: lunatic [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()); + auth.setConsumerSecret(config["consumer_secret"].as()); + auth.setAccessKey(config["access_key"].as()); + auth.setAccessSecret(config["access_secret"].as()); + + twitter::client client(auth); + + std::random_device randomDevice; + std::mt19937 rng(randomDevice()); + + for (;;) + { + std::cout << "Generating tweet" << std::endl; + + // Reload achievements list every time in case it has been updated + std::vector achievements; + std::ifstream datafile(config["achievements"].as()); + if (!datafile.is_open()) + { + std::cout << "Could not find achievements file." << std::endl; + return 1; + } + + std::string line; + while (getline(datafile, line)) + { + if (line.back() == '\r') + { + line.pop_back(); + } + + achievements.push_back(line); + } + + std::uniform_int_distribution dist(0, achievements.size() - 1); + std::string achievement = achievements[dist(rng)]; + + std::string header; + if (std::bernoulli_distribution(1.0 / 50.0)(rng)) + { + header = "YOU GOT A MULTI MOON!"; + } else { + header = "YOU GOT A MOON!"; + } + + std::string action = header + "\n" + achievement; + action.resize(140); + + try + { + client.updateStatus(action); + } 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::this_thread::sleep_for(std::chrono::hours(3)); + + std::cout << std::endl; + } +} diff --git a/scrape.rb b/scrape.rb new file mode 100644 index 0000000..5d62e89 --- /dev/null +++ b/scrape.rb @@ -0,0 +1,38 @@ +require 'json' +require 'nokogiri' +require 'open-uri' +require 'yaml' + +config = YAML.load(open("config.yml")) +usernames = config["usernames"] + +achieves = usernames.map do |username| + page = Nokogiri::HTML(open("https://steamcommunity.com/id/#{username}/games/?tab=all")) + script = page.css(".responsive_page_template_content script").text[18..-1] + data = JSON.parse(script[0..script.index(";\r\n\t\t")-1]) + ids = data.map { |d| d["appid"] } + + ids.map do |id| + achsp = Nokogiri::HTML(open("https://steamcommunity.com/id/#{username}/stats/#{id}/")) + achsp.css(".achieveTxt .achieveUnlockTime + h3").map { |d| d.text } + end +end.flatten + +if File.exists?("achieves.txt") + already = File.read("achieves.txt").split("\n") + all_achieves = achieves + already +else + all_achieves = achieves +end + +all_achieves.sort! +all_achieves.uniq! + +if config.key? "blacklist" + blacklist = File.read(config["blacklist"]).split("\n") + all_achieves.reject! { |l| blacklist.include? l } +end + +File.open("achieves.txt", "w") do |f| + f << all_achieves.join("\n") +end diff --git a/vendor/libtwittercpp b/vendor/libtwittercpp new file mode 160000 index 0000000..d1d2051 --- /dev/null +++ b/vendor/libtwittercpp @@ -0,0 +1 @@ +Subproject commit d1d20515281e32b23657762e72bf37dcff14f0ab -- cgit 1.4.1