From: Flavio Date: Fri, 5 Aug 2011 18:54:21 +0000 (+0200) Subject: Extract videoId utility method X-Git-Tag: 1.5~3 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=2a006c4ece54ad70485e6b14435b4649a95037ba;p=minitube Extract videoId utility method --- diff --git a/src/youtubesearch.cpp b/src/youtubesearch.cpp index a57b7c2..edb0dd1 100644 --- a/src/youtubesearch.cpp +++ b/src/youtubesearch.cpp @@ -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 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(); +} diff --git a/src/youtubesearch.h b/src/youtubesearch.h index dc2739d..4a094e0 100644 --- a/src/youtubesearch.h +++ b/src/youtubesearch.h @@ -13,6 +13,7 @@ public: void search(SearchParams *searchParams, int max, int skip); void abort(); QList getResults(); + static QString videoIdFromUrl(QString url); signals: void gotVideo(Video*);