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"
29 #include "compatibility/qurlqueryhelper.h"
32 NetworkAccess* http();
35 YTCategories::YTCategories(QObject *parent) : QObject(parent) { }
37 void YTCategories::loadCategories(QString language) {
38 if (language.isEmpty())
39 language = QLocale::system().uiLanguages().first();
40 lastLanguage = language;
43 QUrl url = YT3::instance().method("videoCategories");
45 QUrlQueryHelper urlHelper(url);
46 urlHelper.addQueryItem("part", "snippet");
47 urlHelper.addQueryItem("hl", language);
49 QString regionCode = YTRegions::currentRegionId();
50 if (regionCode.isEmpty()) regionCode = "us";
51 urlHelper.addQueryItem("regionCode", regionCode);
54 QString url = "http://gdata.youtube.com/schemas/2007/categories.cat?hl=" + language;
58 QObject *reply = The::http()->get(url);
59 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseCategories(QByteArray)));
60 connect(reply, SIGNAL(error(QNetworkReply*)), SLOT(requestError(QNetworkReply*)));
65 void YTCategories::parseCategories(QByteArray bytes) {
66 QList<YTCategory> categories;
69 QScriptValue json = engine.evaluate("(" + QString::fromUtf8(bytes) + ")");
71 QScriptValue items = json.property("items");
73 if (items.isArray()) {
74 QScriptValueIterator it(items);
75 while (it.hasNext()) {
77 QScriptValue item = it.value();
78 // For some reason the array has an additional element containing its size.
79 if (!item.isObject()) continue;
81 QScriptValue snippet = item.property("snippet");
83 bool isAssignable = snippet.property("assignable").toBool();
84 if (!isAssignable) continue;
87 category.term = item.property("id").toString();
88 category.label = snippet.property("title").toString();
89 categories << category;
93 emit categoriesLoaded(categories);
98 void YTCategories::parseCategories(QByteArray bytes) {
99 QList<YTCategory> categories;
101 QXmlStreamReader xml(bytes);
102 while (!xml.atEnd()) {
104 if (xml.isStartElement() && xml.name() == QLatin1String("category")) {
105 QString term = xml.attributes().value("term").toString();
106 QString label = xml.attributes().value("label").toString();
107 while(xml.readNextStartElement())
108 if (xml.name() == QLatin1String("assignable")) {
110 category.term = term;
111 category.label = label;
112 categories << category;
113 } else xml.skipCurrentElement();
117 if (xml.hasError()) {
118 emit error(xml.errorString());
122 emit categoriesLoaded(categories);
127 void YTCategories::requestError(QNetworkReply *reply) {
128 if (lastLanguage != "en") loadCategories("en");
129 else emit error(reply->errorString());