1
0
Fork 0
mirror of https://gitlab.com/niansa/qcommsy.git synced 2025-03-06 20:53:33 +01:00

Improved modularisation of libcommsy

This commit is contained in:
niansa 2020-09-02 22:05:29 +02:00
parent 51bc98e48e
commit 2b0a942557
4 changed files with 37 additions and 36 deletions

View file

@ -16,6 +16,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
curlreq.cpp \
libcommsy.cpp \
main.cpp

28
curlreq.cpp Normal file
View file

@ -0,0 +1,28 @@
#include <sstream>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Infos.hpp>
#include <curlpp/Options.hpp>
long curlreq(std::stringstream &responsebuffer, std::string SID, std::string URL) {
std::cout << "Connection details begin" << std::endl;
std::cout << "URL: " << URL << std::endl;
std::cout << "SID: " << SID << std::endl;
std::cout << "Connection details end" << std::endl;
// Initialise variables
curlpp::Cleanup cleaner;
curlpp::Easy request;
// Set the writer callback to enable cURL to write result in a memory area
request.setOpt(new curlpp::options::WriteStream(&responsebuffer));
// Setting the URL to retrive.
request.setOpt(new curlpp::options::Url(URL));
// Set SID cookie
std::list<std::string> header;
header.push_back("Cookie: SID=" + SID);
request.setOpt(new curlpp::options::HttpHeader(header));
// Perform request
request.perform();
// Return result
return curlpp::infos::ResponseCode::get(request);
}

View file

@ -11,11 +11,6 @@
#include <cstdio>
#include <unistd.h>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Infos.hpp>
#include <curlpp/Options.hpp>
#include <gumbo.h>
#include "libcommsy.hpp"
@ -25,10 +20,6 @@ class parsingNoSuchTagError {};
class descDownloadError {};
static std::string server_url;
static std::string server_sid;
static std::string room;
std::string ltrim(const std::string& s) {
static const std::regex lws{"^[[:space:]]*", std::regex_constants::extended};
@ -59,27 +50,7 @@ std::vector<std::string> merge_strvects(std::vector<std::string> base, const std
return base;
}
static long curlreq(std::stringstream &responsebuffer, std::string SID, std::string URL) {
std::cout << "Connection details begin" << std::endl;
std::cout << "URL: " << URL << std::endl;
std::cout << "SID: " << SID << std::endl;
std::cout << "Connection details end" << std::endl;
// Initialise variables
curlpp::Cleanup cleaner;
curlpp::Easy request;
// Set the writer callback to enable cURL to write result in a memory area
request.setOpt(new curlpp::options::WriteStream(&responsebuffer));
// Setting the URL to retrive.
request.setOpt(new curlpp::options::Url(URL));
// Set SID cookie
std::list<std::string> header;
header.push_back("Cookie: SID=" + SID);
request.setOpt(new curlpp::options::HttpHeader(header));
// Perform request
request.perform();
// Return result
return curlpp::infos::ResponseCode::get(request);
}
extern long curlreq(std::stringstream &responsebuffer, std::string SID, std::string URL);
void gumbo_search_by_attr(std::vector<GumboNode *> &elemvect, GumboNode* node, std::string attrname, std::string searchword, GumboTag expectedtag) {
if (node->type != GUMBO_NODE_ELEMENT) {
@ -292,7 +263,7 @@ std::vector<std::map<std::string, std::string>> get_post_files(GumboNode *node)
return filenameurlmap;
}
std::string get_post_desc(std::string post_url) {
std::string get_post_desc(const std::string& post_url, const std::string& server_sid) {
std::string material_id;
std::stringstream httpcontent;
GumboOutput *post_document;
@ -326,7 +297,7 @@ std::string get_post_desc(std::string post_url) {
}
// Functions
// Class functions
bool libCommsy::postExists(unsigned long postID) {
return postID < numposts;
}
@ -347,18 +318,17 @@ std::string *libCommsy::getDescription(unsigned long postID) {
// Check if post description was downloaded already
if (thispost->description == "\xFF") {
// Download post
thispost->description = get_post_desc(server_url + thispost->url);
thispost->description = get_post_desc(server_url + thispost->url, server_sid);
}
// Return it
return &thispost->description;
}
libCommsy::libCommsy(const std::string& _server_url, const std::string& _server_sid, const std::string& _room, const std::string start_id, const unsigned long max_posts) {
libCommsy::libCommsy(const std::string& _server_url, const std::string& _server_sid, const std::string& room, const std::string start_id, const unsigned long max_posts) {
// Define required variables
server_url = _server_url;
server_sid = _server_sid;
room = _room;
lastID = start_id;
std::stringstream httpcontent;
GumboOutput *document;

View file

@ -26,12 +26,14 @@ struct commsyPost {
#define libCommsy_NAME "libcommsy"
#define libCommsy_VERSION "1.3-stable"
#define libCommsy_VERSION "1.4-stable"
class libCommsy {
public:
std::vector<commsyPost> posts;
unsigned long numposts;
std::string lastID;
std::string server_url;
std::string server_sid;
bool postExists(unsigned long postID);