]> git.sur5r.net Git - minitube/blobdiff - src/channelsuggest.cpp
Fix updating QUrl query parameters with Qt5.
[minitube] / src / channelsuggest.cpp
index 00f6e901a3672043e00984b1f4d08192179275cc..20b564ac54249f5d4d00cb579bcde85a3e8059d0 100644 (file)
@@ -20,6 +20,7 @@ $END_LICENSE */
 
 #include "channelsuggest.h"
 #include "networkaccess.h"
+#include "compatibility/qurlqueryhelper.h"
 
 namespace The {
     NetworkAccess* http();
@@ -29,38 +30,37 @@ ChannelSuggest::ChannelSuggest(QObject *parent) : Suggester(parent) {
 
 }
 
-void ChannelSuggest::suggest(QString query) {
-    QUrl url("http://www.youtube.com/results");
-#if QT_VERSION >= 0x050000
-        {
-            QUrl &u = url;
-            QUrlQuery url;
-#endif
-    url.addQueryItem("search_type", "search_users");
-    url.addQueryItem("search_query", query);
-#if QT_VERSION >= 0x050000
-            u.setQuery(url);
-        }
-#endif
+void ChannelSuggest::suggest(const QString &query) {
+    QUrl url("https://www.youtube.com/results");
+    {
+        QUrlQueryHelper urlHelper(url);
+        urlHelper.addQueryItem("search_type", "search_users");
+        urlHelper.addQueryItem("search_query", query);
+    }
+
     QObject *reply = The::http()->get(url);
     connect(reply, SIGNAL(data(QByteArray)), SLOT(handleNetworkData(QByteArray)));
 }
 
 void ChannelSuggest::handleNetworkData(QByteArray data) {
     QStringList choices;
+    QList<Suggestion*> suggestions;
+
     QString html = QString::fromUtf8(data);
-    QRegExp re("/user/([a-zA-Z0-9]+)");
+    QRegExp re("/(?:user|channel)/[a-zA-Z0-9]+[^>]+data-ytid=[\"']([^\"']+)[\"'][^>]+>([a-zA-Z0-9 ]+)</a>");
 
     int pos = 0;
     while ((pos = re.indexIn(html, pos)) != -1) {
-        // qDebug() << re.cap(0) << re.cap(1);
-        QString choice = re.cap(1);
+        QString choice = re.cap(2);
         if (!choices.contains(choice, Qt::CaseInsensitive)) {
+            qDebug() << re.capturedTexts();
+            QString channelId = re.cap(1);
+            suggestions << new Suggestion(choice, "channel", channelId);
             choices << choice;
             if (choices.size() == 10) break;
         }
         pos += re.matchedLength();
     }
 
-    emit ready(choices);
+    emit ready(suggestions);
 }