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"
23 #include "httputils.h"
24 #include "datautils.h"
26 #include "ytregions.h"
28 YTCategories::YTCategories(QObject *parent) : QObject(parent) { }
30 void YTCategories::loadCategories(QString language) {
31 if (language.isEmpty()) {
32 language = QLocale::system().uiLanguages().at(0);
33 int index = language.indexOf('-');
34 if (index > 0) language = language.mid(0, index);
36 lastLanguage = language;
38 QUrl url = YT3::instance().method("videoCategories");
41 q.addQueryItem("part", "snippet");
42 q.addQueryItem("hl", language);
44 QString regionCode = YTRegions::currentRegionId();
45 if (regionCode.isEmpty()) regionCode = "us";
46 q.addQueryItem("regionCode", regionCode);
49 QObject *reply = HttpUtils::yt().get(url);
50 connect(reply, SIGNAL(data(QByteArray)), SLOT(parseCategories(QByteArray)));
51 connect(reply, SIGNAL(error(QString)), SLOT(requestError(QString)));
54 void YTCategories::parseCategories(const QByteArray &bytes) {
55 QJsonDocument doc = QJsonDocument::fromJson(bytes);
56 QJsonObject obj = doc.object();
57 const QJsonArray items = obj["items"].toArray();
59 QVector<YTCategory> categories;
60 categories.reserve(items.size());
61 for (const QJsonValue &v : items) {
62 QJsonObject item = v.toObject();
63 QJsonObject snippet = item["snippet"].toObject();
64 bool isAssignable = snippet["assignable"].toBool();
65 if (!isAssignable) continue;
68 category.term = item["id"].toString();
69 category.label = snippet["title"].toString();
70 // if (category.label.startsWith(QLatin1String("News"))) continue;
71 categories << category;
74 emit categoriesLoaded(categories);
77 void YTCategories::requestError(const QString &message) {
78 if (lastLanguage != "en") loadCategories("en");
79 else emit error(message);