Skip to content

Commit

Permalink
Add date column to the built-in search engine
Browse files Browse the repository at this point in the history
Adds a date column to the built-in search engine to show when a torrent was published/uploaded on the engine site.
When a plugin wants to show a date, it can now add a `pub_date` entry to its result dict. The value format is a unix timestamp (an integer representing seconds since epoch).
Plugins with no date support will keep working.

PR #20703.
  • Loading branch information
ducalex committed Apr 29, 2024
1 parent 775b380 commit 42b8796
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 10 deletions.
13 changes: 11 additions & 2 deletions src/base/search/searchhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ namespace
PL_LEECHS,
PL_ENGINE_URL,
PL_DESC_LINK,
PL_PUB_DATE,
NB_PLUGIN_COLUMNS
};
}
Expand Down Expand Up @@ -176,7 +177,7 @@ bool SearchHandler::parseSearchResult(const QStringView line, SearchResult &sear
const QList<QStringView> parts = line.split(u'|');
const int nbFields = parts.size();

if (nbFields < (NB_PLUGIN_COLUMNS - 1)) return false; // -1 because desc_link is optional
if (nbFields <= PL_ENGINE_URL) return false; // Anything after ENGINE_URL is optional

searchResult = SearchResult();
searchResult.fileUrl = parts.at(PL_DL_LINK).trimmed().toString(); // download URL
Expand All @@ -194,9 +195,17 @@ bool SearchHandler::parseSearchResult(const QStringView line, SearchResult &sear
searchResult.nbLeechers = -1;

searchResult.siteUrl = parts.at(PL_ENGINE_URL).trimmed().toString(); // Search site URL
if (nbFields == NB_PLUGIN_COLUMNS)

if (nbFields > PL_DESC_LINK)
searchResult.descrLink = parts.at(PL_DESC_LINK).trimmed().toString(); // 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;
}

Expand Down
2 changes: 2 additions & 0 deletions src/base/search/searchhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#pragma once

#include <QByteArray>
#include <QDateTime>
#include <QList>
#include <QObject>
#include <QString>
Expand All @@ -47,6 +48,7 @@ struct SearchResult
qlonglong nbLeechers = 0;
QString siteUrl;
QString descrLink;
QDateTime pubDate;
};

class SearchPluginManager;
Expand Down
2 changes: 2 additions & 0 deletions src/gui/search/searchjobwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ SearchJobWidget::SearchJobWidget(SearchHandler *searchHandler, IGUIApplication *
m_searchListModel->setHeaderData(SearchSortModel::SEEDS, Qt::Horizontal, tr("Seeders", "i.e: Number of full sources"));
m_searchListModel->setHeaderData(SearchSortModel::LEECHES, Qt::Horizontal, tr("Leechers", "i.e: Number of partial sources"));
m_searchListModel->setHeaderData(SearchSortModel::ENGINE_URL, Qt::Horizontal, tr("Search engine"));
m_searchListModel->setHeaderData(SearchSortModel::PUB_DATE, Qt::Horizontal, tr("Published On"));
// Set columns text alignment
m_searchListModel->setHeaderData(SearchSortModel::SIZE, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
m_searchListModel->setHeaderData(SearchSortModel::SEEDS, Qt::Horizontal, QVariant(Qt::AlignRight | Qt::AlignVCenter), Qt::TextAlignmentRole);
Expand Down Expand Up @@ -533,6 +534,7 @@ void SearchJobWidget::appendSearchResults(const QVector<SearchResult> &results)
setModelData(SearchSortModel::SIZE, Utils::Misc::friendlyUnit(result.fileSize), result.fileSize, (Qt::AlignRight | Qt::AlignVCenter));
setModelData(SearchSortModel::SEEDS, QString::number(result.nbSeeders), result.nbSeeders, (Qt::AlignRight | Qt::AlignVCenter));
setModelData(SearchSortModel::LEECHES, QString::number(result.nbLeechers), result.nbLeechers, (Qt::AlignRight | Qt::AlignVCenter));
setModelData(SearchSortModel::PUB_DATE, QLocale().toString(result.pubDate.toLocalTime(), QLocale::ShortFormat), result.pubDate);
}

updateResultsCount();
Expand Down
1 change: 1 addition & 0 deletions src/gui/search/searchsortmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class SearchSortModel final : public QSortFilterProxyModel
SEEDS,
LEECHES,
ENGINE_URL,
PUB_DATE,
DL_LINK,
DESC_LINK,
NB_SEARCH_COLUMNS
Expand Down
18 changes: 11 additions & 7 deletions src/searchengine/nova3/novaprinter.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#VERSION: 1.47
#VERSION: 1.48

# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Expand Down Expand Up @@ -26,12 +26,16 @@


def prettyPrinter(dictionary):
dictionary['size'] = anySizeToBytes(dictionary['size'])
outtext = "|".join((dictionary["link"], dictionary["name"].replace("|", " "),
str(dictionary["size"]), str(dictionary["seeds"]),
str(dictionary["leech"]), dictionary["engine_url"]))
if 'desc_link' in dictionary:
outtext = "|".join((outtext, dictionary["desc_link"]))
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
))

# fd 1 is stdout
with open(1, 'w', encoding='utf-8', closefd=False) as utf8stdout:
Expand Down
5 changes: 4 additions & 1 deletion src/webui/api/searchcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "base/global.h"
#include "base/logger.h"
#include "base/search/searchhandler.h"
#include "base/utils/datetime.h"
#include "base/utils/foreignapps.h"
#include "base/utils/random.h"
#include "base/utils/string.h"
Expand Down Expand Up @@ -301,6 +302,7 @@ int SearchController::generateSearchId() const
* - "nbLeechers"
* - "siteUrl"
* - "descrLink"
* - "pubDate"
*/
QJsonObject SearchController::getResults(const QList<SearchResult> &searchResults, const bool isSearchActive, const int totalResults) const
{
Expand All @@ -315,7 +317,8 @@ QJsonObject SearchController::getResults(const QList<SearchResult> &searchResult
{u"nbSeeders"_s, searchResult.nbSeeders},
{u"nbLeechers"_s, searchResult.nbLeechers},
{u"siteUrl"_s, searchResult.siteUrl},
{u"descrLink"_s, searchResult.descrLink}
{u"descrLink"_s, searchResult.descrLink},
{u"pubDate"_s, Utils::DateTime::toSecsSinceEpoch(searchResult.pubDate)}
};
}

Expand Down
8 changes: 8 additions & 0 deletions src/webui/www/private/scripts/dynamicTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,7 @@ window.qBittorrent.DynamicTable = (function() {
this.newColumn('nbSeeders', '', 'QBT_TR(Seeders)QBT_TR[CONTEXT=SearchResultsTable]', 100, true);
this.newColumn('nbLeechers', '', 'QBT_TR(Leechers)QBT_TR[CONTEXT=SearchResultsTable]', 100, true);
this.newColumn('siteUrl', '', 'QBT_TR(Search engine)QBT_TR[CONTEXT=SearchResultsTable]', 250, true);
this.newColumn('pubDate', '', 'QBT_TR(Published On)QBT_TR[CONTEXT=SearchResultsTable]', 200, true);

this.initColumnsFunctions();
},
Expand All @@ -1701,10 +1702,17 @@ window.qBittorrent.DynamicTable = (function() {
td.set('text', formattedValue);
td.set('title', formattedValue);
};
const displayDate = function(td, row) {
const value = this.getRowValue(row) * 1000;
const formattedValue = (isNaN(value) || (value <= 0)) ? "" : (new Date(value).toLocaleString());
td.set('text', formattedValue);
td.set('title', formattedValue);
};

this.columns['fileSize'].updateTd = displaySize;
this.columns['nbSeeders'].updateTd = displayNum;
this.columns['nbLeechers'].updateTd = displayNum;
this.columns['pubDate'].updateTd = displayDate;
},

getFilteredAndSortedRows: function() {
Expand Down
1 change: 1 addition & 0 deletions src/webui/www/private/views/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@
nbLeechers: result.nbLeechers,
nbSeeders: result.nbSeeders,
siteUrl: result.siteUrl,
pubDate: result.pubDate,
};

newRows.push(row);
Expand Down

0 comments on commit 42b8796

Please sign in to comment.