History

enabled fbx, obj, and blocks model url retrieval

Elisa Lupin-Jimenez committed 8 years ago

Commit e7fad051 parent 0e57ed52 files · +63 −52Browse files

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

Unified · Split

interface/src/scripting/GooglePolyScriptingInterface.cpp+55−50
@@ -17,7 +17,6 @@ · 16 unmodified lines
1717 #include <QNetworkAccessManager>
1818 #include <QNetworkReply>
1919 #include <QNetworkRequest>
20−//#include <QScopedPointer>
2120 #include <QString>
2221 #include <QTime>
2322 #include <QUrl>
@@ -26,10 +25,8 @@ · 2 unmodified lines
2625 #include "GooglePolyScriptingInterface.h"
2726 #include "ScriptEngineLogging.h"
2827
29−QString listPolyUrl = "https://poly.googleapis.com/v1/assets?";
30−QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?";
31−
32−QString authCode = "broke";
28+const QString listPolyUrl = "https://poly.googleapis.com/v1/assets?";
29+const QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?";
3330
3431 QStringList validFormats = QStringList() << "BLOCKS" << "FBX" << "GLTF" << "GLTF2" << "OBJ" << "TILT" << "";
3532 QStringList validCategories = QStringList() << "animals" << "architecture" << "art" << "food" <<
@@ -42,8 +39,7 @@ GooglePolyScriptingInterface::GooglePolyScriptingInterface() { · 6 unmodified lines
4239 QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) {
4340 QUrl url = formatURLQuery(keyword, category, format);
4441 if (!url.isEmpty()) {
45− QByteArray jsonString = getHTTPRequest(url);
46− QJsonArray json = parseJSON(&jsonString, 0).toJsonArray();
42+ QJsonArray json = parseJSON(url, 0).toJsonArray();
4743 qCDebug(scriptengine) << "first asset name: " << json.at(0).toObject().value("displayName");
4844 return json;
4945 } else {
@@ -54,19 +50,40 @@ QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString c · 4 unmodified lines
5450
5551 QString GooglePolyScriptingInterface::getFBX(QString keyword, QString category) {
5652 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− }
53+ return getModelURL(url);
54+}
55+
56+QString GooglePolyScriptingInterface::getOBJ(QString keyword, QString category) {
57+ QUrl url = formatURLQuery(keyword, category, "OBJ");
58+ return getModelURL(url);
59+}
60+
61+QString GooglePolyScriptingInterface::getBlocks(QString keyword, QString category) {
62+ QUrl url = formatURLQuery(keyword, category, "BLOCKS");
63+ qCDebug(scriptengine) << "Google url request: " << url;
64+ return getModelURL(url);
65+}
66+
67+/*
68+// This method will not work until we support Tilt models
69+QString GooglePolyScriptingInterface::getTilt(QString keyword, QString category) {
70+ QUrl url = formatURLQuery(keyword, category, "TILT");
71+ return getModelURL(url);
72+}
73+*/
6974
75+// can provide asset name or full URL to model
76+QString GooglePolyScriptingInterface::getModelInfo(QString name) {
77+ if (name.contains("poly.googleapis")) {
78+ QStringList list = name.split("/");
79+ name = list[4];
80+ }
81+ QString urlString = QString(getPolyUrl);
82+ urlString = urlString.replace("model", name) + "key=" + authCode;
83+ qCDebug(scriptengine) << "google get url: " << urlString;
84+ QUrl url(urlString);
85+ QByteArray json = parseJSON(url, 2).toByteArray();
86+ return json;
7087 }
7188
7289 void GooglePolyScriptingInterface::testPrint(QString input) {
@@ -103,6 +120,16 @@ QUrl GooglePolyScriptingInterface::formatURLQuery(QString keyword, QString categ · 30 unmodified lines
103120 }
104121 }
105122
123+QString GooglePolyScriptingInterface::getModelURL(QUrl url) {
124+ qCDebug(scriptengine) << "Google url request: " << url;
125+ if (!url.isEmpty()) {
126+ return parseJSON(url, 1).toString();
127+ } else {
128+ qCDebug(scriptengine) << "Invalid filters were specified.";
129+ return "";
130+ }
131+}
132+
106133 // FIXME: synchronous
107134 QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) {
108135 QNetworkAccessManager manager;
@@ -115,26 +142,23 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 6 unmodified lines
115142 }
116143
117144 // since the list is a QJsonArray and a single model is a QJsonObject
118−QVariant GooglePolyScriptingInterface::parseJSON(QByteArray* response, int fileType) {
145+QVariant GooglePolyScriptingInterface::parseJSON(QUrl url, int fileType) {
119146 //QString firstItem = QString::fromUtf8(response->readAll());
120− QJsonDocument doc = QJsonDocument::fromJson(*response);
147+ QByteArray jsonString = getHTTPRequest(url);
148+ QJsonDocument doc = QJsonDocument::fromJson(jsonString);
121149 QJsonObject obj = doc.object();
122− qCDebug(scriptengine) << "json obj: " << obj;
123− qCDebug(scriptengine) << "json list: " << obj.keys();
124150 if (obj.keys().first() == "error") {
125− qCDebug(scriptengine) << "Invalid API key";
151+ QString error = obj.value("error").toObject().value("message").toString();
152+ qCDebug(scriptengine) << error;
126153 return QJsonObject();
127154 }
128155 qCDebug(scriptengine) << "the assets: " << obj.value("assets");
129156 if (fileType == 0 || fileType == 1) {
130157 QJsonArray arr = obj.value("assets").toArray();
131− qCDebug(scriptengine) << "in array: " << arr;
132158 // return model url
133159 if (fileType == 1) {
134160 int random = getRandIntInRange(arr.size());
135161 QJsonObject json = arr.at(random).toObject();
136− qCDebug(scriptengine) << random << "th object: " << json;
137− qCDebug(scriptengine) << random << "th asset name: " << json.value("displayName");
138162 // nested JSONs
139163 return json.value("formats").toArray().at(0).toObject().value("root").toObject().value("url");
140164 }
@@ -142,17 +166,14 @@ QVariant GooglePolyScriptingInterface::parseJSON(QByteArray* response, int fileT · 1 unmodified line
142166 return arr;
143167 // return specific object
144168 } else {
145− return obj;
169+ //return obj;
170+ //return doc.toJson();
171+ return jsonString;
146172 }
147173 }
148174
149175
150−/*void GooglePolyScriptingInterface::getAssetList() {
151− authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A";
152− QUrl url(listPolyUrl + "key=" + authCode);
153− QByteArray reply = getHTTPRequest(url);
154− qCDebug(scriptengine) << "the list: " << test;
155−}
176+/*
156177
157178 // FIXME: synchronous = not good code
158179 QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) {
@@ -164,23 +185,7 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 5 unmodified lines
164185 return response->readAll();
165186 }
166187
167−*/
168−
169−/* useful for constructing the url?
170−
171− QUrl url("http://gdata.youtube.com/feeds/api/standardfeeds/");
172− QString method = "most_popular";
173− url.setPath(QString("%1%2").arg(url.path()).arg(method));
174188
175− QMap<QString, QVariant> params;
176− params["alt"] = "json";
177− params["v"] = "2";
178−
179− foreach(QString param, params.keys()) {
180− url.addQueryItem(param, params[param].toString());
181− }
182−
183−*/
184189
185190 /* NONE OF THESE HTTP GET METHODS WORK D:
186191 https://stackoverflow.com/questions/28267477/getting-a-page-content-with-qt kinda used rn