File indexing completed on 2025-01-19 04:23:50
0001 #include "nextmusic.h" 0002 #include <QDomDocument> 0003 #include <QJsonDocument> 0004 #include <QJsonObject> 0005 #include <QNetworkReply> 0006 #include <QNetworkRequest> 0007 #include <QUrl> 0008 #include <QVariantMap> 0009 0010 #include <MauiKit3/FileBrowsing/downloader.h> 0011 #include <MauiKit3/FileBrowsing/fm.h> 0012 0013 //static const inline QNetworkRequest formRequest(const QUrl &url, const QString &user, const QString &password) 0014 //{ 0015 // if (!url.isValid() && !user.isEmpty() && !password.isEmpty()) 0016 // return QNetworkRequest(); 0017 0018 // const QString concatenated = QString("%1:%2").arg(user, password); 0019 // const QByteArray data = concatenated.toLocal8Bit().toBase64(); 0020 // const QString headerData = "Basic " + data; 0021 0022 // // Construct new QNetworkRequest with prepared header values 0023 // QNetworkRequest newRequest(url); 0024 0025 // newRequest.setRawHeader(QString("Authorization").toLocal8Bit(), headerData.toLocal8Bit()); 0026 // // newRequest.setRawHeader(QByteArrayLiteral("OCS-APIREQUEST"), QByteArrayLiteral("true")); 0027 0028 // qDebug() << "headers" << newRequest.rawHeaderList() << newRequest.url(); 0029 0030 // return newRequest; 0031 //} 0032 0033 const QString NextMusic::API = QStringLiteral("/index.php/apps/music/api/"); 0034 0035 NextMusic::NextMusic(QObject *parent) 0036 : AbstractMusicProvider(parent) 0037 { 0038 } 0039 0040 QVariantList NextMusic::getAlbumsList() const 0041 { 0042 return this->m_albums; 0043 } 0044 0045 QVariantList NextMusic::getArtistsList() const 0046 { 0047 qDebug() << "ASKING FOR ARISTS" << this->m_artists; 0048 return this->m_artists; 0049 } 0050 0051 FMH::MODEL_LIST NextMusic::parseCollection(const QByteArray &array) 0052 { 0053 FMH::MODEL_LIST res; 0054 // qDebug()<< "trying to parse array" << array; 0055 QJsonParseError jsonParseError; 0056 QJsonDocument jsonResponse = QJsonDocument::fromJson(static_cast<QString>(array).toUtf8(), &jsonParseError); 0057 0058 if (jsonParseError.error != QJsonParseError::NoError) { 0059 qDebug() << "ERROR PARSING" << array; 0060 return res; 0061 } 0062 0063 const auto data = jsonResponse.toVariant(); 0064 0065 if (data.isNull() || !data.isValid()) 0066 return res; 0067 0068 const auto list = data.toList(); 0069 qDebug() << "SOFAR GOOD PARSING"; 0070 0071 if (!list.isEmpty()) { 0072 for (const auto &item : list) { 0073 const auto map = item.toMap(); 0074 const auto artist = map.value("name").toString(); 0075 const auto artistId = map.value("id").toString(); 0076 0077 this->m_artists.append(QVariantMap{{"artist", artist}, {"id", artistId}}); 0078 0079 qDebug() << "ARTIST" << artist << artistId; 0080 0081 const auto albumsList = map.value("albums").toList(); 0082 for (const auto &albumItem : albumsList) { 0083 const auto albumMap = albumItem.toMap(); 0084 const auto album = albumMap.value("name").toString(); 0085 const auto albumId = albumMap.value("id").toString(); 0086 const auto albumYear = albumMap.value("year").toString(); 0087 const auto albumCover = albumMap.value("cover").toString(); 0088 0089 this->m_albums.append(QVariantMap{{"album", album}, {"artist", artist}, {"release_date", albumYear}, {"artwork", albumCover}, {"id", albumId}}); 0090 0091 qDebug() << "ARTIST && ALBUM" << artist << album << artistId; 0092 0093 const auto tracksList = albumMap.value("tracks").toList(); 0094 for (const auto &trackItem : tracksList) { 0095 const auto trackMap = trackItem.toMap(); 0096 0097 const auto title = trackMap.value("title").toString(); 0098 const auto track = trackMap.value("number").toString(); 0099 // const auto id = trackMap.value("id").toString(); 0100 0101 const auto filesMap = trackMap.value("files").toMap(); 0102 const auto keys = filesMap.keys(); 0103 for (const auto &fileKey : keys) { 0104 // const auto mime = fileKey; 0105 const auto url = filesMap[fileKey].toString(); 0106 0107 const auto trackModel = FMH::MODEL({{FMH::MODEL_KEY::ID, url}, 0108 {FMH::MODEL_KEY::TITLE, title}, 0109 {FMH::MODEL_KEY::TRACK, track}, 0110 {FMH::MODEL_KEY::ALBUM, album}, 0111 {FMH::MODEL_KEY::ARTIST, artist}, 0112 {FMH::MODEL_KEY::ARTWORK, albumCover}, 0113 {FMH::MODEL_KEY::RELEASEDATE, albumYear}, 0114 {FMH::MODEL_KEY::SOURCE, this->m_provider}}); 0115 0116 this->m_tracks.insert(url, trackModel); 0117 res << trackModel; 0118 } 0119 } 0120 } 0121 } 0122 } 0123 0124 qDebug() << res; 0125 return res; 0126 } 0127 0128 FMH::MODEL NextMusic::getTrackItem(const QString &id) 0129 { 0130 return this->m_tracks.value(id); 0131 } 0132 0133 void NextMusic::getTrackPath(const QString &id) 0134 { 0135 QUrl relativeUrl("../.." + NextMusic::API + QString("file/%1/path").arg(id)); 0136 auto url = QUrl(this->m_provider).resolved(relativeUrl); 0137 0138 QString concatenated = this->m_user + ":" + this->m_password; 0139 QByteArray data = concatenated.toLocal8Bit().toBase64(); 0140 QString headerData = "Basic " + data; 0141 0142 QMap<QString, QString> header{{"Authorization", headerData.toLocal8Bit()}}; 0143 0144 const auto downloader = new FMH::Downloader; 0145 connect(downloader, &FMH::Downloader::dataReady, [this, id, downloader](QByteArray array) { 0146 QJsonParseError jsonParseError; 0147 QJsonDocument jsonResponse = QJsonDocument::fromJson(static_cast<QString>(array).toUtf8(), &jsonParseError); 0148 0149 if (jsonParseError.error != QJsonParseError::NoError) { 0150 qDebug() << "ERROR PARSING"; 0151 return; 0152 } 0153 0154 const auto data = jsonResponse.toVariant(); 0155 0156 if (data.isNull() || !data.isValid()) 0157 return; 0158 0159 const auto map = data.toMap(); 0160 auto path = map["path"].toString(); 0161 const auto url = this->provider() + (path.startsWith("/") ? path.remove(0, 1) : path); 0162 Q_EMIT this->trackPathReady(id, url); 0163 0164 downloader->deleteLater(); 0165 }); 0166 0167 downloader->getArray(url, header); 0168 } 0169 0170 void NextMusic::getCollection(const std::initializer_list<QString> ¶meters) 0171 { 0172 Q_UNUSED(parameters) 0173 0174 QUrl relativeUrl("../.." + NextMusic::API + "collection"); 0175 auto url = QUrl(this->m_provider).resolved(relativeUrl); 0176 0177 QString concatenated = this->m_user + ":" + this->m_password; 0178 QByteArray data = concatenated.toLocal8Bit().toBase64(); 0179 QString headerData = "Basic " + data; 0180 0181 QMap<QString, QString> header{{"Authorization", headerData.toLocal8Bit()}}; 0182 0183 const auto downloader = new FMH::Downloader; 0184 connect(downloader, &FMH::Downloader::dataReady, [this, downloader](QByteArray array) { 0185 qDebug() << "FINISHED REQUEST WITH RESPONSEC : " << array; 0186 0187 const auto data = this->parseCollection(array); 0188 Q_EMIT this->collectionReady(data); 0189 downloader->deleteLater(); 0190 }); 0191 0192 downloader->getArray(url, header); 0193 } 0194 0195 void NextMusic::getTracks() 0196 { 0197 } 0198 0199 void NextMusic::getTrack(const QString &id) 0200 { 0201 Q_UNUSED(id) 0202 } 0203 0204 void NextMusic::getArtists() 0205 { 0206 } 0207 0208 void NextMusic::getArtist(const QString &id) 0209 { 0210 Q_UNUSED(id) 0211 } 0212 0213 void NextMusic::getAlbums() 0214 { 0215 } 0216 0217 void NextMusic::getAlbum(const QString &id) 0218 { 0219 Q_UNUSED(id) 0220 } 0221 0222 void NextMusic::getPlaylists() 0223 { 0224 } 0225 0226 void NextMusic::getPlaylist(const QString &id) 0227 { 0228 Q_UNUSED(id) 0229 } 0230 0231 void NextMusic::getFolders() 0232 { 0233 } 0234 0235 void NextMusic::getFolder(const QString &id) 0236 { 0237 Q_UNUSED(id) 0238 }