diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/DownloadUtils.cpp | 5 | ||||
-rw-r--r-- | src/plugins/Mangadex.cpp | 71 | ||||
-rw-r--r-- | src/plugins/Matrix.cpp | 1 |
3 files changed, 32 insertions, 45 deletions
diff --git a/src/DownloadUtils.cpp b/src/DownloadUtils.cpp index 4f8b5b9..5b39bdd 100644 --- a/src/DownloadUtils.cpp +++ b/src/DownloadUtils.cpp @@ -4,7 +4,6 @@ #include "../include/base64_url.hpp" #include <SFML/System/Clock.hpp> #include <rapidjson/filereadstream.h> -#include <unistd.h> static const bool debug_download = false; @@ -107,6 +106,7 @@ namespace QuickMedia { } fprintf(stderr, "\n"); } + ReadProgram read_program; if(exec_program_pipe(args.data(), &read_program) != 0) return DownloadResult::NET_ERR; @@ -116,10 +116,9 @@ namespace QuickMedia { rapidjson::FileReadStream is(file, read_buffer, sizeof(read_buffer)); rapidjson::ParseResult parse_result = result.ParseStream(is); - wait_program(read_program.pid); fclose(file); + wait_program(read_program.pid); fprintf(stderr, "Download duration for %s: %d ms\n", url.c_str(), timer.getElapsedTime().asMilliseconds()); - return parse_result.IsError() ? DownloadResult::ERR : DownloadResult::OK; } }
\ No newline at end of file diff --git a/src/plugins/Mangadex.cpp b/src/plugins/Mangadex.cpp index be2d342..f0e163e 100644 --- a/src/plugins/Mangadex.cpp +++ b/src/plugins/Mangadex.cpp @@ -2,11 +2,21 @@ #include "../../include/Storage.hpp" #include "../../include/Notification.hpp" #include "../../include/StringUtils.hpp" +#include <rapidjson/document.h> +#include <rapidjson/reader.h> #include <quickmedia/HtmlSearch.h> #include <json/reader.h> static const std::string mangadex_url = "https://mangadex.org"; +static rapidjson::Value nullValue(rapidjson::kNullType); +static const rapidjson::Value& GetMember(const rapidjson::Value &obj, const char *key) { + auto it = obj.FindMember(key); + if(it != obj.MemberEnd()) + return it->value; + return nullValue; +} + namespace QuickMedia { static std::string title_url_extract_manga_id(const std::string &url) { size_t find_index = url.find("/title/"); @@ -104,74 +114,53 @@ namespace QuickMedia { std::string manga_id = title_url_extract_manga_id(url); std::string request_url = "https://mangadex.org/api/?id=" + manga_id + "&type=manga"; - Json::Value json_root; - DownloadResult result = download_json(json_root, request_url, {}, true); + rapidjson::Document json_root; + DownloadResult result = download_to_json(request_url, json_root, {}, is_tor_enabled(), true); if(result != DownloadResult::OK) return download_result_to_plugin_result(result); - if(!json_root.isObject()) + if(!json_root.IsObject()) return PluginResult::ERR; - Json::Value &status_json = json_root["status"]; - if(!status_json.isString() || status_json.asString() != "OK") + const rapidjson::Value &status_json = GetMember(json_root, "status"); + if(!status_json.IsString() || strcmp(status_json.GetString(), "OK") != 0) return PluginResult::ERR; - Json::Value &chapter_json = json_root["chapter"]; - if(!chapter_json.isObject()) + const rapidjson::Value &chapter_json = GetMember(json_root, "chapter"); + if(!chapter_json.IsObject()) return PluginResult::ERR; - std::vector<std::pair<std::string, Json::Value>> chapters(chapter_json.size()); - /* TODO: Optimize member access */ - for(auto &member_name : chapter_json.getMemberNames()) { - Json::Value chapter = chapter_json[member_name]; - if(chapter.isObject()) - chapters.push_back(std::make_pair(member_name, std::move(chapter))); - } - - std::sort(chapters.begin(), chapters.end(), [](std::pair<std::string, Json::Value> &a, std::pair<std::string, Json::Value> &b) { - Json::Value &a_timestamp_json = a.second["timestamp"]; - Json::Value &b_timestamp_json = b.second["timestamp"]; - int64_t a_timestamp = 0; - int64_t b_timestamp = 0; - if(a_timestamp_json.isInt64()) - a_timestamp = a_timestamp_json.asInt64(); - if(b_timestamp_json.isInt64()) - b_timestamp = b_timestamp_json.asInt64(); - return a_timestamp > b_timestamp; - }); - time_t time_now = time(NULL); - auto body = create_body(); /* TODO: Pointer */ std::string prev_chapter_number; - for(auto it = chapters.begin(); it != chapters.end(); ++it) { - const std::string &chapter_id = it->first; - Json::Value &chapter = it->second; + for(auto const &it : chapter_json.GetObject()) { + const std::string &chapter_id = it.name.GetString(); + const rapidjson::Value &chapter = it.value; - Json::Value ×tamp_json = chapter["timestamp"]; - if(timestamp_json.isInt64() && timestamp_json.asInt64() > time_now) + const rapidjson::Value ×tamp_json = chapter["timestamp"]; + if(timestamp_json.IsNumber() && timestamp_json.GetInt64() > time_now) continue; - Json::Value &lang_code = chapter["lang_code"]; + const rapidjson::Value &lang_code = chapter["lang_code"]; // TODO: Allow selecting other languages than english - if(!lang_code.isString() || lang_code.asString() != "gb") + if(!lang_code.IsString() || strcmp(lang_code.GetString(), "gb") != 0) continue; - Json::Value &chapter_number_json = chapter["chapter"]; - if(!chapter_number_json.isString()) + const rapidjson::Value &chapter_number_json = chapter["chapter"]; + if(!chapter_number_json.IsString()) continue; - std::string chapter_number_str = chapter_number_json.asString(); + std::string chapter_number_str = chapter_number_json.GetString(); if(chapter_number_str == prev_chapter_number) continue; prev_chapter_number = chapter_number_str; - Json::Value &chapter_title_json = chapter["title"]; + const rapidjson::Value &chapter_title_json = chapter["title"]; std::string chapter_url = mangadex_url + "/chapter/" + chapter_id; std::string chapter_name = std::string("Ch. ") + chapter_number_str; - if(chapter_title_json.isString() && strlen(chapter_title_json.asCString()) > 0) - chapter_name += std::string(" - ") + chapter_title_json.asCString(); + if(chapter_title_json.IsString() && chapter_title_json.GetStringLength() > 0) + chapter_name += std::string(" - ") + chapter_title_json.GetString(); auto item = BodyItem::create(std::move(chapter_name)); item->url = std::move(chapter_url); diff --git a/src/plugins/Matrix.cpp b/src/plugins/Matrix.cpp index a0d8568..18a31a0 100644 --- a/src/plugins/Matrix.cpp +++ b/src/plugins/Matrix.cpp @@ -22,7 +22,6 @@ static const char* SERVICE_NAME = "matrix"; static rapidjson::Value nullValue(rapidjson::kNullType); - static const rapidjson::Value& GetMember(const rapidjson::Value &obj, const char *key) { auto it = obj.FindMember(key); if(it != obj.MemberEnd()) |