File indexing completed on 2024-05-12 04:45:57

0001 /*
0002    Babe - tiny music player
0003    Copyright (C) 2017  Camilo Higuita
0004    This program is free software; you can redistribute it and/or modify
0005    it under the terms of the GNU General Public License as published by
0006    the Free Software Foundation; either version 3 of the License, or
0007    (at your option) any later version.
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0011    GNU General Public License for more details.
0012    You should have received a copy of the GNU General Public License
0013    along with this program; if not, write to the Free Software Foundation,
0014    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
0015 
0016    */
0017 
0018 
0019 #include "youtube.h"
0020 
0021 #include <QUrl>
0022 #include <QJsonObject>
0023 #include <QJsonDocument>
0024 #include <QVariantMap>
0025 
0026 #include <MauiKit3/FileBrowsing/downloader.h>
0027 
0028 YouTube::YouTube(QObject *parent) : QObject(parent)
0029 {
0030 
0031 }
0032 
0033 void YouTube::getQuery(const QString &query, const int &limit)
0034 {
0035     QUrl encodedQuery(query);
0036     encodedQuery.toEncoded(QUrl::FullyEncoded);
0037 
0038     auto url = this->API;
0039 
0040     url.append("q="+encodedQuery.toString());
0041     url.append(QString("&maxResults=%1&part=snippet").arg(QString::number(limit)));
0042     url.append("&key="+ this->KEY);
0043 
0044     qDebug()<< url;
0045 
0046     auto downloader = new FMH::Downloader;
0047 
0048     connect(downloader, &FMH::Downloader::dataReady, [this, downloader](QByteArray array)
0049     {
0050         this->packQueryResults(array);
0051         downloader->deleteLater();
0052     });
0053 
0054     downloader->getArray(url);
0055 }
0056 
0057 bool YouTube::packQueryResults(const QByteArray &array)
0058 {
0059     QJsonParseError jsonParseError;
0060     QJsonDocument jsonResponse = QJsonDocument::fromJson(static_cast<QString>(array).toUtf8(), &jsonParseError);
0061 
0062     if (jsonParseError.error != QJsonParseError::NoError)
0063         return false;
0064 
0065     if (!jsonResponse.isObject())
0066         return false;
0067 
0068     QJsonObject mainJsonObject(jsonResponse.object());
0069     auto data = mainJsonObject.toVariantMap();
0070     auto items = data.value("items").toList();
0071 
0072     if(items.isEmpty()) return false;
0073 
0074     FMH::MODEL_LIST res;
0075 
0076     for(auto item : items)
0077     {
0078         auto itemMap = item.toMap().value("id").toMap();
0079         auto id = itemMap.value("videoId").toString();
0080         auto url = "https://www.youtube.com/embed/"+id;
0081 
0082         auto snippet = item.toMap().value("snippet").toMap();
0083 
0084         auto comment = snippet.value("description").toString();
0085         auto title = snippet.value("title").toString();
0086         auto artwork = snippet.value("thumbnails").toMap().value("high").toMap().value("url").toString();
0087 
0088         if(!id.isEmpty())
0089         {
0090             res <<   FMH::MODEL {
0091             {FMH::MODEL_KEY::ID, id},
0092             {FMH::MODEL_KEY::URL, url},
0093             {FMH::MODEL_KEY::LABEL, title},
0094             {FMH::MODEL_KEY::TITLE, title},
0095             {FMH::MODEL_KEY::THUMBNAIL, artwork},
0096             {FMH::MODEL_KEY::COMMENT, comment}
0097         };
0098         }
0099     }
0100 
0101     Q_EMIT this->queryResultsReady(res);
0102     return true;
0103 }
0104 
0105 void YouTube::setKey(const QString &key)
0106 {
0107     this->KEY = key;
0108 }
0109 
0110 QString YouTube::getKey() const
0111 {
0112     return this->KEY;
0113 }
0114