]> git.sur5r.net Git - minitube/blob - src/ytcategories.cpp
Merge tag 'upstream/2.5.1'
[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 "networkaccess.h"
23 #ifdef APP_YT3
24 #include "datautils.h"
25 #include "yt3.h"
26 #include "ytregions.h"
27 #include <QtScript>
28 #endif
29 #include "compatibility/qurlqueryhelper.h"
30
31 namespace The {
32 NetworkAccess* http();
33 }
34
35 YTCategories::YTCategories(QObject *parent) : QObject(parent) { }
36
37 void YTCategories::loadCategories(QString language) {
38     if (language.isEmpty())
39         language = QLocale::system().uiLanguages().first();
40     lastLanguage = language;
41
42 #ifdef APP_YT3
43     QUrl url = YT3::instance().method("videoCategories");
44     {
45         QUrlQueryHelper urlHelper(url);
46         urlHelper.addQueryItem("part", "snippet");
47         urlHelper.addQueryItem("hl", language);
48
49         QString regionCode = YTRegions::currentRegionId();
50         if (regionCode.isEmpty()) regionCode = "us";
51         urlHelper.addQueryItem("regionCode", regionCode);
52     }
53 #else
54     QString url = "http://gdata.youtube.com/schemas/2007/categories.cat?hl=" + language;
55 #endif
56
57
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*)));
61 }
62
63 #ifdef APP_YT3
64
65 void YTCategories::parseCategories(QByteArray bytes) {
66     QList<YTCategory> categories;
67
68     QScriptEngine engine;
69     QScriptValue json = engine.evaluate("(" + QString::fromUtf8(bytes) + ")");
70
71     QScriptValue items = json.property("items");
72
73     if (items.isArray()) {
74         QScriptValueIterator it(items);
75         while (it.hasNext()) {
76             it.next();
77             QScriptValue item = it.value();
78             // For some reason the array has an additional element containing its size.
79             if (!item.isObject()) continue;
80
81             QScriptValue snippet = item.property("snippet");
82
83             bool isAssignable = snippet.property("assignable").toBool();
84             if (!isAssignable) continue;
85
86             YTCategory category;
87             category.term = item.property("id").toString();
88             category.label = snippet.property("title").toString();
89             categories << category;
90         }
91     }
92
93     emit categoriesLoaded(categories);
94 }
95
96 #else
97
98 void YTCategories::parseCategories(QByteArray bytes) {
99     QList<YTCategory> categories;
100
101     QXmlStreamReader xml(bytes);
102     while (!xml.atEnd()) {
103         xml.readNext();
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")) {
109                     YTCategory category;
110                     category.term = term;
111                     category.label = label;
112                     categories << category;
113                 } else xml.skipCurrentElement();
114         }
115     }
116
117     if (xml.hasError()) {
118         emit error(xml.errorString());
119         return;
120     }
121
122     emit categoriesLoaded(categories);
123 }
124
125 #endif
126
127 void YTCategories::requestError(QNetworkReply *reply) {
128     if (lastLanguage != "en") loadCategories("en");
129     else emit error(reply->errorString());
130 }