]> git.sur5r.net Git - minitube/blobdiff - src/ytchannel.cpp
New upstream version 3.1
[minitube] / src / ytchannel.cpp
index ad652415b297258c6295598d7197675a96d59f8b..bfc2d42e101105b311b7cff6f09722b727bf527e 100644 (file)
@@ -19,36 +19,26 @@ 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
 
-namespace The {
-NetworkAccess* http();
-}
+#include "iconutils.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);
@@ -58,7 +48,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
 
@@ -72,6 +62,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(2.0);
         channel->maybeLoadfromAPI();
         cache.insert(channelId, channel);
     }
@@ -89,93 +80,29 @@ void YTChannel::maybeLoadfromAPI() {
 
     loading = true;
 
-#ifdef APP_YT3
-
     QUrl url = YT3::instance().method("channels");
-#if QT_VERSION >= 0x050000
-    {
-        QUrl &u = url;
-        QUrlQuery url;
-#endif
-        url.addQueryItem("id", channelId);
-        url.addQueryItem("part", "snippet");
-#if QT_VERSION >= 0x050000
-        u.setQuery(url);
-    }
-#endif
-
-#else
-
-    QUrl url("http://gdata.youtube.com/feeds/api/users/" + channelId);
-#if QT_VERSION >= 0x050000
-    {
-        QUrl &u = url;
-        QUrlQuery url;
-#endif
-        url.addQueryItem("v", "2");
-#if QT_VERSION >= 0x050000
-        u.setQuery(url);
-    }
-#endif
-
-#endif
+    QUrlQuery q(url);
+    q.addQueryItem("id", channelId);
+    q.addQueryItem("part", "snippet");
+    url.setQuery(q);
 
-    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("default").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();
@@ -183,31 +110,20 @@ void YTChannel::parseResponse(const QByteArray &bytes) {
     loading = false;
 }
 
-#endif
-
 void YTChannel::loadThumbnail() {
     if (loadingThumbnail) return;
     if (thumbnailUrl.isEmpty()) return;
     loadingThumbnail = true;
 
-#ifdef Q_OS_WIN
-    thumbnailUrl.replace(QLatin1String("https://"), QLatin1String("http://"));
-#endif
-
     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() {
+const QString &YTChannel::getThumbnailDir() {
     static const QString thumbDir =
-        #if QT_VERSION >= 0x050000
-            QStandardPaths::writableLocation(QStandardPaths::DataLocation)
-        #else
-            QDesktopServices::storageLocation(QDesktopServices::DataLocation)
-        #endif
-            + "/channels/";
+            QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + "/channels/";
     return thumbDir;
 }
 
@@ -215,19 +131,34 @@ QString YTChannel::getThumbnailLocation() {
     return getThumbnailDir() + channelId;
 }
 
+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.bindValue(0, channelId);
+    bool success = query.exec();
+    if (!success) qWarning() << query.lastQuery() << query.lastError().text();
+    if (!query.next()) return QString();
+    return query.value(0).toString();
+}
+
 void YTChannel::unsubscribe() {
     YTChannel::unsubscribe(channelId);
 }
 
 void YTChannel::storeThumbnail(const QByteArray &bytes) {
     thumbnail.loadFromData(bytes);
-    static const int maxWidth = 88;
+    qreal maxRatio = 2.0;
+    thumbnail.setDevicePixelRatio(maxRatio);
+    const int maxWidth = 88 * maxRatio;
 
     QDir dir;
     dir.mkpath(getThumbnailDir());
 
     if (thumbnail.width() > maxWidth) {
         thumbnail = thumbnail.scaledToWidth(maxWidth, Qt::SmoothTransformation);
+        thumbnail.setDevicePixelRatio(maxRatio);
         thumbnail.save(getThumbnailLocation(), "JPG");
     } else {
         QFile file(getThumbnailLocation());
@@ -241,9 +172,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;
 }
@@ -316,8 +247,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;
 }
 
@@ -346,7 +276,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();
@@ -354,8 +285,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();