]> git.sur5r.net Git - minitube/blobdiff - src/ytchannel.cpp
New upstream version 2.9
[minitube] / src / ytchannel.cpp
index a80879af1736c586325db3bbf898cd6542aefafe..2c0a745659eff552baf884a7424cc88d02ce8913 100644 (file)
@@ -19,21 +19,14 @@ along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
 $END_LICENSE */
 
 #include "ytchannel.h"
-#include "networkaccess.h"
+#include "http.h"
+#include "httputils.h"
 #include "database.h"
 #include <QtSql>
 
-#ifdef APP_YT3
 #include "yt3.h"
-#include <QtScript>
-#endif
-#include "compatibility/qurlqueryhelper.h"
-#include "compatibility/pathsservice.h"
-#include "iconutils.h"
 
-namespace The {
-NetworkAccess* http();
-}
+#include "iconutils.h"
 
 YTChannel::YTChannel(const QString &channelId, QObject *parent) : QObject(parent),
     id(0),
@@ -50,8 +43,8 @@ QHash<QString, YTChannel*> YTChannel::cache;
 YTChannel* YTChannel::forId(const QString &channelId) {
     if (channelId.isEmpty()) return 0;
 
-    if (cache.contains(channelId))
-        return cache.value(channelId);
+    auto i = cache.constFind(channelId);
+    if (i != cache.constEnd()) return i.value();
 
     QSqlDatabase db = Database::instance().getConnection();
     QSqlQuery query(db);
@@ -93,81 +86,29 @@ void YTChannel::maybeLoadfromAPI() {
 
     loading = true;
 
-#ifdef APP_YT3
-
     QUrl url = YT3::instance().method("channels");
-    {
-        QUrlQueryHelper urlHelper(url);
-        urlHelper.addQueryItem("id", channelId);
-        urlHelper.addQueryItem("part", "snippet");
-    }
+    QUrlQuery q(url);
+    q.addQueryItem("id", channelId);
+    q.addQueryItem("part", "snippet");
+    url.setQuery(q);
 
-#else
-
-    QUrl url("http://gdata.youtube.com/feeds/api/users/" + channelId);
-    {
-        QUrlQueryHelper urlHelper(url);
-        urlHelper.addQueryItem("v", "2");
-    }
-
-#endif
-
-    QObject *reply = The::http()->get(url);
+    QObject *reply = HttpUtils::yt().get(url);
     connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResponse(QByteArray)));
-    connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
-}
-
-#ifdef APP_YT3
-
-void YTChannel::parseResponse(const QByteArray &bytes) {
-    QScriptEngine engine;
-    QScriptValue json = engine.evaluate("(" + QString::fromUtf8(bytes) + ")");
-    QScriptValue items = json.property("items");
-    if (items.isArray()) {
-        QScriptValueIterator it(items);
-        while (it.hasNext()) {
-            it.next();
-            QScriptValue item = it.value();
-            // For some reason the array has an additional element containing its size.
-            if (item.isObject()) {
-                QScriptValue snippet = item.property("snippet");
-                displayName = snippet.property("title").toString();
-                description = snippet.property("description").toString();
-                QScriptValue thumbnails = snippet.property("thumbnails");
-                thumbnailUrl = thumbnails.property("medium").property("url").toString();
-                qDebug() << displayName << description << thumbnailUrl;
-            }
-        }
-    }
-
-    emit infoLoaded();
-    storeInfo();
-    loading = false;
+    connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
 }
 
-#else
-
 void YTChannel::parseResponse(const QByteArray &bytes) {
-    QXmlStreamReader xml(bytes);
-    xml.readNextStartElement();
-    if (xml.name() == QLatin1String("entry"))
-        while(xml.readNextStartElement()) {
-            const QStringRef n = xml.name();
-            if (n == QLatin1String("summary"))
-                description = xml.readElementText().simplified();
-            else if (n == QLatin1String("title"))
-                displayName = xml.readElementText();
-            else if (n == QLatin1String("thumbnail")) {
-                thumbnailUrl = xml.attributes().value("url").toString();
-                xml.skipCurrentElement();
-            } else if (n == QLatin1String("username"))
-                userName = xml.readElementText();
-            else xml.skipCurrentElement();
-        }
-
-    if (xml.hasError()) {
-        emit error(xml.errorString());
-        qWarning() << xml.errorString();
+    QJsonDocument doc = QJsonDocument::fromJson(bytes);
+    QJsonObject obj = doc.object();
+    const QJsonArray items = obj["items"].toArray();
+    for (const QJsonValue &v : items) {
+        QJsonObject item = v.toObject();
+        QJsonObject snippet = item["snippet"].toObject();
+        displayName = snippet["title"].toString();
+        description = snippet["description"].toString();
+        QJsonObject thumbnails = snippet["thumbnails"].toObject();
+        thumbnailUrl = thumbnails["medium"].toObject()["url"].toString();
+        qDebug() << displayName << description << thumbnailUrl;
     }
 
     emit infoLoaded();
@@ -175,21 +116,19 @@ void YTChannel::parseResponse(const QByteArray &bytes) {
     loading = false;
 }
 
-#endif
-
 void YTChannel::loadThumbnail() {
     if (loadingThumbnail) return;
     if (thumbnailUrl.isEmpty()) return;
     loadingThumbnail = true;
 
     QUrl url(thumbnailUrl);
-    QObject *reply = The::http()->get(url);
+    QObject *reply = HttpUtils::yt().get(url);
     connect(reply, SIGNAL(data(QByteArray)), SLOT(storeThumbnail(QByteArray)));
-    connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
+    connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
 }
 
 const QString & YTChannel::getThumbnailDir() {
-    static const QString thumbDir = Paths::getCacheLocation() + "/channels/";
+    static const QString thumbDir = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/channels/";
     return thumbDir;
 }
 
@@ -237,9 +176,9 @@ void YTChannel::storeThumbnail(const QByteArray &bytes) {
     loadingThumbnail = false;
 }
 
-void YTChannel::requestError(QNetworkReply *reply) {
-    emit error(reply->errorString());
-    qWarning() << reply->errorString();
+void YTChannel::requestError(const QString &message) {
+    emit error(message);
+    qWarning() << message;
     loading = false;
     loadingThumbnail = false;
 }