X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fyoutubesearch.cpp;h=cd6cff1df49e0268ce9fd6eb01c323171f294adc;hb=57c25e20a8f10a98a18dbc2b64ce55cf87d4fd7c;hp=664240b4fc62e11b705f111c303b467e3ffbe695;hpb=efec77e73c8eafb7ba1f0b7817567272f02262e5;p=minitube diff --git a/src/youtubesearch.cpp b/src/youtubesearch.cpp index 664240b..cd6cff1 100644 --- a/src/youtubesearch.cpp +++ b/src/youtubesearch.cpp @@ -1,6 +1,6 @@ #include "youtubesearch.h" #include "youtubestreamreader.h" -#include "Constants.h" +#include "constants.h" #include "networkaccess.h" namespace The { @@ -12,27 +12,27 @@ 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)); - - // 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)); - */ + 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()) { + 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; } - QUrl url(urlString); - QObject *reply = The::http()->get(url); connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray))); connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(error(QNetworkReply*))); @@ -72,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(); +}