From e49181c3a22505900cedc39a1b5da2b22fbd1cc9 Mon Sep 17 00:00:00 2001 From: Elisa Lupin-Jimenez Date: Wed, 13 Dec 2017 17:45:57 -0800 Subject: [PATCH] added getAssetList() and getFBX() --- .../GooglePolyScriptingInterface.cpp | 113 ++++++++++++++---- .../scripting/GooglePolyScriptingInterface.h | 12 +- 2 files changed, 99 insertions(+), 26 deletions(-) diff --git a/interface/src/scripting/GooglePolyScriptingInterface.cpp b/interface/src/scripting/GooglePolyScriptingInterface.cpp index b353b4b3b4..8b48f1d115 100644 --- a/interface/src/scripting/GooglePolyScriptingInterface.cpp +++ b/interface/src/scripting/GooglePolyScriptingInterface.cpp @@ -10,6 +10,7 @@ // #include +#include #include #include #include @@ -18,7 +19,9 @@ #include //#include #include +#include #include +#include #include "GooglePolyScriptingInterface.h" #include "ScriptEngineLogging.h" @@ -26,28 +29,81 @@ QString listPolyUrl = "https://poly.googleapis.com/v1/assets?"; QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?"; +//authCode = "broke"; + +QStringList validFormats = QStringList() << "BLOCKS" << "FBX" << "GLTF" << "GLTF2" << "OBJ" << "TILT" << ""; +QStringList validCategories = QStringList() << "animals" << "architecture" << "art" << "food" << + "nature" << "objects" << "people" << "scenes" << "technology" << "transport" << ""; + GooglePolyScriptingInterface::GooglePolyScriptingInterface() { // nothing to be implemented } -void GooglePolyScriptingInterface::testPrint() { - qCDebug(scriptengine) << "Google Poly interface exists"; +QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) { + QUrl url = formatURLQuery(keyword, category, format); + if (!url.isEmpty()) { + QByteArray jsonString = getHTTPRequest(url); + QJsonArray json = parseJSON(&jsonString, 0).toJsonArray(); + qCDebug(scriptengine) << "first asset name: " << json.at(0).toObject().value("displayName"); + return json; + } else { + qCDebug(scriptengine) << "Invalid filters were specified."; + return QJsonArray(); + } +} + +QString GooglePolyScriptingInterface::getFBX(QString keyword, QString category) { + QUrl url = formatURLQuery(keyword, category, "FBX"); + qCDebug(scriptengine) << "google url: " << url; + if (!url.isEmpty()) { + QByteArray jsonString = getHTTPRequest(url); + qCDebug(scriptengine) << "the list: " << jsonString; + QString modelURL = parseJSON(&jsonString, 1).toString(); + + qCDebug(scriptengine) << "the model url: " << modelURL; + return modelURL; + } else { + qCDebug(scriptengine) << "Invalid filters were specified."; + return ""; + } + +} + +void GooglePolyScriptingInterface::testPrint(QString input) { + qCDebug(scriptengine) << "Google message: " << input; } void GooglePolyScriptingInterface::setAPIKey(QString key) { authCode = key; } -void GooglePolyScriptingInterface::getAssetList() { - //authCode = "broke"; - QUrl url(listPolyUrl + "key=" + authCode); - QByteArray jsonString = getHTTPRequest(url); - qCDebug(scriptengine) << "the list: " << jsonString; - QJsonObject json = makeJSON(&jsonString, true).toJsonObject; - +int GooglePolyScriptingInterface::getRandIntInRange(int length) { + QTime time = QTime::currentTime(); + qsrand((uint)time.msec()); + return qrand() % length; } -// FIXME: synchronous = not good code +QUrl GooglePolyScriptingInterface::formatURLQuery(QString keyword, QString category, QString format) { + QString queries; + if (!validFormats.contains(format) || !validCategories.contains(category)) { + return QUrl(""); + } else { + if (!keyword.isEmpty()) { + keyword.replace(" ", "+"); + queries.append("&keywords=" + keyword); + } + if (!category.isEmpty()) { + queries.append("&category=" + category); + } + if (!format.isEmpty()) { + queries.append("&format=" + format); + } + QString urlString = QString(listPolyUrl + "key=" + authCode + queries); + return QUrl(urlString); + } +} + +// FIXME: synchronous QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { QNetworkAccessManager manager; QNetworkReply *response = manager.get(QNetworkRequest(url)); @@ -56,31 +112,46 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { event.exec(); return response->readAll(); - + } // since the list is a QJsonArray and a single model is a QJsonObject -QVariant GooglePolyScriptingInterface::makeJSON(QByteArray* response, bool isList) { +QVariant GooglePolyScriptingInterface::parseJSON(QByteArray* response, int fileType) { //QString firstItem = QString::fromUtf8(response->readAll()); QJsonDocument doc = QJsonDocument::fromJson(*response); - qCDebug(scriptengine) << "json doc is empty: " << doc.isEmpty(); QJsonObject obj = doc.object(); qCDebug(scriptengine) << "json obj: " << obj; qCDebug(scriptengine) << "json list: " << obj.keys(); if (obj.keys().first() == "error") { qCDebug(scriptengine) << "Invalid API key"; + return QJsonObject(); + } + qCDebug(scriptengine) << "the assets: " << obj.value("assets"); + if (fileType == 0 || fileType == 1) { + QJsonArray arr = obj.value("assets").toArray(); + qCDebug(scriptengine) << "in array: " << arr; + // return model url + if (fileType == 1) { + int random = getRandIntInRange(arr.size()); + QJsonObject json = arr.at(random).toObject(); + qCDebug(scriptengine) << random << "th object: " << json; + qCDebug(scriptengine) << random << "th asset name: " << json.value("displayName"); + // nested JSONs + return json.value("formats").toArray().at(0).toObject().value("root").toObject().value("url"); + } + // return whole asset list + return arr; + // return specific object + } else { return obj; } - qCDebug(scriptengine) << "total size: " << obj.value("totalSize"); - qCDebug(scriptengine) << "the assets: " << obj.value("assets"); - QJsonArray arr = obj.value("assets").toArray(); - qCDebug(scriptengine) << "in array: " << arr; - QJsonObject first = arr.takeAt(0).toObject(); - qCDebug(scriptengine) << "first asset: " << first; - qCDebug(scriptengine) << "first asset description: " << first.value("description"); - return obj; } + + + + + /*void GooglePolyScriptingInterface::getAssetList() { authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A"; QUrl url(listPolyUrl + "key=" + authCode); diff --git a/interface/src/scripting/GooglePolyScriptingInterface.h b/interface/src/scripting/GooglePolyScriptingInterface.h index 707eabc0af..a90557e593 100644 --- a/interface/src/scripting/GooglePolyScriptingInterface.h +++ b/interface/src/scripting/GooglePolyScriptingInterface.h @@ -21,18 +21,20 @@ public: GooglePolyScriptingInterface(); public slots: - void testPrint(); + void testPrint(QString input); void setAPIKey(QString key); - //QJsonArray GooglePolyScriptingInterface::getAssetList(); - void getAssetList(); + QJsonArray getAssetList(QString keyword, QString category, QString format); + QString getFBX(QString keyword, QString category); private: + QUrl formatURLQuery(QString keyword, QString category, QString format); QByteArray getHTTPRequest(QUrl url); - QVariant makeJSON(QByteArray* response, bool isList); + QVariant parseJSON(QByteArray* response, int fileType); + int getRandIntInRange(int length); //void onResult(QNetworkReply* reply); - QString authCode; + //QString authCode; };