]> git.sur5r.net Git - minitube/blobdiff - src/channelaggregator.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / channelaggregator.cpp
index 5848fa9e935d47bf908db7e039871fa7c1d452fc..8097af180e51fe03d44ed32383b50b61cc8e42f9 100644 (file)
@@ -30,8 +30,9 @@ $END_LICENSE */
 #include "http.h"
 #include "httputils.h"
 
-#include "videoapi.h"
 #include "ivchannelsource.h"
+#include "videoapi.h"
+#include "ytjschannelsource.h"
 
 ChannelAggregator::ChannelAggregator(QObject *parent)
     : QObject(parent), unwatchedCount(-1), running(false), stopped(false), currentChannel(0) {
@@ -80,9 +81,6 @@ void ChannelAggregator::run() {
     updatedChannels.clear();
     updatedChannels.squeeze();
 
-    if (!Database::instance().getConnection().transaction())
-        qWarning() << "Transaction failed" << __PRETTY_FUNCTION__;
-
     processNextChannel();
 }
 
@@ -100,7 +98,15 @@ void ChannelAggregator::processNextChannel() {
 
 void ChannelAggregator::checkWebPage(YTChannel *channel) {
     currentChannel = channel;
-    QString url = "https://www.youtube.com/channel/" + channel->getChannelId() + "/videos";
+
+    QString channelId = channel->getChannelId();
+    QString url;
+    if (channelId.startsWith("UC") && !channelId.contains(' ')) {
+        url = "https://www.youtube.com/channel/" + channelId + "/videos";
+    } else {
+        url = "https://www.youtube.com/user/" + channelId + "/videos";
+    }
+
     QObject *reply = HttpUtils::yt().get(url);
 
     connect(reply, SIGNAL(data(QByteArray)), SLOT(parseWebPage(QByteArray)));
@@ -126,7 +132,7 @@ void ChannelAggregator::parseWebPage(const QByteArray &bytes) {
     } else {
         currentChannel->updateChecked();
         currentChannel = 0;
-        processNextChannel();
+        QTimer::singleShot(5000, this, &ChannelAggregator::processNextChannel);
     }
 }
 
@@ -153,6 +159,11 @@ void ChannelAggregator::reallyProcessChannel(YTChannel *channel) {
         connect(videoSource, SIGNAL(gotVideos(QVector<Video *>)),
                 SLOT(videosLoaded(QVector<Video *>)));
         videoSource->loadVideos(50, 1);
+    } else if (VideoAPI::impl() == VideoAPI::JS) {
+        auto *videoSource = new YTJSChannelSource(params);
+        connect(videoSource, SIGNAL(gotVideos(QVector<Video *>)),
+                SLOT(videosLoaded(QVector<Video *>)));
+        videoSource->loadVideos(50, 1);
     }
 
     channel->updateChecked();
@@ -161,9 +172,6 @@ void ChannelAggregator::reallyProcessChannel(YTChannel *channel) {
 void ChannelAggregator::finish() {
     currentChannel = 0;
 
-    QSqlDatabase db = Database::instance().getConnection();
-    if (!db.commit()) qWarning() << "Commit failed" << __PRETTY_FUNCTION__;
-
 #ifdef Q_OS_MAC
     if (newVideoCount > 0 && unwatchedCount > 0 && mac::canNotify()) {
         QString channelNames;
@@ -193,8 +201,10 @@ void ChannelAggregator::videosLoaded(const QVector<Video *> &videos) {
 
     if (!videos.isEmpty()) {
         YTChannel *channel = YTChannel::forId(videos.at(0)->getChannelId());
-        channel->updateNotifyCount();
-        emit channelChanged(channel);
+        if (channel) {
+            channel->updateNotifyCount();
+            emit channelChanged(channel);
+        }
         updateUnwatchedCount();
         for (Video *video : videos)
             video->deleteLater();
@@ -263,7 +273,18 @@ void ChannelAggregator::addVideo(Video *video) {
     query.bindValue(7, video->getChannelId());
     query.bindValue(8, video->getDescription());
     query.bindValue(9, video->getWebpage());
-    query.bindValue(10, video->getThumbnailUrl());
+
+    QJsonDocument thumbsDoc;
+    auto thumbsArray = thumbsDoc.array();
+    for (const auto &t : video->getThumbs()) {
+        thumbsArray.append(QJsonObject{
+                {"url", t.getUrl()},
+                {"width", t.getWidth()},
+                {"height", t.getHeight()},
+        });
+    }
+    thumbsDoc.setArray(thumbsArray);
+    query.bindValue(10, thumbsDoc.toJson(QJsonDocument::Compact));
     query.bindValue(11, video->getViewCount());
     query.bindValue(12, video->getDuration());
     success = query.exec();
@@ -310,7 +331,7 @@ void ChannelAggregator::videoWatched(Video *video) {
     if (!success) qWarning() << query.lastQuery() << query.lastError().text();
     if (query.numRowsAffected() > 0) {
         YTChannel *channel = YTChannel::forId(video->getChannelId());
-        channel->updateNotifyCount();
+        if (channel) channel->updateNotifyCount();
     }
 }