2010-10-31 15:35:07 +03:00
|
|
|
/*
|
2015-10-14 12:49:29 +03:00
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2010-10-31 15:35:07 +03:00
|
|
|
* Copyright (C) 2010 Christophe Dumez
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QRegExp>
|
2010-11-13 18:14:50 +03:00
|
|
|
#include <QDebug>
|
2014-07-05 16:44:13 +04:00
|
|
|
#include <QDir>
|
2010-10-31 15:35:07 +03:00
|
|
|
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/preferences.h"
|
2015-10-14 12:31:44 +03:00
|
|
|
#include "base/utils/fs.h"
|
2010-11-13 22:36:46 +03:00
|
|
|
#include "rssfeed.h"
|
|
|
|
#include "rssarticle.h"
|
2015-10-14 12:31:44 +03:00
|
|
|
#include "rssdownloadrule.h"
|
2010-10-31 15:35:07 +03:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
using namespace Rss;
|
|
|
|
|
|
|
|
DownloadRule::DownloadRule()
|
2015-10-14 12:49:29 +03:00
|
|
|
: m_enabled(false)
|
|
|
|
, m_useRegex(false)
|
|
|
|
, m_apstate(USE_GLOBAL)
|
2016-01-13 19:42:58 +03:00
|
|
|
, m_ignoreDays(0)
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
bool DownloadRule::matches(const QString &articleTitle) const
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
foreach (const QString &token, m_mustContain) {
|
|
|
|
if (!token.isEmpty()) {
|
|
|
|
QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard);
|
|
|
|
if (reg.indexIn(articleTitle) < 0)
|
|
|
|
return false;
|
|
|
|
}
|
2012-02-20 22:49:31 +04:00
|
|
|
}
|
2015-10-14 12:49:29 +03:00
|
|
|
qDebug("Checking not matching tokens");
|
|
|
|
// Checking not matching
|
|
|
|
foreach (const QString &token, m_mustNotContain) {
|
|
|
|
if (!token.isEmpty()) {
|
|
|
|
QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard);
|
|
|
|
if (reg.indexIn(articleTitle) > -1)
|
|
|
|
return false;
|
2013-07-24 15:30:58 +04:00
|
|
|
}
|
2015-10-14 12:49:29 +03:00
|
|
|
}
|
|
|
|
if (!m_episodeFilter.isEmpty()) {
|
|
|
|
qDebug("Checking episode filter");
|
|
|
|
QRegExp f("(^\\d{1,4})x(.*;$)");
|
|
|
|
int pos = f.indexIn(m_episodeFilter);
|
|
|
|
if (pos < 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
QString s = f.cap(1);
|
|
|
|
QStringList eps = f.cap(2).split(";");
|
|
|
|
QString expStr;
|
|
|
|
expStr += "s0?" + s + "[ -_\\.]?" + "e0?";
|
|
|
|
|
|
|
|
foreach (const QString &ep, eps) {
|
|
|
|
if (ep.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ep.indexOf('-') != -1) { // Range detected
|
|
|
|
QString partialPattern = "s0?" + s + "[ -_\\.]?" + "e(0?\\d{1,4})";
|
|
|
|
QRegExp reg(partialPattern, Qt::CaseInsensitive);
|
|
|
|
|
|
|
|
if (ep.endsWith('-')) { // Infinite range
|
|
|
|
int epOurs = ep.left(ep.size() - 1).toInt();
|
|
|
|
|
|
|
|
// Extract partial match from article and compare as digits
|
|
|
|
pos = reg.indexIn(articleTitle);
|
|
|
|
if (pos != -1) {
|
|
|
|
int epTheirs = reg.cap(1).toInt();
|
|
|
|
if (epTheirs >= epOurs)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // Normal range
|
|
|
|
QStringList range = ep.split('-');
|
|
|
|
Q_ASSERT(range.size() == 2);
|
|
|
|
if (range.first().toInt() > range.last().toInt())
|
|
|
|
continue; // Ignore this subrule completely
|
|
|
|
|
|
|
|
int epOursFirst = range.first().toInt();
|
|
|
|
int epOursLast = range.last().toInt();
|
|
|
|
|
|
|
|
// Extract partial match from article and compare as digits
|
|
|
|
pos = reg.indexIn(articleTitle);
|
|
|
|
if (pos != -1) {
|
|
|
|
int epTheirs = reg.cap(1).toInt();
|
|
|
|
if (epOursFirst <= epTheirs && epOursLast >= epTheirs)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // Single number
|
|
|
|
QRegExp reg(expStr + ep + "\\D", Qt::CaseInsensitive);
|
|
|
|
if (reg.indexIn(articleTitle) != -1)
|
|
|
|
return true;
|
|
|
|
}
|
2013-07-24 15:30:58 +04:00
|
|
|
}
|
2015-10-14 12:49:29 +03:00
|
|
|
return false;
|
2013-07-24 15:30:58 +04:00
|
|
|
}
|
2015-10-14 12:49:29 +03:00
|
|
|
return true;
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setMustContain(const QString &tokens)
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
if (m_useRegex)
|
|
|
|
m_mustContain = QStringList() << tokens;
|
|
|
|
else
|
|
|
|
m_mustContain = tokens.split(" ");
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setMustNotContain(const QString &tokens)
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
if (m_useRegex)
|
|
|
|
m_mustNotContain = QStringList() << tokens;
|
|
|
|
else
|
|
|
|
m_mustNotContain = tokens.split("|");
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QStringList DownloadRule::rssFeeds() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_rssFeeds;
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setRssFeeds(const QStringList &rssFeeds)
|
2010-11-07 16:06:00 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
m_rssFeeds = rssFeeds;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QString DownloadRule::name() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_name;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setName(const QString &name)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QString DownloadRule::savePath() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_savePath;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
DownloadRulePtr DownloadRule::fromVariantHash(const QVariantHash &ruleHash)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
2015-10-15 19:33:27 +03:00
|
|
|
DownloadRulePtr rule(new DownloadRule);
|
2015-10-14 12:49:29 +03:00
|
|
|
rule->setName(ruleHash.value("name").toString());
|
|
|
|
rule->setUseRegex(ruleHash.value("use_regex", false).toBool());
|
|
|
|
rule->setMustContain(ruleHash.value("must_contain").toString());
|
|
|
|
rule->setMustNotContain(ruleHash.value("must_not_contain").toString());
|
|
|
|
rule->setEpisodeFilter(ruleHash.value("episode_filter").toString());
|
|
|
|
rule->setRssFeeds(ruleHash.value("affected_feeds").toStringList());
|
|
|
|
rule->setEnabled(ruleHash.value("enabled", false).toBool());
|
|
|
|
rule->setSavePath(ruleHash.value("save_path").toString());
|
2016-02-09 11:56:48 +03:00
|
|
|
rule->setCategory(ruleHash.value("category_assigned").toString());
|
2015-10-14 12:49:29 +03:00
|
|
|
rule->setAddPaused(AddPausedState(ruleHash.value("add_paused").toUInt()));
|
|
|
|
rule->setLastMatch(ruleHash.value("last_match").toDateTime());
|
|
|
|
rule->setIgnoreDays(ruleHash.value("ignore_days").toInt());
|
|
|
|
return rule;
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QVariantHash DownloadRule::toVariantHash() const
|
2010-11-07 16:06:00 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
QVariantHash hash;
|
|
|
|
hash["name"] = m_name;
|
|
|
|
hash["must_contain"] = m_mustContain.join(" ");
|
|
|
|
hash["must_not_contain"] = m_mustNotContain.join("|");
|
|
|
|
hash["save_path"] = m_savePath;
|
|
|
|
hash["affected_feeds"] = m_rssFeeds;
|
|
|
|
hash["enabled"] = m_enabled;
|
2016-02-09 11:56:48 +03:00
|
|
|
hash["category_assigned"] = m_category;
|
2015-10-14 12:49:29 +03:00
|
|
|
hash["use_regex"] = m_useRegex;
|
|
|
|
hash["add_paused"] = m_apstate;
|
|
|
|
hash["episode_filter"] = m_episodeFilter;
|
|
|
|
hash["last_match"] = m_lastMatch;
|
|
|
|
hash["ignore_days"] = m_ignoreDays;
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
bool DownloadRule::operator==(const DownloadRule &other) const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_name == other.name();
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setSavePath(const QString &savePath)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
m_savePath = Utils::Fs::fromNativePath(savePath);
|
2015-10-14 12:49:29 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
DownloadRule::AddPausedState DownloadRule::addPaused() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_apstate;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setAddPaused(const DownloadRule::AddPausedState &aps)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
m_apstate = aps;
|
|
|
|
}
|
|
|
|
|
2016-02-09 11:56:48 +03:00
|
|
|
QString DownloadRule::category() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
return m_category;
|
2015-10-14 12:49:29 +03:00
|
|
|
}
|
|
|
|
|
2016-02-09 11:56:48 +03:00
|
|
|
void DownloadRule::setCategory(const QString &category)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
2016-02-09 11:56:48 +03:00
|
|
|
m_category = category;
|
2015-10-14 12:49:29 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
bool DownloadRule::isEnabled() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_enabled;
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setEnabled(bool enable)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
m_enabled = enable;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setLastMatch(const QDateTime &d)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
m_lastMatch = d;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QDateTime DownloadRule::lastMatch() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_lastMatch;
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
2010-11-13 13:49:22 +03:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setIgnoreDays(int d)
|
2010-11-13 13:49:22 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
m_ignoreDays = d;
|
2010-11-13 13:49:22 +03:00
|
|
|
}
|
2010-11-13 22:36:46 +03:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
int DownloadRule::ignoreDays() const
|
2010-11-13 22:36:46 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
return m_ignoreDays;
|
|
|
|
}
|
2012-07-14 15:45:40 +04:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QString DownloadRule::mustContain() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_mustContain.join(" ");
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QString DownloadRule::mustNotContain() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_mustNotContain.join("|");
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
bool DownloadRule::useRegex() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_useRegex;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setUseRegex(bool enabled)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
m_useRegex = enabled;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QString DownloadRule::episodeFilter() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_episodeFilter;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRule::setEpisodeFilter(const QString &e)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
m_episodeFilter = e;
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QStringList DownloadRule::findMatchingArticles(const FeedPtr &feed) const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
QStringList ret;
|
2015-10-15 19:33:27 +03:00
|
|
|
const ArticleHash &feedArticles = feed->articleHash();
|
2015-10-14 12:49:29 +03:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
ArticleHash::ConstIterator artIt = feedArticles.begin();
|
|
|
|
ArticleHash::ConstIterator artItend = feedArticles.end();
|
2015-10-14 12:49:29 +03:00
|
|
|
for ( ; artIt != artItend ; ++artIt) {
|
|
|
|
const QString title = artIt.value()->title();
|
|
|
|
if (matches(title))
|
|
|
|
ret << title;
|
|
|
|
}
|
|
|
|
return ret;
|
2010-11-13 22:36:46 +03:00
|
|
|
}
|