]> git.sur5r.net Git - minitube/blob - src/ytcategories.cpp
Upload 3.9.3-2 to unstable
[minitube] / src / ytcategories.cpp
1 /* $BEGIN_LICENSE
2
3 This file is part of Minitube.
4 Copyright 2009, Flavio Tordini <flavio.tordini@gmail.com>
5
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.
10
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.
15
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/>.
18
19 $END_LICENSE */
20
21 #include "ytcategories.h"
22 #include "http.h"
23 #include "httputils.h"
24 #include "datautils.h"
25 #include "yt3.h"
26 #include "ytregions.h"
27
28 YTCategories::YTCategories(QObject *parent) : QObject(parent) { }
29
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);
35     }
36     lastLanguage = language;
37
38     QUrl url = YT3::instance().method("videoCategories");
39
40     QUrlQuery q(url);
41     q.addQueryItem("part", "snippet");
42     q.addQueryItem("hl", language);
43
44     QString regionCode = YTRegions::currentRegionId();
45     if (regionCode.isEmpty()) regionCode = "us";
46     q.addQueryItem("regionCode", regionCode);
47     url.setQuery(q);
48
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)));
52 }
53
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();
58
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;
66
67         YTCategory category;
68         category.term = item["id"].toString();
69         category.label = snippet["title"].toString();
70         // if (category.label.startsWith(QLatin1String("News"))) continue;
71         categories << category;
72     }
73
74     emit categoriesLoaded(categories);
75 }
76
77 void YTCategories::requestError(const QString &message) {
78     if (lastLanguage != "en") loadCategories("en");
79     else emit error(message);
80 }