|
|
@ -1,6 +1,7 @@ |
|
|
|
/*
|
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent. |
|
|
|
* Bittorrent Client using Qt and libtorrent. |
|
|
|
* Copyright (C) 2012 Christophe Dumez |
|
|
|
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru> |
|
|
|
|
|
|
|
* Copyright (C) 2012 Christophe Dumez <chris@qbittorrent.org> |
|
|
|
* |
|
|
|
* |
|
|
|
* This program is free software; you can redistribute it and/or |
|
|
|
* This program is free software; you can redistribute it and/or |
|
|
|
* modify it under the terms of the GNU General Public License |
|
|
|
* modify it under the terms of the GNU General Public License |
|
|
@ -30,64 +31,36 @@ |
|
|
|
|
|
|
|
|
|
|
|
#include <QDebug> |
|
|
|
#include <QDebug> |
|
|
|
#include <QDateTime> |
|
|
|
#include <QDateTime> |
|
|
|
#include <QFile> |
|
|
|
|
|
|
|
#include <QRegExp> |
|
|
|
#include <QRegExp> |
|
|
|
#include <QStringList> |
|
|
|
#include <QStringList> |
|
|
|
#include <QVariant> |
|
|
|
#include <QVariant> |
|
|
|
#include <QXmlStreamReader> |
|
|
|
#include <QXmlStreamReader> |
|
|
|
|
|
|
|
|
|
|
|
#include "base/utils/fs.h" |
|
|
|
|
|
|
|
#include "rssparser.h" |
|
|
|
#include "rssparser.h" |
|
|
|
|
|
|
|
|
|
|
|
namespace Rss |
|
|
|
namespace |
|
|
|
{ |
|
|
|
{ |
|
|
|
namespace Private |
|
|
|
const char shortDay[][4] = { |
|
|
|
{ |
|
|
|
|
|
|
|
struct ParsingJob |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
QString feedUrl; |
|
|
|
|
|
|
|
QByteArray feedData; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static const char shortDay[][4] = { |
|
|
|
|
|
|
|
"Mon", "Tue", "Wed", |
|
|
|
"Mon", "Tue", "Wed", |
|
|
|
"Thu", "Fri", "Sat", |
|
|
|
"Thu", "Fri", "Sat", |
|
|
|
"Sun" |
|
|
|
"Sun" |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static const char longDay[][10] = { |
|
|
|
const char longDay[][10] = { |
|
|
|
"Monday", "Tuesday", "Wednesday", |
|
|
|
"Monday", "Tuesday", "Wednesday", |
|
|
|
"Thursday", "Friday", "Saturday", |
|
|
|
"Thursday", "Friday", "Saturday", |
|
|
|
"Sunday" |
|
|
|
"Sunday" |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
static const char shortMonth[][4] = { |
|
|
|
const char shortMonth[][4] = { |
|
|
|
"Jan", "Feb", "Mar", "Apr", |
|
|
|
"Jan", "Feb", "Mar", "Apr", |
|
|
|
"May", "Jun", "Jul", "Aug", |
|
|
|
"May", "Jun", "Jul", "Aug", |
|
|
|
"Sep", "Oct", "Nov", "Dec" |
|
|
|
"Sep", "Oct", "Nov", "Dec" |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
using namespace Rss::Private; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parser::Parser(QObject *parent) |
|
|
|
|
|
|
|
: QThread(parent) |
|
|
|
|
|
|
|
, m_running(true) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
start(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Parser::~Parser() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_running = false; |
|
|
|
|
|
|
|
m_waitCondition.wakeOne(); |
|
|
|
|
|
|
|
wait(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Ported to Qt from KDElibs4
|
|
|
|
// Ported to Qt from KDElibs4
|
|
|
|
QDateTime Parser::parseDate(const QString &string) |
|
|
|
QDateTime parseDate(const QString &string) |
|
|
|
{ |
|
|
|
{ |
|
|
|
const QString str = string.trimmed(); |
|
|
|
const QString str = string.trimmed(); |
|
|
|
if (str.isEmpty()) |
|
|
|
if (str.isEmpty()) |
|
|
|
return QDateTime::currentDateTime(); |
|
|
|
return QDateTime::currentDateTime(); |
|
|
@ -230,48 +203,54 @@ QDateTime Parser::parseDate(const QString &string) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return result; |
|
|
|
return result; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Parser::parseFeedData(const QString &feedUrl, const QByteArray &feedData) |
|
|
|
using namespace Rss::Private; |
|
|
|
{ |
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << feedUrl; |
|
|
|
|
|
|
|
m_mutex.lock(); |
|
|
|
|
|
|
|
ParsingJob job = { feedUrl, feedData }; |
|
|
|
|
|
|
|
m_queue.enqueue(job); |
|
|
|
|
|
|
|
// Wake up thread.
|
|
|
|
|
|
|
|
if (m_queue.count() == 1) { |
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << "Waking up thread"; |
|
|
|
|
|
|
|
m_waitCondition.wakeOne(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
m_mutex.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Parser::clearFeedData(const QString &feedUrl) |
|
|
|
// read and create items from a rss document
|
|
|
|
|
|
|
|
void Parser::parse(const QByteArray &feedData) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_mutex.lock(); |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
m_lastBuildDates.remove(feedUrl); |
|
|
|
|
|
|
|
m_mutex.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Parser::run() |
|
|
|
QXmlStreamReader xml(feedData); |
|
|
|
{ |
|
|
|
bool foundChannel = false; |
|
|
|
while (m_running) { |
|
|
|
while (xml.readNextStartElement()) { |
|
|
|
m_mutex.lock(); |
|
|
|
if (xml.name() == "rss") { |
|
|
|
if (!m_queue.empty()) { |
|
|
|
// Find channels
|
|
|
|
ParsingJob job = m_queue.dequeue(); |
|
|
|
while (xml.readNextStartElement()) { |
|
|
|
m_mutex.unlock(); |
|
|
|
if (xml.name() == "channel") { |
|
|
|
parseFeed(job); |
|
|
|
parseRSSChannel(xml); |
|
|
|
|
|
|
|
foundChannel = true; |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
else { |
|
|
|
qDebug() << Q_FUNC_INFO << "Thread is waiting."; |
|
|
|
qDebug() << "Skip rss item: " << xml.name(); |
|
|
|
m_waitCondition.wait(&m_mutex); |
|
|
|
xml.skipCurrentElement(); |
|
|
|
qDebug() << Q_FUNC_INFO << "Thread woke up."; |
|
|
|
} |
|
|
|
m_mutex.unlock(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
else if (xml.name() == "feed") { // Atom feed
|
|
|
|
|
|
|
|
parseAtomChannel(xml); |
|
|
|
|
|
|
|
foundChannel = true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
qDebug() << "Skip root item: " << xml.name(); |
|
|
|
|
|
|
|
xml.skipCurrentElement(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (xml.hasError()) |
|
|
|
|
|
|
|
emit finished(xml.errorString()); |
|
|
|
|
|
|
|
else if (!foundChannel) |
|
|
|
|
|
|
|
emit finished(tr("Invalid RSS feed.")); |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
emit finished(QString()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Parser::parseRssArticle(QXmlStreamReader &xml, const QString &feedUrl) |
|
|
|
void Parser::parseRssArticle(QXmlStreamReader &xml) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QVariantHash article; |
|
|
|
QVariantHash article; |
|
|
|
|
|
|
|
|
|
|
@ -332,12 +311,12 @@ void Parser::parseRssArticle(QXmlStreamReader &xml, const QString &feedUrl) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
emit newArticle(feedUrl, article); |
|
|
|
emit newArticle(article); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Parser::parseRSSChannel(QXmlStreamReader &xml, const QString &feedUrl) |
|
|
|
void Parser::parseRSSChannel(QXmlStreamReader &xml) |
|
|
|
{ |
|
|
|
{ |
|
|
|
qDebug() << Q_FUNC_INFO << feedUrl; |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
Q_ASSERT(xml.isStartElement() && xml.name() == "channel"); |
|
|
|
Q_ASSERT(xml.isStartElement() && xml.name() == "channel"); |
|
|
|
|
|
|
|
|
|
|
|
while(!xml.atEnd()) { |
|
|
|
while(!xml.atEnd()) { |
|
|
@ -346,27 +325,26 @@ void Parser::parseRSSChannel(QXmlStreamReader &xml, const QString &feedUrl) |
|
|
|
if (xml.isStartElement()) { |
|
|
|
if (xml.isStartElement()) { |
|
|
|
if (xml.name() == "title") { |
|
|
|
if (xml.name() == "title") { |
|
|
|
QString title = xml.readElementText(); |
|
|
|
QString title = xml.readElementText(); |
|
|
|
emit feedTitle(feedUrl, title); |
|
|
|
emit feedTitle(title); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (xml.name() == "lastBuildDate") { |
|
|
|
else if (xml.name() == "lastBuildDate") { |
|
|
|
QString lastBuildDate = xml.readElementText(); |
|
|
|
QString lastBuildDate = xml.readElementText(); |
|
|
|
if (!lastBuildDate.isEmpty()) { |
|
|
|
if (!lastBuildDate.isEmpty()) { |
|
|
|
QMutexLocker locker(&m_mutex); |
|
|
|
if (m_lastBuildDate == lastBuildDate) { |
|
|
|
if (m_lastBuildDates.value(feedUrl, "") == lastBuildDate) { |
|
|
|
|
|
|
|
qDebug() << "The RSS feed has not changed since last time, aborting parsing."; |
|
|
|
qDebug() << "The RSS feed has not changed since last time, aborting parsing."; |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
m_lastBuildDates[feedUrl] = lastBuildDate; |
|
|
|
m_lastBuildDate = lastBuildDate; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (xml.name() == "item") { |
|
|
|
else if (xml.name() == "item") { |
|
|
|
parseRssArticle(xml, feedUrl); |
|
|
|
parseRssArticle(xml); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Parser::parseAtomArticle(QXmlStreamReader &xml, const QString &feedUrl, const QString &baseUrl) |
|
|
|
void Parser::parseAtomArticle(QXmlStreamReader &xml) |
|
|
|
{ |
|
|
|
{ |
|
|
|
QVariantHash article; |
|
|
|
QVariantHash article; |
|
|
|
bool doubleContent = false; |
|
|
|
bool doubleContent = false; |
|
|
@ -392,7 +370,7 @@ void Parser::parseAtomArticle(QXmlStreamReader &xml, const QString &feedUrl, con |
|
|
|
// Atom feeds can have relative links, work around this and
|
|
|
|
// Atom feeds can have relative links, work around this and
|
|
|
|
// take the stress of figuring article full URI from UI
|
|
|
|
// take the stress of figuring article full URI from UI
|
|
|
|
// Assemble full URI
|
|
|
|
// Assemble full URI
|
|
|
|
article["news_link"] = ( baseUrl.isEmpty() ? link : baseUrl + link ); |
|
|
|
article["news_link"] = ( m_baseUrl.isEmpty() ? link : m_baseUrl + link ); |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
else if ((xml.name() == "summary") || (xml.name() == "content")){ |
|
|
|
else if ((xml.name() == "summary") || (xml.name() == "content")){ |
|
|
@ -453,15 +431,15 @@ void Parser::parseAtomArticle(QXmlStreamReader &xml, const QString &feedUrl, con |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
emit newArticle(feedUrl, article); |
|
|
|
emit newArticle(article); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Parser::parseAtomChannel(QXmlStreamReader &xml, const QString &feedUrl) |
|
|
|
void Parser::parseAtomChannel(QXmlStreamReader &xml) |
|
|
|
{ |
|
|
|
{ |
|
|
|
qDebug() << Q_FUNC_INFO << feedUrl; |
|
|
|
qDebug() << Q_FUNC_INFO; |
|
|
|
Q_ASSERT(xml.isStartElement() && xml.name() == "feed"); |
|
|
|
Q_ASSERT(xml.isStartElement() && xml.name() == "feed"); |
|
|
|
|
|
|
|
|
|
|
|
QString baseURL = xml.attributes().value("xml:base").toString(); |
|
|
|
m_baseUrl = xml.attributes().value("xml:base").toString(); |
|
|
|
|
|
|
|
|
|
|
|
while (!xml.atEnd()) { |
|
|
|
while (!xml.atEnd()) { |
|
|
|
xml.readNext(); |
|
|
|
xml.readNext(); |
|
|
@ -469,74 +447,21 @@ void Parser::parseAtomChannel(QXmlStreamReader &xml, const QString &feedUrl) |
|
|
|
if (xml.isStartElement()) { |
|
|
|
if (xml.isStartElement()) { |
|
|
|
if (xml.name() == "title") { |
|
|
|
if (xml.name() == "title") { |
|
|
|
QString title = xml.readElementText(); |
|
|
|
QString title = xml.readElementText(); |
|
|
|
emit feedTitle(feedUrl, title); |
|
|
|
emit feedTitle(title); |
|
|
|
} |
|
|
|
} |
|
|
|
else if (xml.name() == "updated") { |
|
|
|
else if (xml.name() == "updated") { |
|
|
|
QString lastBuildDate = xml.readElementText(); |
|
|
|
QString lastBuildDate = xml.readElementText(); |
|
|
|
if (!lastBuildDate.isEmpty()) { |
|
|
|
if (!lastBuildDate.isEmpty()) { |
|
|
|
QMutexLocker locker(&m_mutex); |
|
|
|
if (m_lastBuildDate == lastBuildDate) { |
|
|
|
if (m_lastBuildDates.value(feedUrl) == lastBuildDate) { |
|
|
|
|
|
|
|
qDebug() << "The RSS feed has not changed since last time, aborting parsing."; |
|
|
|
qDebug() << "The RSS feed has not changed since last time, aborting parsing."; |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
m_lastBuildDates[feedUrl] = lastBuildDate; |
|
|
|
m_lastBuildDate = lastBuildDate; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
else if (xml.name() == "entry") { |
|
|
|
else if (xml.name() == "entry") { |
|
|
|
parseAtomArticle(xml, feedUrl, baseURL); |
|
|
|
parseAtomArticle(xml); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// read and create items from a rss document
|
|
|
|
|
|
|
|
void Parser::parseFeed(const ParsingJob &job) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
qDebug() << Q_FUNC_INFO << job.feedUrl; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QXmlStreamReader xml(job.feedData); |
|
|
|
|
|
|
|
bool foundChannel = false; |
|
|
|
|
|
|
|
while (xml.readNextStartElement()) { |
|
|
|
|
|
|
|
if (xml.name() == "rss") { |
|
|
|
|
|
|
|
// Find channels
|
|
|
|
|
|
|
|
while (xml.readNextStartElement()) { |
|
|
|
|
|
|
|
if (xml.name() == "channel") { |
|
|
|
|
|
|
|
parseRSSChannel(xml, job.feedUrl); |
|
|
|
|
|
|
|
foundChannel = true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
qDebug() << "Skip rss item: " << xml.name(); |
|
|
|
|
|
|
|
xml.skipCurrentElement(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (xml.name() == "feed") { // Atom feed
|
|
|
|
|
|
|
|
parseAtomChannel(xml, job.feedUrl); |
|
|
|
|
|
|
|
foundChannel = true; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else { |
|
|
|
|
|
|
|
qDebug() << "Skip root item: " << xml.name(); |
|
|
|
|
|
|
|
xml.skipCurrentElement(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (xml.hasError()) { |
|
|
|
|
|
|
|
reportFailure(job, xml.errorString()); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!foundChannel) { |
|
|
|
|
|
|
|
reportFailure(job, tr("Invalid RSS feed at '%1'.").arg(job.feedUrl)); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
emit feedParsingFinished(job.feedUrl, QString()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Parser::reportFailure(const ParsingJob &job, const QString &error) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
emit feedParsingFinished(job.feedUrl, error); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|