3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
6 Minitube is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Minitube is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Minitube. If not, see <http://www.gnu.org/licenses/>.
21 #include "ytcategories.h"
22 #include "networkaccess.h"
24 #include "datautils.h"
26 #include "ytregions.h"
31 NetworkAccess* http();
34 YTCategories::YTCategories(QObject *parent) : QObject(parent) { }
36 void YTCategories::loadCategories(QString language) {
37 if (language.isEmpty())
38 language = QLocale::system().uiLanguages().first();
39 lastLanguage = language;
42 QUrl url = YT3::instance().method("videoCategories");
44 #if QT_VERSION >= 0x050000
50 url.addQueryItem("part", "snippet");
51 url.addQueryItem("hl", language);
53 QString regionCode = YTRegions::currentRegionId();
54 if (regionCode.isEmpty()) regionCode = "us";
55 url.addQueryItem("regionCode", regionCode);
57 #if QT_VERSION >= 0x050000
63 QString url = "http://gdata.youtube.com/schemas/2007/categories.cat?hl=" + language;
67 QObject *reply = The::http()->get(url);
68 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseCategories(QByteArray)));
69 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
74 void YTCategories::parseCategories(QByteArray bytes) {
75 QList<YTCategory> categories;
78 QScriptValue json = engine.evaluate("(" + QString::fromUtf8(bytes) + ")");
80 QScriptValue items = json.property("items");
82 if (items.isArray()) {
83 QScriptValueIterator it(items);
84 while (it.hasNext()) {
86 QScriptValue item = it.value();
87 // For some reason the array has an additional element containing its size.
88 if (!item.isObject()) continue;
90 QScriptValue snippet = item.property("snippet");
92 bool isAssignable = snippet.property("assignable").toBool();
93 if (!isAssignable) continue;
96 category.term = item.property("id").toString();
97 category.label = snippet.property("title").toString();
98 categories << category;
102 emit categoriesLoaded(categories);
107 void YTCategories::parseCategories(QByteArray bytes) {
108 QList<YTCategory> categories;
110 QXmlStreamReader xml(bytes);
111 while (!xml.atEnd()) {
113 if (xml.isStartElement() && xml.name() == QLatin1String("category")) {
114 QString term = xml.attributes().value("term").toString();
115 QString label = xml.attributes().value("label").toString();
116 while(xml.readNextStartElement())
117 if (xml.name() == QLatin1String("assignable")) {
119 category.term = term;
120 category.label = label;
121 categories << category;
122 } else xml.skipCurrentElement();
126 if (xml.hasError()) {
127 emit error(xml.errorString());
131 emit categoriesLoaded(categories);
136 void YTCategories::requestError(QNetworkReply *reply) {
137 if (lastLanguage != "en") loadCategories("en");
138 else emit error(reply->errorString());