1 #include "youtubesearch.h"
2 #include "youtubestreamreader.h"
4 #include "networkaccess.h"
10 YouTubeSearch::YouTubeSearch() : QObject() {}
12 void YouTubeSearch::search(SearchParams *searchParams, int max, int skip) {
13 this->abortFlag = false;
15 QString urlString = QString(
16 "http://gdata.youtube.com/feeds/api/videos?q=%1&max-results=%2&start-index=%3")
17 .arg(searchParams->keywords(), QString::number(max), QString::number(skip));
19 // Useful to test with a local webserver
20 urlString = QString("http://localhost/oringo/video.xml?q=%1&max-results=%2&start-index=%3")
21 .arg(searchParams->keywords(), QString::number(max), QString::number(skip));
23 switch (searchParams->sortBy()) {
24 case SearchParams::SortByNewest:
25 urlString.append("&orderby=published");
27 case SearchParams::SortByViewCount:
28 urlString.append("&orderby=viewCount");
34 QObject *reply = The::http()->get(url);
35 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
36 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(error(QNetworkReply*)));
40 void YouTubeSearch::error(QNetworkReply *reply) {
41 emit error(reply->errorString());
44 void YouTubeSearch::parseResults(QByteArray data) {
46 YouTubeStreamReader reader;
47 if (!reader.read(data)) {
48 qDebug() << "Error parsing XML";
50 videos = reader.getVideos();
52 foreach (Video *video, videos) {
53 // send it to the model
57 foreach (Video *video, videos) {
59 if (abortFlag) return;
60 video->preloadThumbnail();
63 emit finished(videos.size());
66 QList<Video*> YouTubeSearch::getResults() {
70 void YouTubeSearch::abort() {
71 this->abortFlag = true;