]> git.sur5r.net Git - minitube/blob - src/ytcategories.cpp
db5a244cddd770c1a2b4605075af0dab18de357f
[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
30 namespace The {
31 NetworkAccess* http();
32 }
33
34 YTCategories::YTCategories(QObject *parent) : QObject(parent) { }
35
36 void YTCategories::loadCategories(QString language) {
37     if (language.isEmpty())
38         language = QLocale::system().uiLanguages().first();
39     lastLanguage = language;
40
41 #ifdef APP_YT3
42     QUrl url = YT3::instance().method("videoCategories");
43
44 #if QT_VERSION >= 0x050000
45     {
46         QUrl &u = url;
47         QUrlQuery url(u);
48 #endif
49
50         url.addQueryItem("part", "snippet");
51         url.addQueryItem("hl", language);
52
53         QString regionCode = YTRegions::currentRegionId();
54         if (regionCode.isEmpty()) regionCode = "us";
55         url.addQueryItem("regionCode", regionCode);
56
57 #if QT_VERSION >= 0x050000
58         u.setQuery(url);
59     }
60 #endif
61
62 #else
63     QString url = "http://gdata.youtube.com/schemas/2007/categories.cat?hl=" + language;
64 #endif
65
66
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*)));
70 }
71
72 #ifdef APP_YT3
73
74 void YTCategories::parseCategories(QByteArray bytes) {
75     QList<YTCategory> categories;
76
77     QScriptEngine engine;
78     QScriptValue json = engine.evaluate("(" + QString::fromUtf8(bytes) + ")");
79
80     QScriptValue items = json.property("items");
81
82     if (items.isArray()) {
83         QScriptValueIterator it(items);
84         while (it.hasNext()) {
85             it.next();
86             QScriptValue item = it.value();
87             // For some reason the array has an additional element containing its size.
88             if (!item.isObject()) continue;
89
90             QScriptValue snippet = item.property("snippet");
91
92             bool isAssignable = snippet.property("assignable").toBool();
93             if (!isAssignable) continue;
94
95             YTCategory category;
96             category.term = item.property("id").toString();
97             category.label = snippet.property("title").toString();
98             categories << category;
99         }
100     }
101
102     emit categoriesLoaded(categories);
103 }
104
105 #else
106
107 void YTCategories::parseCategories(QByteArray bytes) {
108     QList<YTCategory> categories;
109
110     QXmlStreamReader xml(bytes);
111     while (!xml.atEnd()) {
112         xml.readNext();
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")) {
118                     YTCategory category;
119                     category.term = term;
120                     category.label = label;
121                     categories << category;
122                 } else xml.skipCurrentElement();
123         }
124     }
125
126     if (xml.hasError()) {
127         emit error(xml.errorString());
128         return;
129     }
130
131     emit categoriesLoaded(categories);
132 }
133
134 #endif
135
136 void YTCategories::requestError(QNetworkReply *reply) {
137     if (lastLanguage != "en") loadCategories("en");
138     else emit error(reply->errorString());
139 }