]> git.sur5r.net Git - minitube/blobdiff - src/ytchannel.cpp
Update upstream source from tag 'upstream/3.6'
[minitube] / src / ytchannel.cpp
index a80879af1736c586325db3bbf898cd6542aefafe..1d83b2a7c11aa8dff802f025b3d08ee86c63d7cf 100644 (file)
@@ -19,39 +19,30 @@ along with Minitube.  If not, see <http://www.gnu.org/licenses/>.
 $END_LICENSE */
 
 #include "ytchannel.h"
-#include "networkaccess.h"
 #include "database.h"
+#include "http.h"
+#include "httputils.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 "ivchannel.h"
+#include "videoapi.h"
+#include "ytjschannel.h"
 
-YTChannel::YTChannel(const QString &channelId, QObject *parent) : QObject(parent),
-    id(0),
-    channelId(channelId),
-    loadingThumbnail(false),
-    notifyCount(0),
-    checked(0),
-    watched(0),
-    loaded(0),
-    loading(false) { }
+YTChannel::YTChannel(const QString &channelId, QObject *parent)
+    : QObject(parent), id(0), channelId(channelId), loadingThumbnail(false), notifyCount(0),
+      checked(0), watched(0), loaded(0), loading(false) {}
 
-QHash<QString, YTChannel*> YTChannel::cache;
+QHash<QString, YTChannel *> YTChannel::cache;
 
-YTChannelYTChannel::forId(const QString &channelId) {
+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);
@@ -61,7 +52,7 @@ YTChannel* YTChannel::forId(const QString &channelId) {
     bool success = query.exec();
     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
 
-    YTChannelchannel = 0;
+    YTChannel *channel = 0;
     if (query.next()) {
         // Change userId to ChannelId
 
@@ -75,7 +66,7 @@ YTChannel* YTChannel::forId(const QString &channelId) {
         channel->checked = query.value(6).toUInt();
         channel->loaded = query.value(7).toUInt();
         channel->thumbnail = QPixmap(channel->getThumbnailLocation());
-        channel->thumbnail.setDevicePixelRatio(IconUtils::maxSupportedPixelRatio());
+        channel->thumbnail.setDevicePixelRatio(2.0);
         channel->maybeLoadfromAPI();
         cache.insert(channelId, channel);
     }
@@ -93,51 +84,55 @@ 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");
-    }
-
-#else
-
-    QUrl url("http://gdata.youtube.com/feeds/api/users/" + channelId);
-    {
-        QUrlQueryHelper urlHelper(url);
-        urlHelper.addQueryItem("v", "2");
+    if (VideoAPI::impl() == VideoAPI::YT3) {
+        QUrl url = YT3::instance().method("channels");
+        QUrlQuery q(url);
+        q.addQueryItem("id", channelId);
+        q.addQueryItem("part", "snippet");
+        url.setQuery(q);
+
+        QObject *reply = HttpUtils::yt().get(url);
+        connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResponse(QByteArray)));
+        connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
+    } else if (VideoAPI::impl() == VideoAPI::IV) {
+        auto ivChannel = new IVChannel(channelId);
+        connect(ivChannel, &IVChannel::error, this, &YTChannel::requestError);
+        connect(ivChannel, &IVChannel::loaded, this, [this, ivChannel] {
+            displayName = ivChannel->getDisplayName();
+            description = ivChannel->getDescription();
+            thumbnailUrl = ivChannel->getThumbnailUrl();
+            ivChannel->deleteLater();
+            emit infoLoaded();
+            storeInfo();
+            loading = false;
+        });
+    } else if (VideoAPI::impl() == VideoAPI::JS) {
+        auto ivChannel = new YTJSChannel(channelId);
+        connect(ivChannel, &YTJSChannel::error, this, &YTChannel::requestError);
+        connect(ivChannel, &YTJSChannel::loaded, this, [this, ivChannel] {
+            displayName = ivChannel->getDisplayName();
+            description = ivChannel->getDescription();
+            thumbnailUrl = ivChannel->getThumbnailUrl();
+            ivChannel->deleteLater();
+            emit infoLoaded();
+            storeInfo();
+            loading = false;
+        });
     }
-
-#endif
-
-    QObject *reply = The::http()->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;
-            }
-        }
+    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();
@@ -145,51 +140,20 @@ void YTChannel::parseResponse(const QByteArray &bytes) {
     loading = false;
 }
 
-#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();
-    }
-
-    emit infoLoaded();
-    storeInfo();
-    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/";
+const QString &YTChannel::getThumbnailDir() {
+    static const QString thumbDir =
+            QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/channels/";
     return thumbDir;
 }
 
@@ -200,7 +164,8 @@ QString YTChannel::getThumbnailLocation() {
 QString YTChannel::latestVideoId() {
     QSqlDatabase db = Database::instance().getConnection();
     QSqlQuery query(db);
-    query.prepare("select video_id from subscriptions_videos where user_id=? order by published desc limit 1");
+    query.prepare("select video_id from subscriptions_videos where user_id=? order by published "
+                  "desc limit 1");
     query.bindValue(0, channelId);
     bool success = query.exec();
     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
@@ -214,7 +179,7 @@ void YTChannel::unsubscribe() {
 
 void YTChannel::storeThumbnail(const QByteArray &bytes) {
     thumbnail.loadFromData(bytes);
-    qreal maxRatio = IconUtils::maxSupportedPixelRatio();
+    qreal maxRatio = 2.0;
     thumbnail.setDevicePixelRatio(maxRatio);
     const int maxWidth = 88 * maxRatio;
 
@@ -237,9 +202,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;
 }
@@ -312,8 +277,7 @@ bool YTChannel::isSubscribed(const QString &channelId) {
     query.bindValue(0, channelId);
     bool success = query.exec();
     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
-    if (query.next())
-        return query.value(0).toInt() > 0;
+    if (query.next()) return query.value(0).toInt() > 0;
     return false;
 }
 
@@ -342,7 +306,8 @@ void YTChannel::updateWatched() {
 
     QSqlDatabase db = Database::instance().getConnection();
     QSqlQuery query(db);
-    query.prepare("update subscriptions set watched=?, notify_count=0, views=views+1 where user_id=?");
+    query.prepare(
+            "update subscriptions set watched=?, notify_count=0, views=views+1 where user_id=?");
     query.bindValue(0, now);
     query.bindValue(1, channelId);
     bool success = query.exec();
@@ -350,8 +315,7 @@ void YTChannel::updateWatched() {
 }
 
 void YTChannel::storeNotifyCount(int count) {
-    if (notifyCount != count)
-        emit notifyCountChanged();
+    if (notifyCount != count) emit notifyCountChanged();
     notifyCount = count;
 
     QSqlDatabase db = Database::instance().getConnection();