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:
parent
51bc98e48e
commit
2b0a942557
4 changed files with 37 additions and 36 deletions
|
@ -16,6 +16,7 @@ DEFINES += QT_DEPRECATED_WARNINGS
|
||||||
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
|
||||||
|
|
||||||
SOURCES += \
|
SOURCES += \
|
||||||
|
curlreq.cpp \
|
||||||
libcommsy.cpp \
|
libcommsy.cpp \
|
||||||
main.cpp
|
main.cpp
|
||||||
|
|
||||||
|
|
28
curlreq.cpp
Normal file
28
curlreq.cpp
Normal 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);
|
||||||
|
}
|
|
@ -11,11 +11,6 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
#include <curlpp/cURLpp.hpp>
|
|
||||||
#include <curlpp/Easy.hpp>
|
|
||||||
#include <curlpp/Infos.hpp>
|
|
||||||
#include <curlpp/Options.hpp>
|
|
||||||
|
|
||||||
#include <gumbo.h>
|
#include <gumbo.h>
|
||||||
|
|
||||||
#include "libcommsy.hpp"
|
#include "libcommsy.hpp"
|
||||||
|
@ -25,10 +20,6 @@ class parsingNoSuchTagError {};
|
||||||
class descDownloadError {};
|
class descDownloadError {};
|
||||||
|
|
||||||
|
|
||||||
static std::string server_url;
|
|
||||||
static std::string server_sid;
|
|
||||||
static std::string room;
|
|
||||||
|
|
||||||
|
|
||||||
std::string ltrim(const std::string& s) {
|
std::string ltrim(const std::string& s) {
|
||||||
static const std::regex lws{"^[[:space:]]*", std::regex_constants::extended};
|
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;
|
return base;
|
||||||
}
|
}
|
||||||
|
|
||||||
static long curlreq(std::stringstream &responsebuffer, std::string SID, std::string URL) {
|
extern 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
void gumbo_search_by_attr(std::vector<GumboNode *> &elemvect, GumboNode* node, std::string attrname, std::string searchword, GumboTag expectedtag) {
|
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) {
|
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;
|
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::string material_id;
|
||||||
std::stringstream httpcontent;
|
std::stringstream httpcontent;
|
||||||
GumboOutput *post_document;
|
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) {
|
bool libCommsy::postExists(unsigned long postID) {
|
||||||
return postID < numposts;
|
return postID < numposts;
|
||||||
}
|
}
|
||||||
|
@ -347,18 +318,17 @@ std::string *libCommsy::getDescription(unsigned long postID) {
|
||||||
// Check if post description was downloaded already
|
// Check if post description was downloaded already
|
||||||
if (thispost->description == "\xFF") {
|
if (thispost->description == "\xFF") {
|
||||||
// Download post
|
// 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 it
|
||||||
return &thispost->description;
|
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
|
// Define required variables
|
||||||
server_url = _server_url;
|
server_url = _server_url;
|
||||||
server_sid = _server_sid;
|
server_sid = _server_sid;
|
||||||
room = _room;
|
|
||||||
lastID = start_id;
|
lastID = start_id;
|
||||||
std::stringstream httpcontent;
|
std::stringstream httpcontent;
|
||||||
GumboOutput *document;
|
GumboOutput *document;
|
||||||
|
|
|
@ -26,12 +26,14 @@ struct commsyPost {
|
||||||
|
|
||||||
|
|
||||||
#define libCommsy_NAME "libcommsy"
|
#define libCommsy_NAME "libcommsy"
|
||||||
#define libCommsy_VERSION "1.3-stable"
|
#define libCommsy_VERSION "1.4-stable"
|
||||||
class libCommsy {
|
class libCommsy {
|
||||||
public:
|
public:
|
||||||
std::vector<commsyPost> posts;
|
std::vector<commsyPost> posts;
|
||||||
unsigned long numposts;
|
unsigned long numposts;
|
||||||
std::string lastID;
|
std::string lastID;
|
||||||
|
std::string server_url;
|
||||||
|
std::string server_sid;
|
||||||
|
|
||||||
bool postExists(unsigned long postID);
|
bool postExists(unsigned long postID);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue