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
21 urlString = QString("http://localhost/oringo/video.xml?q=%1&max-results=%2&start-index=%3")
22 .arg(searchParams->keywords(), QString::number(max), QString::number(skip));
25 switch (searchParams->sortBy()) {
26 case SearchParams::SortByNewest:
27 urlString.append("&orderby=published");
29 case SearchParams::SortByViewCount:
30 urlString.append("&orderby=viewCount");
36 QObject *reply = The::http()->get(url);
37 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
38 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(error(QNetworkReply*)));
42 void YouTubeSearch::error(QNetworkReply *reply) {
43 emit error(reply->errorString());
46 void YouTubeSearch::parseResults(QByteArray data) {
48 YouTubeStreamReader reader;
49 if (!reader.read(data)) {
50 qDebug() << "Error parsing XML";
52 videos = reader.getVideos();
54 foreach (Video *video, videos) {
55 // send it to the model
59 foreach (Video *video, videos) {
61 if (abortFlag) return;
62 video->preloadThumbnail();
65 emit finished(videos.size());
68 QList<Video*> YouTubeSearch::getResults() {
72 void YouTubeSearch::abort() {
73 this->abortFlag = true;