]> git.sur5r.net Git - minitube/blobdiff - src/youtubesearch.cpp
Upload 1.9-1 to unstable
[minitube] / src / youtubesearch.cpp
index 664240b4fc62e11b705f111c303b467e3ffbe695..3263525c020f7dc0bd494b310d1568d1a26b6922 100644 (file)
@@ -1,6 +1,6 @@
 #include "youtubesearch.h"
 #include "youtubestreamreader.h"
-#include "Constants.h"
+#include "constants.h"
 #include "networkaccess.h"
 
 namespace The {
@@ -12,26 +12,63 @@ YouTubeSearch::YouTubeSearch() : QObject() {}
 void YouTubeSearch::search(SearchParams *searchParams, int max, int skip) {
     this->abortFlag = false;
 
-    QString urlString = QString(
-            "http://gdata.youtube.com/feeds/api/videos?q=%1&max-results=%2&start-index=%3")
-            .arg(searchParams->keywords(), QString::number(max), QString::number(skip));
+    QUrl url("http://gdata.youtube.com/feeds/api/videos/");
+    url.addQueryItem("v", "2");
 
-    // Useful to test with a local webserver
-    /*
-    urlString = QString("http://localhost/oringo/video.xml?q=%1&max-results=%2&start-index=%3")
-                .arg(searchParams->keywords(), QString::number(max), QString::number(skip));
-                */
+    url.addQueryItem("max-results", QString::number(max));
+    url.addQueryItem("start-index", QString::number(skip));
+
+    if (!searchParams->keywords().isEmpty()) {
+        if (searchParams->keywords().startsWith("http://") ||
+                searchParams->keywords().startsWith("https://")) {
+            url.addQueryItem("q", YouTubeSearch::videoIdFromUrl(searchParams->keywords()));
+        } else url.addQueryItem("q", searchParams->keywords());
+    }
+
+    if (!searchParams->author().isEmpty())
+        url.addQueryItem("author", searchParams->author());
 
     switch (searchParams->sortBy()) {
     case SearchParams::SortByNewest:
-        urlString.append("&orderby=published");
+        url.addQueryItem("orderby", "published");
         break;
     case SearchParams::SortByViewCount:
-        urlString.append("&orderby=viewCount");
+        url.addQueryItem("orderby", "viewCount");
+        break;
+    case SearchParams::SortByRating:
+        url.addQueryItem("orderby", "rating");
         break;
     }
 
-    QUrl url(urlString);
+    switch (searchParams->duration()) {
+    case SearchParams::DurationShort:
+        url.addQueryItem("duration", "short");
+        break;
+    case SearchParams::DurationMedium:
+        url.addQueryItem("duration", "medium");
+        break;
+    case SearchParams::DurationLong:
+        url.addQueryItem("duration", "long");
+        break;
+    }
+
+    switch (searchParams->time()) {
+    case SearchParams::TimeToday:
+        url.addQueryItem("time", "today");
+        break;
+    case SearchParams::TimeWeek:
+        url.addQueryItem("time", "this_week");
+        break;
+    case SearchParams::TimeMonth:
+        url.addQueryItem("time", "this_month");
+        break;
+    }
+
+    switch (searchParams->quality()) {
+    case SearchParams::QualityHD:
+        url.addQueryItem("hd", "true");
+        break;
+    }
 
     QObject *reply = The::http()->get(url);
     connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
@@ -50,6 +87,7 @@ void YouTubeSearch::parseResults(QByteArray data) {
         qDebug() << "Error parsing XML";
     }
     videos = reader.getVideos();
+    suggestions = reader.getSuggestions();
 
     foreach (Video *video, videos) {
         // send it to the model
@@ -69,6 +107,20 @@ QList<Video*> YouTubeSearch::getResults() {
     return videos;
 }
 
+const QStringList & YouTubeSearch::getSuggestions() const {
+    return suggestions;
+}
+
 void YouTubeSearch::abort() {
     this->abortFlag = true;
 }
+
+QString YouTubeSearch::videoIdFromUrl(QString url) {
+    QRegExp re = QRegExp("^.*\\?v=([^&]+).*$");
+    if (re.exactMatch(url)) {
+        QString videoId = re.cap(1);
+        videoId.remove('-');
+        return videoId;
+    }
+    return QString();
+}