remarkable_tool/main.cpp
King_DuckZ 7548688eda First import
List all books with associated info
2021-03-25 16:12:46 +01:00

104 lines
2.9 KiB
C++

#include <iostream>
#include <rmlab/rmlab.hpp>
#include <simdjson.h>
#include <string_view>
#include <filesystem>
#include <vector>
#include <unordered_map>
#include <cstdint>
namespace fs = std::filesystem;
namespace {
#define BASE_PATH "/run/media/duckz/Sabrent/deleme/remarkable_20210324/xochitl/"
//#define BASE_PATH "/home/duckz/dev/code/cpp/remarkable_tool/lib/lines-are-beautiful/share/rmlab/examples/"
struct NotebookInfo {
std::string visible_name;
std::string parent;
std::string type;
std::vector<std::string> files;
std::uint64_t version = 0;
std::uintmax_t total_size = 0;
bool deleted = false;
};
std::ostream& operator<< (std::ostream& str, const std::pair<std::string, NotebookInfo>& v) {
auto& key = v.first;
auto& val = v.second;
str << key << " (\"" << val.visible_name << "\", " << val.type <<
"): deleted=" << std::boolalpha << val.deleted <<
", parent=\"" << val.parent <<
"\", version=" << val.version <<
", files=";
std::string_view sep("");
for (const auto& f : val.files) {
str << sep << f;
sep = ";";
}
str << ", size=" << std::setprecision(2) <<
static_cast<double>(val.total_size) / (1024.0 * 1024.0) << "MiB";
return str;
}
std::unordered_map<std::string, NotebookInfo> build_notebook_infos (
const fs::path& from_path
) {
simdjson::dom::parser parser;
std::unordered_map<std::string, NotebookInfo> retval;
for (const auto& entry : fs::directory_iterator(from_path)) {
NotebookInfo& curr_info = retval[entry.path().stem()];
if (entry.path().extension() == ".metadata") {
simdjson::dom::object json_doc = parser.load(entry.path());
curr_info.deleted = json_doc["deleted"];
curr_info.parent = json_doc["parent"];
curr_info.type = json_doc["type"];
curr_info.version = json_doc["version"];
curr_info.visible_name = json_doc["visibleName"];
}
else if (fs::is_directory(entry)) {
for (const auto& subentry : fs::recursive_directory_iterator(entry)) {
curr_info.files.push_back(fs::relative(subentry, from_path));
if (fs::is_regular_file(subentry))
curr_info.total_size += fs::file_size(subentry);
}
}
curr_info.files.push_back(fs::relative(entry, from_path));
if (fs::is_regular_file(entry))
curr_info.total_size += fs::file_size(entry);
}
return retval;
}
} //unnamed namespace
int main() {
using std::string_view;
rmlab::Notebook doc(BASE_PATH "a4bcfe83-a91d-485b-b27c-2a06aa287641");
//rmlab::Notebook doc(BASE_PATH "aa90b0e7-5c1a-42fe-930f-dad9cf3363cc");
std::cout << "Document version: " << doc.version << '\n';
auto notebooks = build_notebook_infos(BASE_PATH);
int z = 0;
std::uintmax_t total = 0;
for (auto& nb : notebooks) {
std::cout << nb << '\n';
std::cout << "------ " << z << "-----\n";
++z;
total += nb.second.total_size;
}
std::cout << "Found " << notebooks.size() << " notebooks\n";
std::cout << "Total size " << std::setprecision(2) <<
static_cast<double>(total) / (1024.0 * 1024.0 * 1024.0) << "GiB\n";
return 0;
}