checks-parser/net/net.cpp

64 lines
1.6 KiB
C++
Raw Normal View History

2024-11-22 23:26:42 +03:00
#include "net.h"
#include <curl/curl.h>
#include <iostream>
#include <vector>
#include <regex>
struct data {};
size_t write_data(void *buffer, size_t size, size_t nmemb, void *filename) {
FILE *f = fopen(((std::string *)filename)->c_str(), "w");
size_t written = fwrite(buffer, size, nmemb, f);
fclose(f);
return written;
}
Net::Net() {}
void write_modules(void *buffer, size_t size, size_t nmemb, void *modules) {
std::vector<std::string> *modules_vector =
(std::vector<std::string> *)modules;
std::string to_parse = std::string((char*)buffer);
std::regex r("(?!\\\")\\w+\\.json(?!\\\")", std::regex::collate);
std::smatch res;
std::string::const_iterator search(to_parse.cbegin());
while (std::regex_search(search, to_parse.cend(), res, r)) {
modules_vector->push_back(res[0]);
search = res.suffix().first;
}
}
std::vector<std::string> Net::get_all_modules(std::string url) {
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_modules);
std::vector<std::string> modules {};
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &modules);
curl_easy_perform(handle);
return modules;
}
std::string Net::get_file(std::string url, std::string filename) {
CURL *handle = curl_easy_init();
curl_easy_setopt(handle, CURLOPT_URL, url.c_str());
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, &filename);
auto success = curl_easy_perform(handle);
curl_easy_cleanup(handle);
return "";
}