-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
118 lines (90 loc) · 3.7 KB
/
main.cpp
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
#include "FoxWebhook.hpp"
#include "spdlog/sinks/basic_file_sink.h"
// Add sleep function based on OS
#ifdef _WIN32
#include <windows.h> // Windows sleep
#define sleep(seconds){Sleep(seconds * 1000);}
#else
#include <unistd.h> // UNIX sleep
#endif
/**
* Pointer for the logger of the script.
*/
std::shared_ptr <spdlog::logger> logger;
void checkForNewPost(FoxWebhook &foxWebhook) {
// Get the most recent post from the blog. Start by getting the json.
cpr::Response response = cpr::Get(cpr::Url{"https://api.tumblr.com/v2/blog/", foxWebhook.blog, "/posts?api_key=",
foxWebhook.key, "&type=photo&npf=true&limit=1"});
// Check the response code for the post. If it isn't 200 be sure to log as an error and return now.
rapidjson::Document returnedJson;
returnedJson.Parse(response.text);
if (response.status_code >= 400) {
logger->warn("Unable to get post!\nResponse code: {0}.\nResponse text: {1}\nError message: {2}", response.status_code,
response.text, response.error.message);
return;
}
rapidjson::GenericObject<false, rapidjson::Value> responseJson = returnedJson.GetObj()["response"].GetObj();
rapidjson::GenericObject<false, rapidjson::Value> post = responseJson["posts"].GetArray()[0].GetObj();
// Compare the posts.
logger->debug(fmt::format("Comparing post id {} to post id {}", post["id_string"].GetString(),
foxWebhook.previousPost["id_string"].GetString()));
if (post["id"].GetInt64() == foxWebhook.previousPost["id"].GetInt64()) {
logger->debug("No new post found");
return;
}
std::string postUrl = post["post_url"].GetString();
logger->info(fmt::format("New post found! {}", postUrl));
rapidjson::GenericObject<false, rapidjson::Value> blog = responseJson["blog"].GetObj();
std::string avatarUrl = blog["avatar"].GetArray()[0].GetObj()["url"].GetString();
Embed embed;
embed.author.name = std::string(blog["title"].GetString()) + " has a new post";
embed.author.url = postUrl;
embed.author.icon_url = avatarUrl;
rapidjson::GenericObject content = post["content"].GetArray()[0].GetObj();
std::string contentType = content["type"].GetString();
if (contentType == "image") {
embed.image.url = content["media"].GetArray()[0].GetObj()["url"].GetString();
} else {
logger->warn("Unable to handle post type: " + contentType);
}
foxWebhook.discordWebhook.appendEmbed(embed);
foxWebhook.discordWebhook.send();
// And finally reset the previous post to the current post.
logger->debug(fmt::format("Setting previous post to {}", post["id_string"].GetString()));
foxWebhook.previousPost = post;
logger->info("Returning to main function");
}
int main() {
// Setup the logger.
logger = spdlog::basic_logger_st("Logger", "log.txt");
logger->flush_on(spdlog::level::debug);
spdlog::set_default_logger(logger);
spdlog::flush_on(spdlog::level::debug);
logger->info("Starting up script...");
// Load the FoxWebhooks from the config file.
std::vector<FoxWebhook> foxWebhooks;
int status = FoxWebhook::loadFromConfig(foxWebhooks);
// If the status from the config was not 0, return the status.
if (status != 0) {
logger->error(fmt::format("Unable to load FoxWebhooks: {}", status));
return status;
}
logger->info(fmt::format("Loaded {} FoxWebhook(s) successfully", foxWebhooks.size()));
while (true) {
try {
// Iterate through each FoxWebhook and check for a new post.
for (FoxWebhook &foxWebhook : foxWebhooks) {
checkForNewPost(foxWebhook);
}
// Wait for 90 seconds before looping...
sleep(90);
} catch (const std::exception &exception) {
// Log any exceptions and break from the loop.
logger->error(fmt::format("Error occurred! {}", exception.what()));
logger->flush();
break;
}
}
logger->warn("Exiting early!");
return 0;
}