History

added getAssetList() and getFBX()

Elisa Lupin-Jimenez committed 8 years ago

Commit e49181c1 parent f7be67b2 files · +98 −25Browse files

Showing the changes to interface/src/scripting/GooglePolyScriptingInterface.cpp (1 of 2 files). Show all

Unified · Split

interface/src/scripting/GooglePolyScriptingInterface.cpp+91−20
@@ -10,6 +10,7 @@ · 9 unmodified lines
1010 //
1111
1212 #include <QEventLoop>
13+#include <QGlobal.h>
1314 #include <QJsonArray>
1415 #include <QJsonDocument>
1516 #include <QJsonObject>
@@ -18,7 +19,9 @@ · 2 unmodified lines
1819 #include <QNetworkRequest>
1920 //#include <QScopedPointer>
2021 #include <QString>
22+#include <QTime>
2123 #include <QUrl>
24+#include <random>
2225
2326 #include "GooglePolyScriptingInterface.h"
2427 #include "ScriptEngineLogging.h"
@@ -26,28 +29,81 @@ · 1 unmodified line
2629 QString listPolyUrl = "https://poly.googleapis.com/v1/assets?";
2730 QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?";
2831
32+//authCode = "broke";
33+
34+QStringList validFormats = QStringList() << "BLOCKS" << "FBX" << "GLTF" << "GLTF2" << "OBJ" << "TILT" << "";
35+QStringList validCategories = QStringList() << "animals" << "architecture" << "art" << "food" <<
36+ "nature" << "objects" << "people" << "scenes" << "technology" << "transport" << "";
37+
2938 GooglePolyScriptingInterface::GooglePolyScriptingInterface() {
3039 // nothing to be implemented
3140 }
3241
33−void GooglePolyScriptingInterface::testPrint() {
34− qCDebug(scriptengine) << "Google Poly interface exists";
42+QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) {
43+ QUrl url = formatURLQuery(keyword, category, format);
44+ if (!url.isEmpty()) {
45+ QByteArray jsonString = getHTTPRequest(url);
46+ QJsonArray json = parseJSON(&jsonString, 0).toJsonArray();
47+ qCDebug(scriptengine) << "first asset name: " << json.at(0).toObject().value("displayName");
48+ return json;
49+ } else {
50+ qCDebug(scriptengine) << "Invalid filters were specified.";
51+ return QJsonArray();
52+ }
53+}
54+
55+QString GooglePolyScriptingInterface::getFBX(QString keyword, QString category) {
56+ QUrl url = formatURLQuery(keyword, category, "FBX");
57+ qCDebug(scriptengine) << "google url: " << url;
58+ if (!url.isEmpty()) {
59+ QByteArray jsonString = getHTTPRequest(url);
60+ qCDebug(scriptengine) << "the list: " << jsonString;
61+ QString modelURL = parseJSON(&jsonString, 1).toString();
62+
63+ qCDebug(scriptengine) << "the model url: " << modelURL;
64+ return modelURL;
65+ } else {
66+ qCDebug(scriptengine) << "Invalid filters were specified.";
67+ return "";
68+ }
69+
70+}
71+
72+void GooglePolyScriptingInterface::testPrint(QString input) {
73+ qCDebug(scriptengine) << "Google message: " << input;
3574 }
3675
3776 void GooglePolyScriptingInterface::setAPIKey(QString key) {
3877 authCode = key;
3978 }
4079
41−void GooglePolyScriptingInterface::getAssetList() {
42− //authCode = "broke";
43− QUrl url(listPolyUrl + "key=" + authCode);
44− QByteArray jsonString = getHTTPRequest(url);
45− qCDebug(scriptengine) << "the list: " << jsonString;
46− QJsonObject json = makeJSON(&jsonString, true).toJsonObject;
80+int GooglePolyScriptingInterface::getRandIntInRange(int length) {
81+ QTime time = QTime::currentTime();
82+ qsrand((uint)time.msec());
83+ return qrand() % length;
84+}
4785
86+QUrl GooglePolyScriptingInterface::formatURLQuery(QString keyword, QString category, QString format) {
87+ QString queries;
88+ if (!validFormats.contains(format) || !validCategories.contains(category)) {
89+ return QUrl("");
90+ } else {
91+ if (!keyword.isEmpty()) {
92+ keyword.replace(" ", "+");
93+ queries.append("&keywords=" + keyword);
94+ }
95+ if (!category.isEmpty()) {
96+ queries.append("&category=" + category);
97+ }
98+ if (!format.isEmpty()) {
99+ queries.append("&format=" + format);
100+ }
101+ QString urlString = QString(listPolyUrl + "key=" + authCode + queries);
102+ return QUrl(urlString);
103+ }
48104 }
49105
50−// FIXME: synchronous = not good code
106+// FIXME: synchronous
51107 QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) {
52108 QNetworkAccessManager manager;
53109 QNetworkReply *response = manager.get(QNetworkRequest(url));
@@ -56,31 +112,46 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 2 unmodified lines
56112 event.exec();
57113
58114 return response->readAll();
59−
115+
60116 }
61117
62118 // since the list is a QJsonArray and a single model is a QJsonObject
63−QVariant GooglePolyScriptingInterface::makeJSON(QByteArray* response, bool isList) {
119+QVariant GooglePolyScriptingInterface::parseJSON(QByteArray* response, int fileType) {
64120 //QString firstItem = QString::fromUtf8(response->readAll());
65121 QJsonDocument doc = QJsonDocument::fromJson(*response);
66− qCDebug(scriptengine) << "json doc is empty: " << doc.isEmpty();
67122 QJsonObject obj = doc.object();
68123 qCDebug(scriptengine) << "json obj: " << obj;
69124 qCDebug(scriptengine) << "json list: " << obj.keys();
70125 if (obj.keys().first() == "error") {
71126 qCDebug(scriptengine) << "Invalid API key";
72− return obj;
127+ return QJsonObject();
73128 }
74− qCDebug(scriptengine) << "total size: " << obj.value("totalSize");
75129 qCDebug(scriptengine) << "the assets: " << obj.value("assets");
76− QJsonArray arr = obj.value("assets").toArray();
77− qCDebug(scriptengine) << "in array: " << arr;
78− QJsonObject first = arr.takeAt(0).toObject();
79− qCDebug(scriptengine) << "first asset: " << first;
80− qCDebug(scriptengine) << "first asset description: " << first.value("description");
81− return obj;
130+ if (fileType == 0 || fileType == 1) {
131+ QJsonArray arr = obj.value("assets").toArray();
132+ qCDebug(scriptengine) << "in array: " << arr;
133+ // return model url
134+ if (fileType == 1) {
135+ int random = getRandIntInRange(arr.size());
136+ QJsonObject json = arr.at(random).toObject();
137+ qCDebug(scriptengine) << random << "th object: " << json;
138+ qCDebug(scriptengine) << random << "th asset name: " << json.value("displayName");
139+ // nested JSONs
140+ return json.value("formats").toArray().at(0).toObject().value("root").toObject().value("url");
141+ }
142+ // return whole asset list
143+ return arr;
144+ // return specific object
145+ } else {
146+ return obj;
147+ }
82148 }
83149
150+
151+
152+
153+
154+
84155 /*void GooglePolyScriptingInterface::getAssetList() {
85156 authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A";
86157 QUrl url(listPolyUrl + "key=" + authCode);