-
Notifications
You must be signed in to change notification settings - Fork 12
/
YouTubeSearch.cpp
115 lines (96 loc) · 3.7 KB
/
YouTubeSearch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <QtDebug>
#include "YouTubeSearch.h"
YouTubeSearch::YouTubeSearch(QObject *parent)
: QObject(parent)
{
cache.setMaximumCacheSize(CACHE_SIZE);
cache.setCacheDirectory(QDesktopServices::storageLocation(QDesktopServices::CacheLocation) + "/omnimedia");
manager.setCache(&cache);
m_reply = 0;
m_context = 0;
}
void YouTubeSearch::setContext(QDeclarativeContext *context)
{
m_context = context;
}
void YouTubeSearch::search(const QString& query)
{
xml.clear();
qDeleteAll(m_objects);
m_objects.clear();
if (query.isEmpty()) {
m_context->setContextProperty("youtubeModel", QVariant::fromValue(m_objects));
return;
}
if (m_reply) {
m_reply->abort();
m_reply->deleteLater();
}
QString url = QString("%1q=%2")
.arg(YOUTUBE_API)
.arg(query);
QNetworkRequest request(url);
request.setAttribute(QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache);
m_reply = manager.get(request);
connect(m_reply, SIGNAL(readyRead()), SLOT(readyRead()));
connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(error(QNetworkReply::NetworkError)));
connect(m_reply, SIGNAL(finished()), SLOT(finished()));
}
void YouTubeSearch::readyRead()
{
int statusCode = m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
if (statusCode >= 200 && statusCode < 300) {
QString currentTag;
QString linkString;
QString descriptionString;
QString titleString;
xml.addData(m_reply->readAll());
while (!xml.atEnd()) {
xml.readNext();
if (xml.isStartElement()) {
if (xml.name() == QLatin1String("item"))
linkString = xml.attributes().value("link").toString();
currentTag = xml.qualifiedName().toString();
} else if (xml.isEndElement()) {
if (xml.qualifiedName() == QLatin1String("media:title")) {
QUrl url(linkString);
QString videoId = url.queryItemValue("v");
QString videoImage = QString(YTIMG)
.arg(qrand() % 3 + 1)
.arg(videoId);
Media *media = new Media(m_context);
media->setTitle(titleString);
media->setDescription(descriptionString);
media->setId(videoId);
media->setImage(QUrl(videoImage));
media->setUrl(url);
m_objects << media;
m_context->setContextProperty("youtubeModel", QVariant::fromValue(m_objects));
currentTag.clear();
titleString.clear();
linkString.clear();
descriptionString.clear();
}
} else if (xml.isCharacters() && !xml.isWhitespace()) {
if (currentTag == QLatin1String("media:title")) {
titleString += xml.text().toString();
} else if (currentTag == QLatin1String("media:description")) {
descriptionString += xml.text().toString();
} else if (currentTag == QLatin1String("link")) {
linkString += xml.text().toString();
}
}
}
if (xml.error() && xml.error() != QXmlStreamReader::PrematureEndOfDocumentError) {
qWarning() << xml.lineNumber() << xml.errorString();
}
}
}
void YouTubeSearch::error(QNetworkReply::NetworkError error)
{
}
void YouTubeSearch::finished()
{
m_context->setContextProperty("youtubeModel", QVariant::fromValue(m_objects));
xml.clear();
}