2020-02-28 23:25:08 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2020 Tobias Fella <fella@posteo.de>
|
|
|
|
*
|
|
|
|
* 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) version 3 or any later version
|
|
|
|
* accepted by the membership of KDE e.V. (or its successor approved
|
|
|
|
* by the membership of KDE e.V.), which shall act as a proxy
|
|
|
|
* defined in Section 14 of version 3 of the license.
|
|
|
|
*
|
|
|
|
* 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, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
|
2020-02-29 15:34:12 +01:00
|
|
|
#include <Syndication/Syndication>
|
2020-02-28 23:25:08 +01:00
|
|
|
|
|
|
|
#include "fetcher.h"
|
|
|
|
|
2020-03-16 22:37:04 +01:00
|
|
|
#include "database.h"
|
|
|
|
|
2020-02-28 23:25:08 +01:00
|
|
|
Fetcher::Fetcher() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Fetcher::fetch(QUrl url)
|
|
|
|
{
|
|
|
|
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
|
|
|
|
manager->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
|
|
manager->setStrictTransportSecurityEnabled(true);
|
|
|
|
manager->enableStrictTransportSecurityStore(true);
|
|
|
|
QNetworkRequest request = QNetworkRequest(QUrl(url));
|
|
|
|
QNetworkReply *reply = manager->get(request);
|
|
|
|
connect(reply, &QNetworkReply::finished, this, [this, url, reply]() {
|
|
|
|
QByteArray data = reply->readAll();
|
|
|
|
Syndication::DocumentSource *document = new Syndication::DocumentSource(data, url.toString());
|
|
|
|
Syndication::FeedPtr feed = Syndication::parserCollection()->parse(*document, QStringLiteral("Atom"));
|
|
|
|
|
|
|
|
QSqlDatabase db = QSqlDatabase::database();
|
2020-02-29 00:55:16 +01:00
|
|
|
QSqlQuery query(db);
|
2020-02-28 23:25:08 +01:00
|
|
|
|
|
|
|
for (const auto &entry : feed->items()) {
|
2020-03-04 14:00:27 +01:00
|
|
|
query.prepare(QStringLiteral("INSERT INTO Entries VALUES (:feed, :id, :title, :content);"));
|
2020-02-28 23:25:08 +01:00
|
|
|
query.bindValue(QStringLiteral(":feed"), url.toString());
|
|
|
|
query.bindValue(QStringLiteral(":id"), entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":title"), entry->title());
|
2020-03-04 14:00:27 +01:00
|
|
|
if(!entry->content().isEmpty())
|
|
|
|
query.bindValue(QStringLiteral(":content"), entry->content());
|
|
|
|
else
|
|
|
|
query.bindValue(QStringLiteral(":content"), entry->description());
|
2020-03-16 22:37:04 +01:00
|
|
|
Database::instance().execute(query);
|
2020-02-28 23:25:08 +01:00
|
|
|
for (const auto &author : entry->authors()) {
|
|
|
|
query = QSqlQuery(db);
|
|
|
|
query.prepare(QStringLiteral("INSERT INTO Authors VALUES(:id, :name, :uri, :email);"));
|
|
|
|
query.bindValue(QStringLiteral(":id"), entry->id());
|
|
|
|
query.bindValue(QStringLiteral(":name"), author->name());
|
|
|
|
query.bindValue(QStringLiteral(":uri"), author->uri());
|
|
|
|
query.bindValue(QStringLiteral(":email"), author->email());
|
2020-03-16 22:37:04 +01:00
|
|
|
Database::instance().execute(query);
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|
2020-02-29 19:13:30 +01:00
|
|
|
query.prepare(QStringLiteral("UPDATE Feeds SET name=:name WHERE url=:url;"));
|
|
|
|
query.bindValue(QStringLiteral(":name"), feed->title());
|
|
|
|
query.bindValue(QStringLiteral(":url"), url.toString());
|
2020-03-16 22:37:04 +01:00
|
|
|
Database::instance().execute(query);
|
2020-02-28 23:25:08 +01:00
|
|
|
}
|
2020-02-29 00:51:26 +01:00
|
|
|
delete reply;
|
2020-02-28 23:25:08 +01:00
|
|
|
emit finished();
|
|
|
|
});
|
|
|
|
}
|