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
10//10//
11 11
12#include <QEventLoop>12#include <QEventLoop>
13#include <QGlobal.h>
13#include <QJsonArray>14#include <QJsonArray>
14#include <QJsonDocument>15#include <QJsonDocument>
15#include <QJsonObject>16#include <QJsonObject>
@@ -18,7 +19,9 @@ · 2 unmodified lines
18#include <QNetworkRequest>19#include <QNetworkRequest>
19//#include <QScopedPointer>20//#include <QScopedPointer>
20#include <QString>21#include <QString>
22#include <QTime>
21#include <QUrl>23#include <QUrl>
24#include <random>
22 25
23#include "GooglePolyScriptingInterface.h"26#include "GooglePolyScriptingInterface.h"
24#include "ScriptEngineLogging.h"27#include "ScriptEngineLogging.h"
@@ -26,28 +29,81 @@ · 1 unmodified line
26QString listPolyUrl = "https://poly.googleapis.com/v1/assets?";29QString listPolyUrl = "https://poly.googleapis.com/v1/assets?";
27QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?";30QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?";
28 31
32//authCode = "broke";
33
34QStringList validFormats = QStringList() << "BLOCKS" << "FBX" << "GLTF" << "GLTF2" << "OBJ" << "TILT" << "";
35QStringList validCategories = QStringList() << "animals" << "architecture" << "art" << "food" <<
36 "nature" << "objects" << "people" << "scenes" << "technology" << "transport" << "";
37
29GooglePolyScriptingInterface::GooglePolyScriptingInterface() {38GooglePolyScriptingInterface::GooglePolyScriptingInterface() {
30 // nothing to be implemented39 // nothing to be implemented
31}40}
32 41
33void GooglePolyScriptingInterface::testPrint() {42QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) {
34 qCDebug(scriptengine) << "Google Poly interface exists";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
55QString 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
72void GooglePolyScriptingInterface::testPrint(QString input) {
73 qCDebug(scriptengine) << "Google message: " << input;
35}74}
36 75
37void GooglePolyScriptingInterface::setAPIKey(QString key) {76void GooglePolyScriptingInterface::setAPIKey(QString key) {
38 authCode = key;77 authCode = key;
39}78}
40 79
41void GooglePolyScriptingInterface::getAssetList() {80int GooglePolyScriptingInterface::getRandIntInRange(int length) {
42 //authCode = "broke";81 QTime time = QTime::currentTime();
43 QUrl url(listPolyUrl + "key=" + authCode);82 qsrand((uint)time.msec());
44 QByteArray jsonString = getHTTPRequest(url);83 return qrand() % length;
45 qCDebug(scriptengine) << "the list: " << jsonString;84}
46 QJsonObject json = makeJSON(&jsonString, true).toJsonObject;
47 85
86QUrl 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 }
48}104}
49 105
50// FIXME: synchronous = not good code106// FIXME: synchronous
51QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) {107QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) {
52 QNetworkAccessManager manager;108 QNetworkAccessManager manager;
53 QNetworkReply *response = manager.get(QNetworkRequest(url));109 QNetworkReply *response = manager.get(QNetworkRequest(url));
@@ -56,31 +112,46 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 2 unmodified lines
56 event.exec();112 event.exec();
57 113
58 return response->readAll();114 return response->readAll();
59 115
60}116}
61 117
62// since the list is a QJsonArray and a single model is a QJsonObject118// since the list is a QJsonArray and a single model is a QJsonObject
63QVariant GooglePolyScriptingInterface::makeJSON(QByteArray* response, bool isList) {119QVariant GooglePolyScriptingInterface::parseJSON(QByteArray* response, int fileType) {
64 //QString firstItem = QString::fromUtf8(response->readAll());120 //QString firstItem = QString::fromUtf8(response->readAll());
65 QJsonDocument doc = QJsonDocument::fromJson(*response);121 QJsonDocument doc = QJsonDocument::fromJson(*response);
66 qCDebug(scriptengine) << "json doc is empty: " << doc.isEmpty();
67 QJsonObject obj = doc.object();122 QJsonObject obj = doc.object();
68 qCDebug(scriptengine) << "json obj: " << obj;123 qCDebug(scriptengine) << "json obj: " << obj;
69 qCDebug(scriptengine) << "json list: " << obj.keys();124 qCDebug(scriptengine) << "json list: " << obj.keys();
70 if (obj.keys().first() == "error") {125 if (obj.keys().first() == "error") {
71 qCDebug(scriptengine) << "Invalid API key";126 qCDebug(scriptengine) << "Invalid API key";
72 return obj;127 return QJsonObject();
73 }128 }
74 qCDebug(scriptengine) << "total size: " << obj.value("totalSize");
75 qCDebug(scriptengine) << "the assets: " << obj.value("assets");129 qCDebug(scriptengine) << "the assets: " << obj.value("assets");
76 QJsonArray arr = obj.value("assets").toArray();130 if (fileType == 0 || fileType == 1) {
77 qCDebug(scriptengine) << "in array: " << arr;131 QJsonArray arr = obj.value("assets").toArray();
78 QJsonObject first = arr.takeAt(0).toObject();132 qCDebug(scriptengine) << "in array: " << arr;
79 qCDebug(scriptengine) << "first asset: " << first;133 // return model url
80 qCDebug(scriptengine) << "first asset description: " << first.value("description");134 if (fileType == 1) {
81 return obj;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 }
82}148}
83 149
150
151
152
153
154
84/*void GooglePolyScriptingInterface::getAssetList() {155/*void GooglePolyScriptingInterface::getAssetList() {
85 authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A";156 authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A";
86 QUrl url(listPolyUrl + "key=" + authCode);157 QUrl url(listPolyUrl + "key=" + authCode);