From 42e5b910ef42584e7ee90d8ac522f48e8aa99365 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sun, 29 Jun 2025 19:38:11 +0300 Subject: [PATCH] Encode search results as JSON --- src/base/search/searchhandler.cpp | 46 ++++++++++----------------- src/searchengine/nova3/novaprinter.py | 15 +++------ 2 files changed, 21 insertions(+), 40 deletions(-) diff --git a/src/base/search/searchhandler.cpp b/src/base/search/searchhandler.cpp index d1d9fb4b7..4a5741938 100644 --- a/src/base/search/searchhandler.cpp +++ b/src/base/search/searchhandler.cpp @@ -31,6 +31,10 @@ #include +#include +#include +#include +#include #include #include #include @@ -168,44 +172,28 @@ void SearchHandler::processFailed() } // Parse one line of search results list -// Line is in the following form: -// file url | file name | file size | nb seeds | nb leechers | Search engine url bool SearchHandler::parseSearchResult(const QByteArrayView line, SearchResult &searchResult) { - const QList parts = Utils::ByteArray::splitToViews(line, "|"); - const int nbFields = parts.size(); + const auto jsonDoc = QJsonDocument::fromJson(line.toByteArray()); + if (jsonDoc.isNull() || !jsonDoc.isObject()) + return false; - if (nbFields <= PL_ENGINE_URL) - return false; // Anything after ENGINE_URL is optional + const QJsonObject jsonObj = jsonDoc.object(); searchResult = SearchResult(); - searchResult.fileUrl = QString::fromUtf8(parts.at(PL_DL_LINK).trimmed()); // download URL - searchResult.fileName = QString::fromUtf8(parts.at(PL_NAME).trimmed()); // Name - searchResult.fileSize = parts.at(PL_SIZE).trimmed().toLongLong(); // Size + searchResult.fileUrl = jsonObj[u"link"_s].toString().trimmed(); // download URL + searchResult.fileName = jsonObj[u"name"_s].toString().trimmed(); // Name + searchResult.fileSize = jsonObj[u"size"_s].toInteger(); // Size + searchResult.nbSeeders = jsonObj[u"seeds"_s].toInteger(-1); // Seeders + searchResult.nbLeechers = jsonObj[u"leech"_s].toInteger(-1); // Leechers + searchResult.siteUrl = jsonObj[u"engine_url"_s].toString().trimmed(); // Search engine site URL + searchResult.descrLink = jsonObj[u"desc_link"_s].toString().trimmed(); // Description Link - bool ok = false; + if (const qint64 secs = jsonObj[u"pub_date"_s].toInteger(); secs > 0) + searchResult.pubDate = QDateTime::fromSecsSinceEpoch(secs); // Date - searchResult.nbSeeders = parts.at(PL_SEEDS).trimmed().toLongLong(&ok); // Seeders - if (!ok || (searchResult.nbSeeders < 0)) - searchResult.nbSeeders = -1; - - searchResult.nbLeechers = parts.at(PL_LEECHS).trimmed().toLongLong(&ok); // Leechers - if (!ok || (searchResult.nbLeechers < 0)) - searchResult.nbLeechers = -1; - - searchResult.siteUrl = QString::fromUtf8(parts.at(PL_ENGINE_URL).trimmed()); // Search engine site URL searchResult.engineName = m_manager->pluginNameBySiteURL(searchResult.siteUrl); // Search engine name - if (nbFields > PL_DESC_LINK) - searchResult.descrLink = QString::fromUtf8(parts.at(PL_DESC_LINK).trimmed()); // Description Link - - if (nbFields > PL_PUB_DATE) - { - const qint64 secs = parts.at(PL_PUB_DATE).trimmed().toLongLong(&ok); - if (ok && (secs > 0)) - searchResult.pubDate = QDateTime::fromSecsSinceEpoch(secs); // Date - } - return true; } diff --git a/src/searchengine/nova3/novaprinter.py b/src/searchengine/nova3/novaprinter.py index 812b0cd95..78fa198b4 100644 --- a/src/searchengine/nova3/novaprinter.py +++ b/src/searchengine/nova3/novaprinter.py @@ -1,4 +1,4 @@ -# VERSION: 1.53 +# VERSION: 2.00 # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: @@ -24,6 +24,7 @@ # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. +import json import re from typing import TypedDict, Union @@ -40,16 +41,8 @@ SearchResults = TypedDict('SearchResults', { def prettyPrinter(dictionary: SearchResults) -> None: - outtext = "|".join(( - dictionary["link"], - dictionary["name"].replace("|", " "), - str(anySizeToBytes(dictionary['size'])), - str(dictionary["seeds"]), - str(dictionary["leech"]), - dictionary["engine_url"], - dictionary.get("desc_link", ""), # Optional - str(dictionary.get("pub_date", -1)) # Optional - )) + dictionary['size'] = anySizeToBytes(dictionary['size']) + outtext = json.dumps(dictionary) # fd 1 is stdout with open(1, 'w', encoding='utf-8', closefd=False) as utf8stdout: