]> git.sur5r.net Git - minitube/commitdiff
Optimized aggregator
authorFlavio Tordini <flavio.tordini@gmail.com>
Sun, 9 Aug 2015 08:20:54 +0000 (10:20 +0200)
committerFlavio Tordini <flavio.tordini@gmail.com>
Sun, 9 Aug 2015 08:20:54 +0000 (10:20 +0200)
src/channelaggregator.cpp
src/channelaggregator.h
src/ytchannel.cpp
src/ytchannel.h

index b660c380ee9848cadeeaf7aca916fe2b7198f852..97b369d40b0207c35fd56e333cb95f8141888644 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,54 @@ 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);
+        checkWebPage(channel);
         channel->updateChecked();
     } 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) reallyProcessChannel(currentChannel);
+    else processNextChannel();
+}
+
+void ChannelAggregator::errorWebPage(QNetworkReply *reply) {
+    Q_UNUSED(reply);
+    reallyProcessChannel(currentChannel);
+}
+
+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);
+}
+
 void ChannelAggregator::finish() {
+    currentChannel = 0;
+
     /*
     foreach (YTChannel *channel, updatedChannels)
         if (channel->updateNotifyCount())
index 288ce82d70955883cbc9e1d263bf1b6e223e312f..bb92227bda47d7b0fb5c797f23b54ddecbf11b83 100644 (file)
@@ -22,6 +22,7 @@ $END_LICENSE */
 #define CHANNELAGGREGATOR_H
 
 #include <QtCore>
+#include <QtNetwork>
 
 class YTChannel;
 class Video;
@@ -50,6 +51,10 @@ signals:
 private slots:
     void videosLoaded(const QList<Video*> &videos);
     void processNextChannel();
+    void checkWebPage(YTChannel *channel);
+    void parseWebPage(const QByteArray &bytes);
+    void errorWebPage(QNetworkReply *reply);
+    void reallyProcessChannel(YTChannel *channel);
 
 private:
     ChannelAggregator(QObject *parent = 0);
@@ -66,6 +71,8 @@ private:
 
     QTimer *timer;
     bool stopped;
+
+    YTChannel *currentChannel;
 };
 
 #endif // CHANNELAGGREGATOR_H
index c2b33aae4ad38fbfea32210e4f25a00622a316f8..bcebcfed0ed4e1ada2677d4ba1491e593a5cbd81 100644 (file)
@@ -199,6 +199,17 @@ 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);
 }
index 25a9fcaa9877a3b0f92adac30a2c2be61704befe..fea95aa992c20e702672f3ac9244ae74edf87a02 100644 (file)
@@ -62,6 +62,8 @@ public:
     QString getThumbnailLocation();
     const QPixmap & getThumbnail() { return thumbnail; }
 
+    QString latestVideoId();
+
     static QList<YTChannel*> getCachedChannels() { return cache.values(); }
 
 public slots: