Version alpha0
This commit is contained in:
commit
28d101b2be
|
@ -0,0 +1,24 @@
|
|||
cmake_minimum_required(VERSION 3.14)
|
||||
project(testCurlPP LANGUAGES CXX)
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
set(CMAKE_CXX_STANDARD 11)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
include(FindPkgConfig)
|
||||
pkg_check_modules(CURLPP REQUIRED curlpp)
|
||||
|
||||
add_executable(fetch_from_ghost
|
||||
fetch_from_ghost.cc
|
||||
)
|
||||
|
||||
add_executable(parse_posts
|
||||
parse_posts.cc
|
||||
)
|
||||
|
||||
add_executable(parse_pages
|
||||
parse_pages.cc
|
||||
)
|
||||
|
||||
target_link_libraries(fetch_from_ghost
|
||||
${CURLPP_LDFLAGS}
|
||||
)
|
|
@ -0,0 +1,9 @@
|
|||
You need libcurlpp and rapidjson to build
|
||||
To build, please do
|
||||
|
||||
mkdir build
|
||||
cd build
|
||||
cmake ..
|
||||
make
|
||||
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
#include <curlpp/cURLpp.hpp>
|
||||
#include <curlpp/Easy.hpp>
|
||||
#include <curlpp/Options.hpp>
|
||||
#include <curlpp/Exception.hpp>
|
||||
|
||||
#define DEBUG_LEVEL -1
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if(argc != 3) {
|
||||
std::cerr<< argv[0] << ": Usage: " << " apiurl contentkey" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
char url_posts[128], url_pages[128];
|
||||
|
||||
snprintf(url_posts, sizeof url_posts, "%s/ghost/api/content/posts/?key=%s&limit=all", argv[1], argv[2]);
|
||||
snprintf(url_pages, sizeof url_pages, "%s/ghost/api/content/pages/?key=%s", argv[1], argv[2]);
|
||||
if (DEBUG_LEVEL > 0) std::cout<<"urls to fetch are "<<std::endl<<url_posts<<std::endl<<url_pages<<std::endl;
|
||||
|
||||
try {
|
||||
curlpp::Cleanup cleaner;
|
||||
curlpp::Easy request1, request2;
|
||||
|
||||
// Setting the URL to retrive.
|
||||
request1.setOpt(new curlpp::options::Url(url_posts));
|
||||
|
||||
if (DEBUG_LEVEL > 0) std::cout << request1 << std::endl;
|
||||
|
||||
std::ofstream outfile;
|
||||
outfile.open ("posts.json");
|
||||
outfile << curlpp::options::Url(url_posts) << std::endl;
|
||||
outfile.close();
|
||||
|
||||
request2.setOpt(new curlpp::options::Url(url_pages));
|
||||
if (DEBUG_LEVEL > 0) std::cout << request2 << std::endl;
|
||||
|
||||
outfile.open ("pages.json");
|
||||
outfile << curlpp::options::Url(url_pages) << std::endl;
|
||||
outfile.close();
|
||||
|
||||
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
catch ( curlpp::LogicError & e ) {
|
||||
if (DEBUG_LEVEL > 0) std::cout << e.what() << std::endl;
|
||||
}
|
||||
catch ( curlpp::RuntimeError & e ) {
|
||||
if (DEBUG_LEVEL > 0) std::cout << e.what() << std::endl;
|
||||
}
|
||||
|
||||
return EXIT_FAILURE;
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/filereadstream.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Open the file
|
||||
FILE* fp = fopen("pages.json", "rb");
|
||||
// Check if the file was opened successfully
|
||||
if (!fp) {
|
||||
std::cerr << "Error: unable to open file"
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Read the file
|
||||
char readBuffer[65536];
|
||||
rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
|
||||
|
||||
// Parse the JSON document
|
||||
rapidjson::Document doc;
|
||||
doc.ParseStream(is);
|
||||
|
||||
// Check if the document is valid
|
||||
if (doc.HasParseError()) {
|
||||
std::cerr << "Error: failed to parse JSON document"
|
||||
<< std::endl;
|
||||
fclose(fp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Close the file
|
||||
fclose(fp);
|
||||
|
||||
if (doc.HasMember("pages")
|
||||
&& doc["pages"].IsArray()) {
|
||||
const rapidjson::Value& pages = doc["pages"];
|
||||
system("mkdir content");
|
||||
for (rapidjson::SizeType i = 0; i < pages.Size();
|
||||
i++) {
|
||||
if (pages[i].IsObject()) {
|
||||
auto post = pages[i].GetObject();
|
||||
std::string postid = post["id"].GetString();
|
||||
std::cout<< postid <<": "<<post["title"].GetString() <<std::endl;
|
||||
char filename[64];
|
||||
snprintf(filename, sizeof filename, "content/page-%s.html", postid.c_str());
|
||||
std::ofstream post_file;
|
||||
post_file.open(filename);
|
||||
post_file << "<html>";
|
||||
post_file << post["html"].GetString();
|
||||
post_file <<"</html>";
|
||||
post_file.close();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
#include <rapidjson/document.h>
|
||||
#include <rapidjson/filereadstream.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
|
||||
int main()
|
||||
{
|
||||
// Open the file
|
||||
FILE* fp = fopen("posts.json", "rb");
|
||||
// Check if the file was opened successfully
|
||||
if (!fp) {
|
||||
std::cerr << "Error: unable to open file"
|
||||
<< std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Read the file
|
||||
char readBuffer[65536];
|
||||
rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
|
||||
|
||||
// Parse the JSON document
|
||||
rapidjson::Document doc;
|
||||
doc.ParseStream(is);
|
||||
|
||||
// Check if the document is valid
|
||||
if (doc.HasParseError()) {
|
||||
std::cerr << "Error: failed to parse JSON document"
|
||||
<< std::endl;
|
||||
fclose(fp);
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Close the file
|
||||
fclose(fp);
|
||||
|
||||
if (doc.HasMember("posts")
|
||||
&& doc["posts"].IsArray()) {
|
||||
const rapidjson::Value& posts = doc["posts"];
|
||||
system("mkdir content");
|
||||
for (rapidjson::SizeType i = 0; i < posts.Size();
|
||||
i++) {
|
||||
if (posts[i].IsObject()) {
|
||||
auto post = posts[i].GetObject();
|
||||
std::string postid = post["id"].GetString();
|
||||
std::cout<< postid <<": "<<post["title"].GetString() <<std::endl;
|
||||
char filename[64];
|
||||
snprintf(filename, sizeof filename, "content/%s.html", postid.c_str());
|
||||
std::ofstream post_file;
|
||||
post_file.open(filename);
|
||||
post_file << "<html>";
|
||||
post_file << post["html"].GetString();
|
||||
post_file <<"</html>";
|
||||
post_file.close();
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue