]> git.sur5r.net Git - minitube/blobdiff - src/channelaggregator.cpp
Imported Upstream version 2.5.1
[minitube] / src / channelaggregator.cpp
index a7eb0553449adb7e0dcd227362c379c346f267ec..57bc5dd73761650205071cd4763ee9a4d094dcff 100644 (file)
@@ -27,12 +27,18 @@ $END_LICENSE */
 #ifdef APP_MAC
 #include "macutils.h"
 #endif
+#include "networkaccess.h"
+
+namespace The {
+    NetworkAccess* http();
+}
 
 ChannelAggregator::ChannelAggregator(QObject *parent) : QObject(parent),
     unwatchedCount(-1),
     running(false),
-    stopped(false) {
-    checkInterval = 3600;
+    stopped(false),
+    currentChannel(0) {
+    checkInterval = 1800;
 
     timer = new QTimer(this);
     timer->setInterval(60000 * 5);
@@ -88,22 +94,64 @@ void ChannelAggregator::processNextChannel() {
         running = false;
         return;
     }
-    qApp->processEvents();
     YTChannel* channel = getChannelToCheck();
     if (channel) {
-        SearchParams *params = new SearchParams();
-        params->setChannelId(channel->getChannelId());
-        params->setSortBy(SearchParams::SortByNewest);
-        params->setTransient(true);
-        params->setPublishedAfter(channel->getChecked());
-        YTSearch *videoSource = new YTSearch(params, this);
-        connect(videoSource, SIGNAL(gotVideos(QList<Video*>)), SLOT(videosLoaded(QList<Video*>)));
-        videoSource->loadVideos(50, 1);
-        channel->updateChecked();
+        checkWebPage(channel);
     } else finish();
 }
 
+void ChannelAggregator::checkWebPage(YTChannel *channel) {
+    currentChannel = channel;
+    QString url = "https://www.youtube.com/channel/" + channel->getChannelId() + "/videos";
+    QObject *reply = The::http()->get(url);
+
+    connect(reply, SIGNAL(data(QByteArray)), SLOT(parseWebPage(QByteArray)));
+    connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(errorWebPage(QNetworkReply*)));
+}
+
+void ChannelAggregator::parseWebPage(const QByteArray &bytes) {
+    bool hasNewVideos = true;
+    QRegExp re = QRegExp("[\\?&]v=([0-9A-Za-z_-]+)");
+    if (re.indexIn(bytes) != -1) {
+        QString videoId = re.cap(1);
+        QString latestVideoId = currentChannel->latestVideoId();
+        // qDebug() << "Comparing" << videoId << latestVideoId;
+        hasNewVideos = videoId != latestVideoId;
+    }
+    if (hasNewVideos) {
+        if (currentChannel) {
+            reallyProcessChannel(currentChannel);
+            currentChannel = 0;
+        }
+    } else {
+        currentChannel->updateChecked();
+        currentChannel = 0;
+        processNextChannel();
+    }
+}
+
+void ChannelAggregator::errorWebPage(QNetworkReply *reply) {
+    Q_UNUSED(reply);
+    reallyProcessChannel(currentChannel);
+    currentChannel = 0;
+}
+
+void ChannelAggregator::reallyProcessChannel(YTChannel *channel) {
+    SearchParams *params = new SearchParams();
+    params->setChannelId(channel->getChannelId());
+    params->setSortBy(SearchParams::SortByNewest);
+    params->setTransient(true);
+    params->setPublishedAfter(channel->getChecked());
+    YTSearch *videoSource = new YTSearch(params, this);
+    connect(videoSource, SIGNAL(gotVideos(QList<Video*>)), SLOT(videosLoaded(QList<Video*>)));
+    videoSource->loadVideos(50, 1);
+
+    channel->updateChecked();
+}
+
 void ChannelAggregator::finish() {
+    currentChannel = 0;
+
     /*
     foreach (YTChannel *channel, updatedChannels)
         if (channel->updateNotifyCount())
@@ -160,7 +208,7 @@ void ChannelAggregator::videosLoaded(const QList<Video*> &videos) {
         foreach (Video* video, videos) video->deleteLater();
     }
 
-    QTimer::singleShot(1000, this, SLOT(processNextChannel()));
+    QTimer::singleShot(0, this, SLOT(processNextChannel()));
 }
 
 void ChannelAggregator::updateUnwatchedCount() {