mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-08-20 21:33:27 -07:00
Encode search results as JSON
This commit is contained in:
parent
f5a93be544
commit
42e5b910ef
2 changed files with 21 additions and 40 deletions
|
@ -31,6 +31,10 @@
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
|
#include <QJsonParseError>
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QJsonValue>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QMetaObject>
|
#include <QMetaObject>
|
||||||
#include <QProcess>
|
#include <QProcess>
|
||||||
|
@ -168,43 +172,27 @@ void SearchHandler::processFailed()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse one line of search results list
|
// 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)
|
bool SearchHandler::parseSearchResult(const QByteArrayView line, SearchResult &searchResult)
|
||||||
{
|
{
|
||||||
const QList<QByteArrayView> parts = Utils::ByteArray::splitToViews(line, "|");
|
const auto jsonDoc = QJsonDocument::fromJson(line.toByteArray());
|
||||||
const int nbFields = parts.size();
|
if (jsonDoc.isNull() || !jsonDoc.isObject())
|
||||||
|
return false;
|
||||||
|
|
||||||
if (nbFields <= PL_ENGINE_URL)
|
const QJsonObject jsonObj = jsonDoc.object();
|
||||||
return false; // Anything after ENGINE_URL is optional
|
|
||||||
|
|
||||||
searchResult = SearchResult();
|
searchResult = SearchResult();
|
||||||
searchResult.fileUrl = QString::fromUtf8(parts.at(PL_DL_LINK).trimmed()); // download URL
|
searchResult.fileUrl = jsonObj[u"link"_s].toString().trimmed(); // download URL
|
||||||
searchResult.fileName = QString::fromUtf8(parts.at(PL_NAME).trimmed()); // Name
|
searchResult.fileName = jsonObj[u"name"_s].toString().trimmed(); // Name
|
||||||
searchResult.fileSize = parts.at(PL_SIZE).trimmed().toLongLong(); // Size
|
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.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
|
searchResult.pubDate = QDateTime::fromSecsSinceEpoch(secs); // Date
|
||||||
}
|
|
||||||
|
searchResult.engineName = m_manager->pluginNameBySiteURL(searchResult.siteUrl); // Search engine name
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
# VERSION: 1.53
|
# VERSION: 2.00
|
||||||
|
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
# modification, are permitted provided that the following conditions are met:
|
# 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
|
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
# POSSIBILITY OF SUCH DAMAGE.
|
# POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
|
||||||
|
import json
|
||||||
import re
|
import re
|
||||||
from typing import TypedDict, Union
|
from typing import TypedDict, Union
|
||||||
|
|
||||||
|
@ -40,16 +41,8 @@ SearchResults = TypedDict('SearchResults', {
|
||||||
|
|
||||||
|
|
||||||
def prettyPrinter(dictionary: SearchResults) -> None:
|
def prettyPrinter(dictionary: SearchResults) -> None:
|
||||||
outtext = "|".join((
|
dictionary['size'] = anySizeToBytes(dictionary['size'])
|
||||||
dictionary["link"],
|
outtext = json.dumps(dictionary)
|
||||||
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
|
|
||||||
))
|
|
||||||
|
|
||||||
# fd 1 is stdout
|
# fd 1 is stdout
|
||||||
with open(1, 'w', encoding='utf-8', closefd=False) as utf8stdout:
|
with open(1, 'w', encoding='utf-8', closefd=False) as utf8stdout:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue