From 080dd79875db652a77e474c5dd7c42f51468373a Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sun, 28 May 2017 14:32:41 +0300 Subject: [PATCH] Store all RSS Article data in auto downloading job --- src/base/rss/rss_article.cpp | 48 ++++++++++++++--------------- src/base/rss/rss_article.h | 9 ++++++ src/base/rss/rss_autodownloader.cpp | 33 +++++++++----------- 3 files changed, 48 insertions(+), 42 deletions(-) diff --git a/src/base/rss/rss_article.cpp b/src/base/rss/rss_article.cpp index c4bfe7b8f..1a5425a1e 100644 --- a/src/base/rss/rss_article.cpp +++ b/src/base/rss/rss_article.cpp @@ -36,47 +36,47 @@ #include "rss_feed.h" -const QString Str_Id(QStringLiteral("id")); -const QString Str_Date(QStringLiteral("date")); -const QString Str_Title(QStringLiteral("title")); -const QString Str_Author(QStringLiteral("author")); -const QString Str_Description(QStringLiteral("description")); -const QString Str_TorrentURL(QStringLiteral("torrentURL")); -const QString Str_Link(QStringLiteral("link")); -const QString Str_IsRead(QStringLiteral("isRead")); - using namespace RSS; +const QString Article::KeyId(QStringLiteral("id")); +const QString Article::KeyDate(QStringLiteral("date")); +const QString Article::KeyTitle(QStringLiteral("title")); +const QString Article::KeyAuthor(QStringLiteral("author")); +const QString Article::KeyDescription(QStringLiteral("description")); +const QString Article::KeyTorrentURL(QStringLiteral("torrentURL")); +const QString Article::KeyLink(QStringLiteral("link")); +const QString Article::KeyIsRead(QStringLiteral("isRead")); + Article::Article(Feed *feed, const QVariantHash &varHash) : QObject(feed) , m_feed(feed) - , m_guid(varHash.value(Str_Id).toString()) - , m_date(varHash.value(Str_Date).toDateTime()) - , m_title(varHash.value(Str_Title).toString()) - , m_author(varHash.value(Str_Author).toString()) - , m_description(varHash.value(Str_Description).toString()) - , m_torrentURL(varHash.value(Str_TorrentURL).toString()) - , m_link(varHash.value(Str_Link).toString()) - , m_isRead(varHash.value(Str_IsRead, false).toBool()) + , m_guid(varHash.value(KeyId).toString()) + , m_date(varHash.value(KeyDate).toDateTime()) + , m_title(varHash.value(KeyTitle).toString()) + , m_author(varHash.value(KeyAuthor).toString()) + , m_description(varHash.value(KeyDescription).toString()) + , m_torrentURL(varHash.value(KeyTorrentURL).toString()) + , m_link(varHash.value(KeyLink).toString()) + , m_isRead(varHash.value(KeyIsRead, false).toBool()) , m_data(varHash) { // If item does not have a guid, fall back to some other identifier if (m_guid.isEmpty()) - m_guid = varHash.value(Str_TorrentURL).toString(); + m_guid = varHash.value(KeyTorrentURL).toString(); if (m_guid.isEmpty()) - m_guid = varHash.value(Str_Title).toString(); + m_guid = varHash.value(KeyTitle).toString(); if (m_guid.isEmpty()) throw std::runtime_error("Bad RSS Article data"); - m_data[Str_Id] = m_guid; + m_data[KeyId] = m_guid; } Article::Article(Feed *feed, const QJsonObject &jsonObj) : Article(feed, jsonObj.toVariantHash()) { // JSON object store DateTime as string so we need to convert it - m_date = QDateTime::fromString(jsonObj.value(Str_Date).toString(), Qt::RFC2822Date); - m_data[Str_Date] = m_date; + m_date = QDateTime::fromString(jsonObj.value(KeyDate).toString(), Qt::RFC2822Date); + m_data[KeyDate] = m_date; } QString Article::guid() const @@ -128,7 +128,7 @@ void Article::markAsRead() { if (!m_isRead) { m_isRead = true; - m_data[Str_IsRead] = m_isRead; + m_data[KeyIsRead] = m_isRead; emit read(this); } } @@ -137,7 +137,7 @@ QJsonObject Article::toJsonObject() const { auto jsonObj = QJsonObject::fromVariantHash(m_data); // JSON object doesn't support DateTime so we need to convert it - jsonObj[Str_Date] = m_date.toString(Qt::RFC2822Date); + jsonObj[KeyDate] = m_date.toString(Qt::RFC2822Date); return jsonObj; } diff --git a/src/base/rss/rss_article.h b/src/base/rss/rss_article.h index 6a9970f88..a77967551 100644 --- a/src/base/rss/rss_article.h +++ b/src/base/rss/rss_article.h @@ -50,6 +50,15 @@ namespace RSS Article(Feed *feed, const QJsonObject &jsonObj); public: + static const QString KeyId; + static const QString KeyDate; + static const QString KeyTitle; + static const QString KeyAuthor; + static const QString KeyDescription; + static const QString KeyTorrentURL; + static const QString KeyLink; + static const QString KeyIsRead; + Feed *feed() const; QString guid() const; QDateTime date() const; diff --git a/src/base/rss/rss_autodownloader.cpp b/src/base/rss/rss_autodownloader.cpp index 0279df675..7707983de 100644 --- a/src/base/rss/rss_autodownloader.cpp +++ b/src/base/rss/rss_autodownloader.cpp @@ -36,6 +36,7 @@ #include #include #include +#include #include "../bittorrent/magneturi.h" #include "../bittorrent/session.h" @@ -54,10 +55,7 @@ struct ProcessingJob { QString feedURL; - QString articleGUID; - QString articleTitle; - QDateTime articleDate; - QString torrentURL; + QVariantHash articleData; }; const QString ConfFolderName(QStringLiteral("rss")); @@ -191,8 +189,8 @@ void AutoDownloader::handleTorrentDownloadFinished(const QString &url) auto job = m_waitingJobs.take(url); if (!job) return; - if (auto feed = Session::instance()->feedByURL(job->feedURL)) - if (auto article = feed->articleByGUID(job->articleGUID)) + if (Feed *feed = Session::instance()->feedByURL(job->feedURL)) + if (Article *article = feed->articleByGUID(job->articleData.value(Article::KeyId).toString())) article->markAsRead(); } @@ -220,10 +218,7 @@ void AutoDownloader::addJobForArticle(Article *article) QSharedPointer job(new ProcessingJob); job->feedURL = article->feed()->url(); - job->articleGUID = article->guid(); - job->articleTitle = article->title(); - job->articleDate = article->date(); - job->torrentURL = torrentURL; + job->articleData = article->data(); m_processingQueue.append(job); if (!m_processingTimer->isActive()) m_processingTimer->start(); @@ -234,17 +229,18 @@ void AutoDownloader::processJob(const QSharedPointer &job) for (AutoDownloadRule &rule: m_rules) { if (!rule.isEnabled()) continue; if (!rule.feedURLs().contains(job->feedURL)) continue; - if (!rule.matches(job->articleTitle)) continue; + if (!rule.matches(job->articleData.value(Article::KeyTitle).toString())) continue; + auto articleDate = job->articleData.value(Article::KeyDate).toDateTime(); // if rule is in ignoring state do nothing with matched torrent if (rule.ignoreDays() > 0) { if (rule.lastMatch().isValid()) { - if (job->articleDate < rule.lastMatch().addDays(rule.ignoreDays())) + if (articleDate < rule.lastMatch().addDays(rule.ignoreDays())) return; } } - rule.setLastMatch(job->articleDate); + rule.setLastMatch(articleDate); m_dirty = true; storeDeferred(); @@ -252,18 +248,19 @@ void AutoDownloader::processJob(const QSharedPointer &job) params.savePath = rule.savePath(); params.category = rule.assignedCategory(); params.addPaused = rule.addPaused(); - BitTorrent::Session::instance()->addTorrent(job->torrentURL, params); + auto torrentURL = job->articleData.value(Article::KeyTorrentURL).toString(); + BitTorrent::Session::instance()->addTorrent(torrentURL, params); - if (BitTorrent::MagnetUri(job->torrentURL).isValid()) { - if (auto feed = Session::instance()->feedByURL(job->feedURL)) { - if (auto article = feed->articleByGUID(job->articleGUID)) + if (BitTorrent::MagnetUri(torrentURL).isValid()) { + if (Feed *feed = Session::instance()->feedByURL(job->feedURL)) { + if (Article *article = feed->articleByGUID(job->articleData.value(Article::KeyId).toString())) article->markAsRead(); } } else { // waiting for torrent file downloading // normalize URL string via QUrl since DownloadManager do it - m_waitingJobs.insert(QUrl(job->torrentURL).toString(), job); + m_waitingJobs.insert(QUrl(torrentURL).toString(), job); } return;