2012-03-04 21:47:58 +01:00
|
|
|
/* This file is part of Clementine.
|
2014-11-01 19:38:39 +01:00
|
|
|
Copyright 2012, 2014, David Sansome <me@davidsansome.com>
|
|
|
|
Copyright 2014, John Maguire <john.maguire@gmail.com>
|
2014-11-02 19:50:39 +01:00
|
|
|
Copyright 2014, Krzysztof Sobiecki <sobkas@gmail.com>
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-03-04 21:47:58 +01:00
|
|
|
Clementine 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 3 of the License, or
|
|
|
|
(at your option) any later version.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-03-04 21:47:58 +01:00
|
|
|
Clementine 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.
|
2014-02-07 16:34:20 +01:00
|
|
|
|
2012-03-04 21:47:58 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "podcastparser.h"
|
|
|
|
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QXmlStreamReader>
|
|
|
|
|
2014-12-13 02:24:20 +01:00
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/utilities.h"
|
|
|
|
#include "opmlcontainer.h"
|
|
|
|
|
2014-05-29 13:11:49 +02:00
|
|
|
// Namespace constants must be lower case.
|
|
|
|
const char* PodcastParser::kAtomNamespace = "http://www.w3.org/2005/atom";
|
2014-02-07 16:34:20 +01:00
|
|
|
const char* PodcastParser::kItunesNamespace =
|
|
|
|
"http://www.itunes.com/dtds/podcast-1.0.dtd";
|
2012-03-04 21:47:58 +01:00
|
|
|
|
|
|
|
PodcastParser::PodcastParser() {
|
2012-03-05 19:15:45 +01:00
|
|
|
supported_mime_types_ << "application/rss+xml"
|
|
|
|
<< "application/xml"
|
2012-03-07 16:11:56 +01:00
|
|
|
<< "text/x-opml"
|
2012-03-05 19:15:45 +01:00
|
|
|
<< "text/xml";
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
|
2012-03-05 19:15:45 +01:00
|
|
|
bool PodcastParser::SupportsContentType(const QString& content_type) const {
|
2012-06-16 23:58:33 +02:00
|
|
|
if (content_type.isEmpty()) {
|
|
|
|
// Why not have a go.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-10 14:29:07 +01:00
|
|
|
for (const QString& mime_type : supported_mime_types()) {
|
2012-03-05 19:15:45 +01:00
|
|
|
if (content_type.contains(mime_type)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-03-11 18:57:15 +01:00
|
|
|
bool PodcastParser::TryMagic(const QByteArray& data) const {
|
|
|
|
QString str(QString::fromUtf8(data));
|
2014-02-07 16:34:20 +01:00
|
|
|
return str.contains(QRegExp("<rss\\b")) || str.contains(QRegExp("<opml\\b"));
|
2012-03-11 18:57:15 +01:00
|
|
|
}
|
|
|
|
|
2012-03-07 16:11:56 +01:00
|
|
|
QVariant PodcastParser::Load(QIODevice* device, const QUrl& url) const {
|
2012-03-04 21:47:58 +01:00
|
|
|
QXmlStreamReader reader(device);
|
2012-03-07 16:11:56 +01:00
|
|
|
|
|
|
|
while (!reader.atEnd()) {
|
|
|
|
switch (reader.readNext()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::StartElement: {
|
|
|
|
const QStringRef name = reader.name();
|
|
|
|
if (name == "rss") {
|
|
|
|
Podcast podcast;
|
|
|
|
if (!ParseRss(&reader, &podcast)) {
|
|
|
|
return QVariant();
|
|
|
|
} else {
|
|
|
|
podcast.set_url(url);
|
|
|
|
return QVariant::fromValue(podcast);
|
|
|
|
}
|
|
|
|
} else if (name == "opml") {
|
|
|
|
OpmlContainer container;
|
|
|
|
if (!ParseOpml(&reader, &container)) {
|
|
|
|
return QVariant();
|
|
|
|
} else {
|
|
|
|
container.url = url;
|
|
|
|
return QVariant::fromValue(container);
|
|
|
|
}
|
2012-03-07 16:11:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
return QVariant();
|
|
|
|
}
|
2012-03-07 16:11:56 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
|
|
|
break;
|
2012-03-07 16:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PodcastParser::ParseRss(QXmlStreamReader* reader, Podcast* ret) const {
|
|
|
|
if (!Utilities::ParseUntilElement(reader, "channel")) {
|
2012-03-05 19:15:45 +01:00
|
|
|
return false;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
|
2012-03-07 16:11:56 +01:00
|
|
|
ParseChannel(reader, ret);
|
2012-03-05 19:15:45 +01:00
|
|
|
return true;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastParser::ParseChannel(QXmlStreamReader* reader, Podcast* ret) const {
|
|
|
|
while (!reader->atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader->readNext();
|
|
|
|
switch (type) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::StartElement: {
|
|
|
|
const QStringRef name = reader->name();
|
2014-05-29 13:11:49 +02:00
|
|
|
const QString lower_namespace =
|
|
|
|
reader->namespaceUri().toString().toLower();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (name == "title") {
|
|
|
|
ret->set_title(reader->readElementText());
|
2014-05-29 13:11:49 +02:00
|
|
|
} else if (name == "link" && lower_namespace.isEmpty()) {
|
2014-02-07 16:34:20 +01:00
|
|
|
ret->set_link(QUrl::fromEncoded(reader->readElementText().toAscii()));
|
|
|
|
} else if (name == "description") {
|
|
|
|
ret->set_description(reader->readElementText());
|
2014-05-29 13:11:49 +02:00
|
|
|
} else if (name == "owner" && lower_namespace == kItunesNamespace) {
|
2014-02-07 16:34:20 +01:00
|
|
|
ParseItunesOwner(reader, ret);
|
|
|
|
} else if (name == "image") {
|
|
|
|
ParseImage(reader, ret);
|
|
|
|
} else if (name == "copyright") {
|
|
|
|
ret->set_copyright(reader->readElementText());
|
2014-12-14 12:23:04 +01:00
|
|
|
} else if (name == "link" && lower_namespace == kAtomNamespace &&
|
2014-02-07 16:34:20 +01:00
|
|
|
ret->url().isEmpty() &&
|
|
|
|
reader->attributes().value("rel") == "self") {
|
|
|
|
ret->set_url(QUrl::fromEncoded(reader->readElementText().toAscii()));
|
|
|
|
} else if (name == "item") {
|
|
|
|
ParseItem(reader, ret);
|
|
|
|
} else {
|
|
|
|
Utilities::ConsumeCurrentElement(reader);
|
|
|
|
}
|
|
|
|
break;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
return;
|
2012-03-04 21:47:58 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
|
|
|
break;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastParser::ParseImage(QXmlStreamReader* reader, Podcast* ret) const {
|
|
|
|
while (!reader->atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader->readNext();
|
|
|
|
switch (type) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::StartElement: {
|
|
|
|
const QStringRef name = reader->name();
|
|
|
|
if (name == "url") {
|
|
|
|
ret->set_image_url_large(
|
|
|
|
QUrl::fromEncoded(reader->readElementText().toAscii()));
|
|
|
|
} else {
|
|
|
|
Utilities::ConsumeCurrentElement(reader);
|
|
|
|
}
|
|
|
|
break;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
return;
|
2012-03-04 21:47:58 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
|
|
|
break;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void PodcastParser::ParseItunesOwner(QXmlStreamReader* reader,
|
|
|
|
Podcast* ret) const {
|
2012-03-04 21:47:58 +01:00
|
|
|
while (!reader->atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader->readNext();
|
|
|
|
switch (type) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::StartElement: {
|
|
|
|
const QStringRef name = reader->name();
|
|
|
|
if (name == "name") {
|
|
|
|
ret->set_owner_name(reader->readElementText());
|
|
|
|
} else if (name == "email") {
|
|
|
|
ret->set_owner_email(reader->readElementText());
|
|
|
|
} else {
|
|
|
|
Utilities::ConsumeCurrentElement(reader);
|
|
|
|
}
|
|
|
|
break;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
return;
|
2012-03-04 21:47:58 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
|
|
|
break;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PodcastParser::ParseItem(QXmlStreamReader* reader, Podcast* ret) const {
|
|
|
|
PodcastEpisode episode;
|
|
|
|
|
|
|
|
while (!reader->atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader->readNext();
|
|
|
|
switch (type) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::StartElement: {
|
|
|
|
const QStringRef name = reader->name();
|
2014-05-29 13:11:49 +02:00
|
|
|
const QString lower_namespace =
|
|
|
|
reader->namespaceUri().toString().toLower();
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
if (name == "title") {
|
|
|
|
episode.set_title(reader->readElementText());
|
|
|
|
} else if (name == "description") {
|
|
|
|
episode.set_description(reader->readElementText());
|
|
|
|
} else if (name == "pubDate") {
|
2014-12-11 22:42:18 +01:00
|
|
|
QString date = reader->readElementText();
|
|
|
|
episode.set_publication_date(Utilities::ParseRFC822DateTime(date));
|
|
|
|
if (!episode.publication_date().isValid()) {
|
|
|
|
qLog(Error) << "Unable to parse date:" << date
|
2014-12-14 14:35:21 +01:00
|
|
|
<< "Please submit it to "
|
2014-12-16 01:43:11 +01:00
|
|
|
<< QUrl::toPercentEncoding(QString("https://github.com/clementine-player/Clementine/"
|
|
|
|
"issues/new?title=[podcast]"
|
|
|
|
" Unable to parse date: %1").arg(date));
|
2014-12-11 22:42:18 +01:00
|
|
|
}
|
2014-05-29 13:11:49 +02:00
|
|
|
} else if (name == "duration" && lower_namespace == kItunesNamespace) {
|
2014-02-07 16:34:20 +01:00
|
|
|
// http://www.apple.com/itunes/podcasts/specs.html
|
|
|
|
QStringList parts = reader->readElementText().split(':');
|
|
|
|
if (parts.count() == 2) {
|
|
|
|
episode.set_duration_secs(parts[0].toInt() * 60 + parts[1].toInt());
|
|
|
|
} else if (parts.count() >= 3) {
|
|
|
|
episode.set_duration_secs(parts[0].toInt() * 60 * 60 +
|
|
|
|
parts[1].toInt() * 60 + parts[2].toInt());
|
|
|
|
}
|
|
|
|
} else if (name == "enclosure") {
|
|
|
|
const QString type = reader->attributes().value("type").toString();
|
|
|
|
if (type.startsWith("audio/") || type.startsWith("x-audio/")) {
|
|
|
|
episode.set_url(QUrl::fromEncoded(
|
|
|
|
reader->attributes().value("url").toString().toAscii()));
|
|
|
|
}
|
|
|
|
Utilities::ConsumeCurrentElement(reader);
|
2014-05-29 13:11:49 +02:00
|
|
|
} else if (name == "author" && lower_namespace == kItunesNamespace) {
|
2014-02-07 16:34:20 +01:00
|
|
|
episode.set_author(reader->readElementText());
|
|
|
|
} else {
|
|
|
|
Utilities::ConsumeCurrentElement(reader);
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
break;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::EndElement:
|
2014-12-11 22:42:18 +01:00
|
|
|
if (!episode.publication_date().isValid()) {
|
|
|
|
episode.set_publication_date(QDateTime::currentDateTime());
|
|
|
|
}
|
2014-02-07 16:34:20 +01:00
|
|
|
if (!episode.url().isEmpty()) {
|
|
|
|
ret->add_episode(episode);
|
|
|
|
}
|
|
|
|
return;
|
2012-03-04 21:47:58 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
|
|
|
break;
|
2012-03-04 21:47:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-07 16:11:56 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
bool PodcastParser::ParseOpml(QXmlStreamReader* reader,
|
|
|
|
OpmlContainer* ret) const {
|
2012-03-07 16:11:56 +01:00
|
|
|
if (!Utilities::ParseUntilElement(reader, "body")) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ParseOutline(reader, ret);
|
|
|
|
|
|
|
|
// OPML files sometimes consist of a single top level container.
|
2014-02-07 16:34:20 +01:00
|
|
|
while (ret->feeds.count() == 0 && ret->containers.count() == 1) {
|
2012-03-07 16:11:56 +01:00
|
|
|
*ret = ret->containers[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
void PodcastParser::ParseOutline(QXmlStreamReader* reader,
|
|
|
|
OpmlContainer* ret) const {
|
2012-03-07 16:11:56 +01:00
|
|
|
while (!reader->atEnd()) {
|
|
|
|
QXmlStreamReader::TokenType type = reader->readNext();
|
|
|
|
switch (type) {
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::StartElement: {
|
|
|
|
const QStringRef name = reader->name();
|
|
|
|
if (name != "outline") {
|
|
|
|
Utilities::ConsumeCurrentElement(reader);
|
|
|
|
continue;
|
|
|
|
}
|
2012-03-07 16:11:56 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
QXmlStreamAttributes attributes = reader->attributes();
|
|
|
|
|
|
|
|
if (attributes.value("type").toString() == "rss") {
|
|
|
|
// Parse the feed and add it to this container
|
|
|
|
Podcast podcast;
|
|
|
|
podcast.set_description(attributes.value("description").toString());
|
|
|
|
podcast.set_title(attributes.value("text").toString());
|
|
|
|
podcast.set_image_url_large(QUrl::fromEncoded(
|
|
|
|
attributes.value("imageHref").toString().toAscii()));
|
|
|
|
podcast.set_url(QUrl::fromEncoded(
|
|
|
|
attributes.value("xmlUrl").toString().toAscii()));
|
|
|
|
ret->feeds.append(podcast);
|
|
|
|
|
|
|
|
// Consume any children and the EndElement.
|
|
|
|
Utilities::ConsumeCurrentElement(reader);
|
|
|
|
} else {
|
|
|
|
// Create a new child container
|
|
|
|
OpmlContainer child;
|
|
|
|
|
|
|
|
// Take the name from the fullname attribute first if it exists.
|
|
|
|
child.name = attributes.value("fullname").toString();
|
|
|
|
if (child.name.isEmpty()) {
|
|
|
|
child.name = attributes.value("text").toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse its contents and add it to this container
|
|
|
|
ParseOutline(reader, &child);
|
|
|
|
ret->containers.append(child);
|
2012-03-07 16:11:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
break;
|
2012-03-07 16:11:56 +01:00
|
|
|
}
|
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
case QXmlStreamReader::EndElement:
|
|
|
|
return;
|
2012-03-07 16:11:56 +01:00
|
|
|
|
2014-02-07 16:34:20 +01:00
|
|
|
default:
|
|
|
|
break;
|
2012-03-07 16:11:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|