wp_saver_rcpp_fw/main.cpp

217 lines
5.4 KiB
C++
Raw Normal View History

2021-11-19 09:24:55 +01:00
#include <string.h>
#include <iostream>
#include <string>
#include "core/file_cache.h"
#include "core/http/web_application.h"
#include "app/ccms_application.h"
#include "database/db_init.h"
#include "core/settings.h"
#include "modules/drogon/web_application.h"
2021-11-20 19:21:21 +01:00
// Backends
2021-11-19 09:24:55 +01:00
#include "backends/hash_hashlib/setup.h"
#include "core/database/database_manager.h"
2021-11-20 01:40:38 +01:00
#include "core/database/query_builder.h"
#include "core/database/query_result.h"
#include "core/database/table_builder.h"
2021-11-19 09:24:55 +01:00
#include "platform/platform_initializer.h"
#include "core/os/platform.h"
#include "modules/rbac/rbac_rank.h"
#include "core/html/html_parser.h"
#include "core/settings.h"
2021-11-20 01:40:38 +01:00
#include "modules/drogon/drogon/lib/inc/drogon/HttpClient.h"
#include "modules/drogon/drogon/lib/inc/http/HttpRequest.h"
2021-11-20 19:21:21 +01:00
#include "modules/drogon/trantor/net/EventLoop.h"
2021-11-20 01:40:38 +01:00
//#include "modules/drogon/trantor/net/Resolver.h"
//#include "modules/drogon/trantor/net/TcpClient.h"
2021-11-20 19:21:21 +01:00
#include <string>
2021-11-19 09:24:55 +01:00
void initialize_backends() {
initialize_database_backends();
backend_hash_hashlib_install_providers();
}
2021-11-20 01:40:38 +01:00
void setup_database(Database *db) {
Ref<QueryBuilder> qb = db->get_query_builder();
qb->select("name")->from("sqlite_master")->where()->wp("type", "table")->land()->wp("name", "settings")->end_command();
Ref<QueryResult> res = qb->run();
if (res->next_row()) {
return;
}
qb->reset();
Ref<TableBuilder> tb = db->get_table_builder();
tb->create_table("settings");
tb->integer("id")->auto_increment()->next_row();
tb->varchar("last_url", 1000)->not_null()->next_row();
tb->integer("table_version")->not_null()->next_row();
tb->primary_key("id");
tb->ccreate_table();
tb->create_table("data");
tb->integer("id")->auto_increment()->next_row();
tb->varchar("url", 1000)->not_null()->next_row();
tb->text("data")->not_null()->next_row();
tb->primary_key("id");
tb->ccreate_table();
tb->run_query();
qb->insert("settings", "id,last_url,table_version")->values()->val(1)->val("")->val(1)->cvalues()->end_command();
qb->run_query();
}
String get_last_url(Database *db) {
Ref<QueryBuilder> qb = db->get_query_builder();
qb->select("last_url")->from("settings")->where()->wp("id", 1)->end_command();
Ref<QueryResult> res = qb->run();
if (!res->next_row()) {
RLOG_ERR("get_last_url: !res->next_row()");
return "";
}
return String(res->get_cell(0));
}
void save_page(Database *db, const String &url, const String &data) {
}
2021-11-20 19:21:21 +01:00
String *ss = nullptr;
2021-11-20 01:40:38 +01:00
void download_posts(Database *db, const String &site) {
setup_database(db);
String url = Settings::get_singleton()->get_value(site + ".url");
String port = Settings::get_singleton()->get_value(site + ".port");
String first_url = Settings::get_singleton()->get_value(site + ".first_url");
String last_url = get_last_url(db);
2021-11-20 19:21:21 +01:00
String full_site_url = port + "://" + url;
2021-11-20 01:40:38 +01:00
if (last_url == "") {
last_url = first_url;
}
RLOG_MSG("Post downloading started for " + site + " | last url: " + last_url);
2021-11-20 19:21:21 +01:00
RLOG_MSG("Sending query to: " + full_site_url + last_url + "\n");
2021-11-20 01:40:38 +01:00
trantor::EventLoop *loop;
std::thread t([&loop]() { loop = new trantor::EventLoop(); loop->loop(); delete loop; loop = nullptr; });
2021-11-20 17:44:55 +01:00
while (loop == nullptr) {
2021-11-20 19:21:21 +01:00
// todo sleep
2021-11-20 17:44:55 +01:00
}
2021-11-20 19:21:21 +01:00
HttpClientPtr http_client = drogon::HttpClient::newHttpClient("http://127.0.0.1/", loop, false, false);
http_client->setUserAgent("Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0");
2021-11-20 01:40:38 +01:00
HttpRequestPtr request = drogon::HttpRequest::newHttpRequest();
request->setMethod(drogon::HttpMethod::Get);
2021-11-20 19:21:21 +01:00
request->setPath("/");
// request->setCustomContentTypeString("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\nAccept-Language: en-US,en;q=0.5\n");
2021-11-20 01:40:38 +01:00
2021-11-20 19:21:21 +01:00
bool done = false;
http_client->sendRequest(request, [&done](ReqResult res, const HttpResponsePtr &resptr) {
if (res != ReqResult::Ok) {
RLOG_ERR("sendRequest: res != ReqResult::Ok!\n");
done = true;
return;
}
2021-11-20 01:40:38 +01:00
2021-11-20 19:21:21 +01:00
RLOG_MSG("Got response. Saving.\n");
2021-11-20 01:40:38 +01:00
2021-11-20 19:21:21 +01:00
ss = new String(resptr->getBody().data());
2021-11-20 01:40:38 +01:00
2021-11-20 19:21:21 +01:00
done = true;
});
2021-11-20 01:40:38 +01:00
2021-11-20 19:21:21 +01:00
while (!done) {
// todo remove
2021-11-20 01:40:38 +01:00
}
loop->quit();
t.join();
2021-11-20 19:21:21 +01:00
if (ss) {
delete ss;
}
2021-11-20 01:40:38 +01:00
}
2021-11-19 09:24:55 +01:00
int main(int argc, char **argv, char **envp) {
PlatformInitializer::allocate_all();
PlatformInitializer::arg_setup(argc, argv, envp);
initialize_backends();
2021-11-20 01:40:38 +01:00
String database_folder = "./data/";
2021-11-19 09:24:55 +01:00
Settings *settings = new Settings(true);
settings->parse_ini_file("settings.ini");
2021-11-19 09:24:55 +01:00
bool download = Platform::get_singleton()->arg_parser.has_arg("-d");
2021-11-19 09:24:55 +01:00
if (download) {
DatabaseManager *dbm = new DatabaseManager();
2021-11-19 09:24:55 +01:00
2021-11-20 01:40:38 +01:00
bool save_original_data = settings->get_value_bool("save_original_data");
if (save_original_data) {
String sites = settings->get_value("sites");
int sc = sites.get_slice_count(',');
for (int i = 0; i < sc; ++i) {
String s = sites.get_slice(',', i);
uint32_t index = dbm->create_database("sqlite");
Database *db = dbm->databases[index];
db->connect(database_folder + s + ".sqlite");
download_posts(db, s);
}
}
2021-11-19 09:24:55 +01:00
delete dbm;
} else {
/*
FileCache *file_cache = new FileCache(true);
file_cache->wwwroot = "./www";
file_cache->wwwroot_refresh_cache();
DWebApplication *app = new DWebApplication();
app->load_settings();
app->setup_routes();
app->setup_middleware();
app->add_listener("127.0.0.1", 8080);
LOG_INFO << "Server running on 127.0.0.1:8080";
2021-11-19 09:24:55 +01:00
printf("Initialized!\n");
app->run();
delete app;
2021-11-19 09:24:55 +01:00
delete file_cache;
*/
2021-11-19 09:24:55 +01:00
}
delete settings;
PlatformInitializer::free_all();
return 0;
}