2015-12-01 07:13:37 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
Copyright 2015, Nick Lanham <nick@afternight.org>
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "subsonicdynamicplaylist.h"
|
|
|
|
|
|
|
|
#include <QEventLoop>
|
|
|
|
#include <QFileInfo>
|
|
|
|
#include <QSslConfiguration>
|
2015-12-13 20:05:12 +01:00
|
|
|
#include <QUrlQuery>
|
2020-09-18 16:15:19 +02:00
|
|
|
#include <boost/scope_exit.hpp>
|
2015-12-01 07:13:37 +01:00
|
|
|
|
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/logging.h"
|
|
|
|
#include "core/network.h"
|
|
|
|
#include "core/taskmanager.h"
|
|
|
|
#include "core/timeconstants.h"
|
2015-12-04 02:25:00 +01:00
|
|
|
#include "core/waitforsignal.h"
|
2015-12-01 07:13:37 +01:00
|
|
|
#include "internet/core/internetplaylistitem.h"
|
2020-09-18 16:15:19 +02:00
|
|
|
#include "subsonicservice.h"
|
2015-12-01 23:46:10 +01:00
|
|
|
|
|
|
|
// 500 limit per subsonic api
|
|
|
|
const int SubsonicDynamicPlaylist::kMaxCount = 500;
|
2020-04-08 16:26:58 +02:00
|
|
|
const int SubsonicDynamicPlaylist::kDefaultAlbumCount = 10;
|
|
|
|
const int SubsonicDynamicPlaylist::kDefaultSongCount = 20;
|
2015-12-01 23:46:10 +01:00
|
|
|
const int SubsonicDynamicPlaylist::kDefaultOffset = 0;
|
|
|
|
|
2015-12-01 07:13:37 +01:00
|
|
|
SubsonicDynamicPlaylist::SubsonicDynamicPlaylist()
|
2020-09-18 16:15:19 +02:00
|
|
|
: type_(QueryType_Album), stat_(QueryStat_Newest), offset_(kDefaultOffset) {
|
2020-04-10 23:09:00 +02:00
|
|
|
service_ = InternetModel::Service<SubsonicService>();
|
|
|
|
}
|
2015-12-01 07:13:37 +01:00
|
|
|
|
|
|
|
SubsonicDynamicPlaylist::SubsonicDynamicPlaylist(const QString& name,
|
2020-09-18 16:15:19 +02:00
|
|
|
QueryType type, QueryStat stat)
|
2020-04-08 16:26:58 +02:00
|
|
|
: type_(type), stat_(stat), offset_(kDefaultOffset) {
|
2015-12-01 07:13:37 +01:00
|
|
|
set_name(name);
|
2020-04-10 23:09:00 +02:00
|
|
|
service_ = InternetModel::Service<SubsonicService>();
|
2015-12-01 07:13:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SubsonicDynamicPlaylist::Load(const QByteArray& data) {
|
|
|
|
QDataStream s(data);
|
|
|
|
s >> *this;
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:46:10 +01:00
|
|
|
void SubsonicDynamicPlaylist::Load(QueryStat stat) { stat_ = stat; }
|
2015-12-01 07:13:37 +01:00
|
|
|
|
|
|
|
QByteArray SubsonicDynamicPlaylist::Save() const {
|
|
|
|
QByteArray ret;
|
|
|
|
QDataStream s(&ret, QIODevice::WriteOnly);
|
|
|
|
s << *this;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copied from SubsonicService
|
2015-12-01 23:46:10 +01:00
|
|
|
QNetworkReply* SubsonicDynamicPlaylist::Send(QNetworkAccessManager& network,
|
|
|
|
const QUrl& url,
|
|
|
|
const bool usesslv3) {
|
2015-12-01 07:13:37 +01:00
|
|
|
QNetworkRequest request(url);
|
|
|
|
// Don't try and check the authenticity of the SSL certificate - it'll almost
|
|
|
|
// certainly be self-signed.
|
|
|
|
QSslConfiguration sslconfig = QSslConfiguration::defaultConfiguration();
|
|
|
|
sslconfig.setPeerVerifyMode(QSslSocket::VerifyNone);
|
|
|
|
if (usesslv3) {
|
|
|
|
sslconfig.setProtocol(QSsl::SslV3);
|
|
|
|
}
|
|
|
|
request.setSslConfiguration(sslconfig);
|
|
|
|
QNetworkReply* reply = network.get(request);
|
|
|
|
return reply;
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:46:10 +01:00
|
|
|
PlaylistItemList SubsonicDynamicPlaylist::Generate() {
|
2020-04-08 16:26:58 +02:00
|
|
|
switch (type_) {
|
|
|
|
case QueryType_Album:
|
|
|
|
return GenerateMoreAlbums(kDefaultAlbumCount);
|
|
|
|
case QueryType_Song:
|
|
|
|
return GenerateMoreSongs(kDefaultSongCount);
|
|
|
|
default:
|
2020-04-10 22:56:39 +02:00
|
|
|
qLog(Warning) << "Invalid playlist type";
|
|
|
|
return PlaylistItemList();
|
2020-04-08 16:26:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PlaylistItemList SubsonicDynamicPlaylist::GenerateMoreSongs(int count) {
|
2020-04-12 12:07:25 +02:00
|
|
|
const int task_id =
|
|
|
|
service_->app_->task_manager()->StartTask(tr("Fetching playlist items"));
|
|
|
|
TaskManager::ScopedTask task(task_id, service_->app_->task_manager());
|
|
|
|
|
2020-04-10 23:09:00 +02:00
|
|
|
QUrl url = service_->BuildRequestUrl("getRandomSongs");
|
2020-04-08 16:26:58 +02:00
|
|
|
QNetworkAccessManager network;
|
|
|
|
|
|
|
|
if (count > kMaxCount) count = kMaxCount;
|
|
|
|
|
|
|
|
QUrlQuery url_query(url.query());
|
|
|
|
url_query.addQueryItem("size", QString::number(count));
|
|
|
|
url.setQuery(url_query);
|
|
|
|
|
|
|
|
PlaylistItemList items;
|
|
|
|
|
2020-04-10 23:09:00 +02:00
|
|
|
QNetworkReply* reply = Send(network, url, service_->usesslv3_);
|
2020-04-08 16:26:58 +02:00
|
|
|
WaitForSignal(reply, SIGNAL(finished()));
|
|
|
|
|
|
|
|
reply->deleteLater();
|
|
|
|
|
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
|
|
|
qLog(Warning) << "HTTP error returned from Subsonic:"
|
|
|
|
<< reply->errorString() << ", url:" << url.toString();
|
|
|
|
return items; // empty
|
|
|
|
}
|
|
|
|
|
|
|
|
QXmlStreamReader reader(reply);
|
|
|
|
reader.readNextStartElement();
|
|
|
|
if (reader.name() != "subsonic-response") {
|
|
|
|
qLog(Warning) << "Not a subsonic-response, aboring playlist fetch";
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reader.attributes().value("status") != "ok") {
|
|
|
|
reader.readNextStartElement();
|
|
|
|
int error = reader.attributes().value("code").toString().toInt();
|
|
|
|
qLog(Warning) << "An error occurred fetching data. Code: " << error
|
|
|
|
<< " Message: "
|
|
|
|
<< reader.attributes().value("message").toString();
|
2020-04-09 16:51:13 +02:00
|
|
|
return items;
|
2020-04-08 16:26:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
reader.readNextStartElement();
|
|
|
|
if (reader.name() != "randomSongs") {
|
|
|
|
qLog(Warning) << "randomSongs tag expected. Aborting playlist fetch";
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (reader.readNextStartElement()) {
|
|
|
|
if (reader.name() != "song") {
|
2020-04-09 16:51:13 +02:00
|
|
|
qLog(Warning) << "song tag expected. Skipping song";
|
|
|
|
reader.skipCurrentElement();
|
|
|
|
continue;
|
2020-04-08 16:26:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-13 07:10:29 +02:00
|
|
|
Song song = service_->ReadSong(reader);
|
2020-04-08 16:26:58 +02:00
|
|
|
|
|
|
|
items << std::shared_ptr<PlaylistItem>(
|
2020-04-10 23:09:00 +02:00
|
|
|
new InternetPlaylistItem(service_, song));
|
2020-04-08 16:26:58 +02:00
|
|
|
|
|
|
|
reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
return items;
|
2015-12-01 23:46:10 +01:00
|
|
|
}
|
2015-12-01 07:13:37 +01:00
|
|
|
|
2020-04-08 16:26:58 +02:00
|
|
|
PlaylistItemList SubsonicDynamicPlaylist::GenerateMoreAlbums(int count) {
|
2020-04-12 12:07:25 +02:00
|
|
|
const int task_id =
|
|
|
|
service_->app_->task_manager()->StartTask(tr("Fetching playlist items"));
|
|
|
|
TaskManager::ScopedTask task(task_id, service_->app_->task_manager());
|
|
|
|
|
2020-04-10 23:09:00 +02:00
|
|
|
QUrl url = service_->BuildRequestUrl("getAlbumList");
|
2015-12-01 07:13:37 +01:00
|
|
|
QNetworkAccessManager network;
|
|
|
|
|
2015-12-01 23:46:10 +01:00
|
|
|
if (count > kMaxCount) count = kMaxCount;
|
2015-12-01 07:13:37 +01:00
|
|
|
|
2016-04-20 00:22:23 +02:00
|
|
|
QUrlQuery url_query(url.query());
|
2015-12-13 20:05:12 +01:00
|
|
|
url_query.addQueryItem("type", GetTypeString());
|
|
|
|
url_query.addQueryItem("size", QString::number(count));
|
|
|
|
url_query.addQueryItem("offset", QString::number(offset_));
|
|
|
|
url.setQuery(url_query);
|
2015-12-01 07:13:37 +01:00
|
|
|
|
|
|
|
PlaylistItemList items;
|
|
|
|
|
2020-04-10 23:09:00 +02:00
|
|
|
QNetworkReply* reply = Send(network, url, service_->usesslv3_);
|
2015-12-04 02:25:00 +01:00
|
|
|
WaitForSignal(reply, SIGNAL(finished()));
|
2015-12-01 07:13:37 +01:00
|
|
|
|
2015-12-01 23:46:10 +01:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2015-12-01 07:13:37 +01:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2015-12-01 23:46:10 +01:00
|
|
|
qLog(Warning) << "HTTP error returned from Subsonic:"
|
|
|
|
<< reply->errorString() << ", url:" << url.toString();
|
|
|
|
return items; // empty
|
2015-12-01 07:13:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
QXmlStreamReader reader(reply);
|
|
|
|
reader.readNextStartElement();
|
|
|
|
if (reader.name() != "subsonic-response") {
|
|
|
|
qLog(Warning) << "Not a subsonic-response, aboring playlist fetch";
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reader.attributes().value("status") != "ok") {
|
|
|
|
reader.readNextStartElement();
|
|
|
|
int error = reader.attributes().value("code").toString().toInt();
|
2019-08-22 05:43:16 +02:00
|
|
|
qLog(Warning) << "An error occurred fetching data. Code: " << error
|
2015-12-01 23:46:10 +01:00
|
|
|
<< " Message: "
|
|
|
|
<< reader.attributes().value("message").toString();
|
2020-04-09 16:51:13 +02:00
|
|
|
return items;
|
2015-12-01 07:13:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
reader.readNextStartElement();
|
|
|
|
if (reader.name() != "albumList") {
|
|
|
|
qLog(Warning) << "albumList tag expected. Aboring playlist fetch";
|
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (reader.readNextStartElement()) {
|
|
|
|
if (reader.name() != "album") {
|
2020-04-09 16:51:13 +02:00
|
|
|
qLog(Warning) << "album tag expected. Skipping album";
|
|
|
|
reader.skipCurrentElement();
|
|
|
|
continue;
|
2015-12-01 07:13:37 +01:00
|
|
|
}
|
|
|
|
|
2015-12-01 23:46:10 +01:00
|
|
|
qLog(Debug) << "Getting album: "
|
|
|
|
<< reader.attributes().value("album").toString();
|
2020-04-10 23:09:00 +02:00
|
|
|
GetAlbum(items, reader.attributes().value("id").toString(), network,
|
|
|
|
service_->usesslv3_);
|
2015-12-01 07:13:37 +01:00
|
|
|
reader.skipCurrentElement();
|
|
|
|
}
|
2015-12-01 23:46:10 +01:00
|
|
|
offset_ += count;
|
2015-12-01 07:13:37 +01:00
|
|
|
return items;
|
|
|
|
}
|
|
|
|
|
2020-04-10 23:09:00 +02:00
|
|
|
void SubsonicDynamicPlaylist::GetAlbum(PlaylistItemList& list, QString id,
|
2015-12-01 23:46:10 +01:00
|
|
|
QNetworkAccessManager& network,
|
|
|
|
const bool usesslv3) {
|
2020-04-10 23:09:00 +02:00
|
|
|
QUrl url = service_->BuildRequestUrl("getAlbum");
|
2016-04-20 00:22:23 +02:00
|
|
|
QUrlQuery url_query(url.query());
|
2015-12-13 20:05:12 +01:00
|
|
|
url_query.addQueryItem("id", id);
|
2020-04-10 23:09:00 +02:00
|
|
|
if (service_->IsAmpache()) {
|
2015-12-13 20:05:12 +01:00
|
|
|
url_query.addQueryItem("ampache", "1");
|
2015-12-08 23:15:56 +01:00
|
|
|
}
|
2015-12-13 20:05:12 +01:00
|
|
|
url.setQuery(url_query);
|
2015-12-01 07:13:37 +01:00
|
|
|
QNetworkReply* reply = Send(network, url, usesslv3);
|
2015-12-04 02:25:00 +01:00
|
|
|
WaitForSignal(reply, SIGNAL(finished()));
|
2015-12-01 23:46:10 +01:00
|
|
|
reply->deleteLater();
|
|
|
|
|
2015-12-01 07:13:37 +01:00
|
|
|
if (reply->error() != QNetworkReply::NoError) {
|
2015-12-01 23:46:10 +01:00
|
|
|
qLog(Warning) << "HTTP error returned from Subsonic:"
|
|
|
|
<< reply->errorString() << ", url:" << url.toString();
|
2015-12-01 07:13:37 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-01 23:46:10 +01:00
|
|
|
QXmlStreamReader reader(reply);
|
2015-12-01 07:13:37 +01:00
|
|
|
reader.readNextStartElement();
|
|
|
|
|
|
|
|
if (reader.name() != "subsonic-response") {
|
|
|
|
qLog(Warning) << "Not a subsonic-response. Aborting playlist fetch.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reader.attributes().value("status") != "ok") {
|
|
|
|
qLog(Warning) << "Status not okay. Aborting playlist fetch.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read album information
|
|
|
|
reader.readNextStartElement();
|
|
|
|
if (reader.name() != "album") {
|
|
|
|
qLog(Warning) << "album tag expected. Aborting playlist fetch.";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString album_artist = reader.attributes().value("artist").toString();
|
|
|
|
|
|
|
|
// Read song information
|
|
|
|
while (reader.readNextStartElement()) {
|
|
|
|
if (reader.name() != "song") {
|
2020-04-09 16:51:13 +02:00
|
|
|
qLog(Warning) << "song tag expected. Skipping song";
|
|
|
|
reader.skipCurrentElement();
|
|
|
|
continue;
|
2015-12-01 07:13:37 +01:00
|
|
|
}
|
|
|
|
|
2020-05-13 07:10:29 +02:00
|
|
|
Song song = service_->ReadSong(reader);
|
2015-12-01 07:13:37 +01:00
|
|
|
song.set_albumartist(album_artist);
|
2015-12-08 23:15:56 +01:00
|
|
|
|
2015-12-01 07:13:37 +01:00
|
|
|
list << std::shared_ptr<PlaylistItem>(
|
2020-04-10 23:09:00 +02:00
|
|
|
new InternetPlaylistItem(service_, song));
|
2015-12-01 07:13:37 +01:00
|
|
|
|
|
|
|
reader.skipCurrentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream& operator<<(QDataStream& s, const SubsonicDynamicPlaylist& p) {
|
2020-04-08 16:26:58 +02:00
|
|
|
s << quint8(p.stat_) << quint8(p.type_);
|
2015-12-01 07:13:37 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDataStream& operator>>(QDataStream& s, SubsonicDynamicPlaylist& p) {
|
2020-04-08 16:26:58 +02:00
|
|
|
quint8 stat, type;
|
|
|
|
s >> stat >> type;
|
2015-12-01 07:13:37 +01:00
|
|
|
p.stat_ = SubsonicDynamicPlaylist::QueryStat(stat);
|
2020-04-08 16:26:58 +02:00
|
|
|
p.type_ = SubsonicDynamicPlaylist::QueryType(type);
|
2015-12-01 07:13:37 +01:00
|
|
|
return s;
|
|
|
|
}
|