~ubuntu-branches/ubuntu/saucy/minitube/saucy

« back to all changes in this revision

Viewing changes to src/youtubesearch.cpp

  • Committer: Package Import Robot
  • Author(s): Jakob Haufe
  • Date: 2013-05-23 13:54:01 UTC
  • mfrom: (1.1.15 sid)
  • Revision ID: package-import@ubuntu.com-20130523135401-wsbh1xtf71nkfvkt
Tags: 2.0-1
* New upstream version
* Switch from hardening-wrapper to buildflags.mk
* Refresh patches:
  - Drop gcc-4.7.patch, fixed upstream
  - Rebuild assure-quit-keybinding
  - Rebuild disable-update-check
* Update Standards-Version to 3.9.4
  - Add Vcs-* control fields
  - No further changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "youtubesearch.h"
2
 
#include "youtubestreamreader.h"
3
 
#include "constants.h"
4
 
#include "networkaccess.h"
5
 
 
6
 
namespace The {
7
 
    NetworkAccess* http();
8
 
}
9
 
 
10
 
YouTubeSearch::YouTubeSearch() : QObject() {}
11
 
 
12
 
void YouTubeSearch::search(SearchParams *searchParams, int max, int skip) {
13
 
    this->abortFlag = false;
14
 
 
15
 
    QUrl url("http://gdata.youtube.com/feeds/api/videos/");
16
 
    url.addQueryItem("v", "2");
17
 
 
18
 
    url.addQueryItem("max-results", QString::number(max));
19
 
    url.addQueryItem("start-index", QString::number(skip));
20
 
 
21
 
    if (!searchParams->keywords().isEmpty()) {
22
 
        if (searchParams->keywords().startsWith("http://") ||
23
 
                searchParams->keywords().startsWith("https://")) {
24
 
            url.addQueryItem("q", YouTubeSearch::videoIdFromUrl(searchParams->keywords()));
25
 
        } else url.addQueryItem("q", searchParams->keywords());
26
 
    }
27
 
 
28
 
    if (!searchParams->author().isEmpty())
29
 
        url.addQueryItem("author", searchParams->author());
30
 
 
31
 
    switch (searchParams->sortBy()) {
32
 
    case SearchParams::SortByNewest:
33
 
        url.addQueryItem("orderby", "published");
34
 
        break;
35
 
    case SearchParams::SortByViewCount:
36
 
        url.addQueryItem("orderby", "viewCount");
37
 
        break;
38
 
    case SearchParams::SortByRating:
39
 
        url.addQueryItem("orderby", "rating");
40
 
        break;
41
 
    }
42
 
 
43
 
    switch (searchParams->duration()) {
44
 
    case SearchParams::DurationShort:
45
 
        url.addQueryItem("duration", "short");
46
 
        break;
47
 
    case SearchParams::DurationMedium:
48
 
        url.addQueryItem("duration", "medium");
49
 
        break;
50
 
    case SearchParams::DurationLong:
51
 
        url.addQueryItem("duration", "long");
52
 
        break;
53
 
    }
54
 
 
55
 
    switch (searchParams->time()) {
56
 
    case SearchParams::TimeToday:
57
 
        url.addQueryItem("time", "today");
58
 
        break;
59
 
    case SearchParams::TimeWeek:
60
 
        url.addQueryItem("time", "this_week");
61
 
        break;
62
 
    case SearchParams::TimeMonth:
63
 
        url.addQueryItem("time", "this_month");
64
 
        break;
65
 
    }
66
 
 
67
 
    switch (searchParams->quality()) {
68
 
    case SearchParams::QualityHD:
69
 
        url.addQueryItem("hd", "true");
70
 
        break;
71
 
    }
72
 
 
73
 
    QObject *reply = The::http()->get(url);
74
 
    connect(reply, SIGNAL(data(QByteArray)), SLOT(parseResults(QByteArray)));
75
 
    connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(error(QNetworkReply*)));
76
 
 
77
 
}
78
 
 
79
 
void YouTubeSearch::error(QNetworkReply *reply) {
80
 
    emit error(reply->errorString());
81
 
}
82
 
 
83
 
void YouTubeSearch::parseResults(QByteArray data) {
84
 
 
85
 
    YouTubeStreamReader reader;
86
 
    if (!reader.read(data)) {
87
 
        qDebug() << "Error parsing XML";
88
 
    }
89
 
    videos = reader.getVideos();
90
 
    suggestions = reader.getSuggestions();
91
 
 
92
 
    foreach (Video *video, videos) {
93
 
        // send it to the model
94
 
        emit gotVideo(video);
95
 
    }
96
 
 
97
 
    foreach (Video *video, videos) {
98
 
        // preload the thumb
99
 
        if (abortFlag) return;
100
 
        video->preloadThumbnail();
101
 
    }
102
 
 
103
 
    emit finished(videos.size());
104
 
}
105
 
 
106
 
QList<Video*> YouTubeSearch::getResults() {
107
 
    return videos;
108
 
}
109
 
 
110
 
const QStringList & YouTubeSearch::getSuggestions() const {
111
 
    return suggestions;
112
 
}
113
 
 
114
 
void YouTubeSearch::abort() {
115
 
    this->abortFlag = true;
116
 
}
117
 
 
118
 
QString YouTubeSearch::videoIdFromUrl(QString url) {
119
 
    QRegExp re = QRegExp("^.*\\?v=([^&]+).*$");
120
 
    if (re.exactMatch(url)) {
121
 
        QString videoId = re.cap(1);
122
 
        videoId.remove('-');
123
 
        return videoId;
124
 
    }
125
 
    return QString();
126
 
}