]> git.sur5r.net Git - minitube/commitdiff
Extract videoId utility method
authorFlavio <flavio@odisseo.local>
Fri, 5 Aug 2011 18:54:21 +0000 (20:54 +0200)
committerFlavio <flavio@odisseo.local>
Fri, 5 Aug 2011 18:54:21 +0000 (20:54 +0200)
src/youtubesearch.cpp
src/youtubesearch.h

index a57b7c21585f1ccef7c67e5a5d8ee04c183b7f30..edb0dd1f70575177116da27d0ff20e1321704d5b 100644 (file)
@@ -15,10 +15,14 @@ void YouTubeSearch::search(SearchParams *searchParams, int max, int skip) {
     QUrl url("http://gdata.youtube.com/feeds/api/videos");
     url.addQueryItem("max-results", QString::number(max));
     url.addQueryItem("start-index", QString::number(skip));
-    if (!searchParams->keywords().isEmpty())
-    url.addQueryItem("q", searchParams->keywords());
+    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());
+        url.addQueryItem("author", searchParams->author());
 
     switch (searchParams->sortBy()) {
     case SearchParams::SortByNewest:
@@ -68,3 +72,12 @@ QList<Video*> YouTubeSearch::getResults() {
 void YouTubeSearch::abort() {
     this->abortFlag = true;
 }
+
+QString YouTubeSearch::videoIdFromUrl(QString url) {
+    QRegExp re = QRegExp("^.*\\?v=([^&]+).*$");
+    if (re.exactMatch(url)) {
+        QString videoId = re.cap(1);
+        return videoId;
+    }
+    return QString();
+}
index dc2739dfa5dcdd3b92e5c75d7740f2b27335a2fc..4a094e0f3d49e7a031882c40d4bdde0f79f6ede2 100644 (file)
@@ -13,6 +13,7 @@ public:
     void search(SearchParams *searchParams, int max, int skip);
     void abort();
     QList<Video*> getResults();
+    static QString videoIdFromUrl(QString url);
 
 signals:
     void gotVideo(Video*);