1
0
Fork 0
mirror of https://gitlab.com/niansa/commsyfuse.git synced 2025-03-06 20:48:31 +01:00

Added JSON dumping and changed arguments a little

This commit is contained in:
niansa 2020-04-30 16:00:37 +02:00
parent ff9a8acd67
commit c690040e8e

View file

@ -34,6 +34,9 @@ along with pilang. If not, see <https://www.gnu.org/licenses/>.
#include <curlpp/Infos.hpp>
#include <curlpp/Options.hpp>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
#include <gumbo.h>
static std::string server_url = "https://unterricht.sh.schulcommsy.de";
@ -315,9 +318,11 @@ std::string get_rss_url(GumboNode *node) { // Currently unused
int mode_cli(char *argv[], unsigned long numposts, std::vector<std::string> postsmap_numname, std::vector<std::string> postsmap_nummeta,
std::vector<std::string> postsmap_numurl, std::vector<std::string> postsmap_numdesc,
std::vector<std::vector<std::string>> postsmap_numfileurls, std::vector<std::vector<std::string>> postsmap_numfilenames) {
#define mode_tmpl(func) func(char *argv[], int argc, unsigned long numposts, std::vector<std::string> postsmap_numname, std::vector<std::string> postsmap_nummeta,std::vector<std::string> postsmap_numurl, std::vector<std::string> postsmap_numdesc,std::vector<std::vector<std::string>> postsmap_numfileurls, std::vector<std::vector<std::string>> postsmap_numfilenames)
typedef std::function<mode_tmpl(int)> mode;
#pragma GCC diagnostic ignored "-Wunused-parameter"
int mode_tmpl(mode_cli) {
// Show overview
for (unsigned long it = 0; it != numposts; it++) {
std::cout << it << ") " << postsmap_numname[it] << " " << postsmap_nummeta[it] << std::endl;
@ -368,11 +373,52 @@ int mode_cli(char *argv[], unsigned long numposts, std::vector<std::string> post
return 0;
}
int mode_tmpl(mode_json) {
json jsonroot = json::array();
// Iterate through the posts
for (unsigned long it = 0; it != numposts; it++) {
jsonroot[it] = json::object();
jsonroot[it]["name"] = postsmap_numname[it];
jsonroot[it]["meta"] = postsmap_nummeta[it];
jsonroot[it]["description"] = postsmap_numdesc[it];
jsonroot[it]["url"] = postsmap_numurl[it];
jsonroot[it]["files"] = json::array();
for (unsigned long it2 = 0; it2 != postsmap_numfileurls[it].size(); it2++) {
jsonroot[it]["files"][it2] = json::object();
jsonroot[it]["files"][it2]["name"] = postsmap_numfilenames[it][it2];
jsonroot[it]["files"][it2]["url"] = postsmap_numfileurls[it][it2];
}
}
// Serialise to cout...
std::cout << jsonroot.dump() << std::endl;
return 0;
}
static std::map<std::string, mode> modes;
static std::map<std::string, bool> mode_requires_description;
static std::map<std::string, int> mode_minargc;
void modedef_init() {
modes["cli"] = mode_cli;
mode_requires_description["cli"] = true;
mode_minargc["cli"] = 4;
modes["json"] = mode_json;
mode_requires_description["json"] = true;
mode_minargc["json"] = 4;
}
void cmdusage(char *argv[]) {
std::cerr << "Usage: " << argv[0] << " license" << std::endl
<< " " << argv[0] << " cli <SID> <room>" << std::endl
<< " " << argv[0] << " json <SID> <room>" << std::endl
<< " " << argv[0] << " * <SID> <room>" << std::endl;
}
int main(int argc, char *argv[]) {
// Catch SIGSEGV
signal(SIGSEGV, sigsegv_panic);
// Show license note if --license was given as first argument
if (argc > 1 and !strncmp(argv[1], "--license", 9)) {
if (argc > 1 and !strncmp(argv[1], "license", 9)) {
std::cout << "CommSyFuse Copyright (C) 2020 niansa" << std::endl;
std::cout << "This program comes with ABSOLUTELY NO WARRANTY; for details type `warranty'." << std::endl;
std::cout << "This is free software, and you are welcome to redistribute it" << std::endl;
@ -380,13 +426,13 @@ int main(int argc, char *argv[]) {
return 0;
}
// Check arguments
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " <SID> <room>" << std::endl;
if (argc < 4) {
cmdusage(argv);
return 1;
}
// Create required variables
server_sid = argv[1];
room = argv[2];
server_sid = argv[2];
room = argv[3];
std::stringstream httpcontent;
std::vector<std::string> postsmap_numname;
std::vector<std::string> postsmap_nummeta;
@ -397,7 +443,7 @@ int main(int argc, char *argv[]) {
std::vector<GumboNode *> postsmap_numnode;
GumboOutput *document;
// Check connection and download document
std::cout << "Connecting to server..." << std::endl;
//std::clog << "Connecting to server..." << std::endl;
long statuscode = curlreq(httpcontent, server_sid, server_url+"/room/" + room + "/material");
if (statuscode == 302) {
std::cerr << "Connection error: Invalid SID" << std::endl;
@ -414,7 +460,10 @@ int main(int argc, char *argv[]) {
httpcontent.str(std::string()); // Clear buffer just in case we need it later
// Get posts
auto posts = get_posts(document->root);
// Initialise mode definitions
modedef_init();
// Map posts and their corresponding URL to a number
//std::clog << "Loading data..." << std::endl;
unsigned long numposts = 0;
for (auto it = posts.begin(); it != posts.end(); it++) {
// Get posts name
@ -424,7 +473,8 @@ int main(int argc, char *argv[]) {
// Get posts URL
postsmap_numurl.push_back(get_post_url(*it));
// Get posts description
postsmap_numdesc.push_back(get_post_desc(server_url + *(postsmap_numurl.end() - 1)));
if (mode_requires_description[argv[1]])
postsmap_numdesc.push_back(get_post_desc(server_url + *(postsmap_numurl.end() - 1)));
// Get posts files
auto urlnamefilemap = get_post_files(*it);
for (auto it2 = urlnamefilemap.begin(); it2 != urlnamefilemap.end(); it2++) {
@ -436,7 +486,18 @@ int main(int argc, char *argv[]) {
}
numposts++;
}
// Start CLI
return mode_cli(argv, numposts, postsmap_numname, postsmap_nummeta,postsmap_numurl,
postsmap_numdesc, postsmap_numfileurls, postsmap_numfilenames);
// Check specified mode
std::string mode = static_cast<std::string>(argv[1]);
if (mode_minargc[mode] > argc) {
cmdusage(argv);
return 1;
}
// Start it
if (modes.find(mode) != modes.end()) {
return modes[mode](argv, argc, numposts, postsmap_numname, postsmap_nummeta,postsmap_numurl,
postsmap_numdesc, postsmap_numfileurls, postsmap_numfilenames);
} else {
std::cout << "It works!" << std::endl;
}
return 0;
}