Showing the changes to interface/src/scripting/GooglePolyScriptingInterface.cpp (1 of 2 files). Show all
Unified · Split
| @@ -10,6 +10,7 @@ · 9 unmodified lines | ||
| 10 | 10 | // |
| 11 | 11 | |
| 12 | 12 | #include <QEventLoop> |
| 13 | +#include <QGlobal.h> | |
| 13 | 14 | #include <QJsonArray> |
| 14 | 15 | #include <QJsonDocument> |
| 15 | 16 | #include <QJsonObject> |
| @@ -18,7 +19,9 @@ · 2 unmodified lines | ||
| 18 | 19 | #include <QNetworkRequest> |
| 19 | 20 | //#include <QScopedPointer> |
| 20 | 21 | #include <QString> |
| 22 | +#include <QTime> | |
| 21 | 23 | #include <QUrl> |
| 24 | +#include <random> | |
| 22 | 25 | |
| 23 | 26 | #include "GooglePolyScriptingInterface.h" |
| 24 | 27 | #include "ScriptEngineLogging.h" |
| @@ -26,28 +29,81 @@ · 1 unmodified line | ||
| 26 | 29 | QString listPolyUrl = "https://poly.googleapis.com/v1/assets?"; |
| 27 | 30 | QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?"; |
| 28 | 31 | |
| 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 | + | |
| 29 | 38 | GooglePolyScriptingInterface::GooglePolyScriptingInterface() { |
| 30 | 39 | // nothing to be implemented |
| 31 | 40 | } |
| 32 | 41 | |
| 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; | |
| 35 | 74 | } |
| 36 | 75 | |
| 37 | 76 | void GooglePolyScriptingInterface::setAPIKey(QString key) { |
| 38 | 77 | authCode = key; |
| 39 | 78 | } |
| 40 | 79 | |
| 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 | +} | |
| 47 | 85 | |
| 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 | + } | |
| 48 | 104 | } |
| 49 | 105 | |
| 50 | −// FIXME: synchronous = not good code | |
| 106 | +// FIXME: synchronous | |
| 51 | 107 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { |
| 52 | 108 | QNetworkAccessManager manager; |
| 53 | 109 | QNetworkReply *response = manager.get(QNetworkRequest(url)); |
| @@ -56,31 +112,46 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 2 unmodified lines | ||
| 56 | 112 | event.exec(); |
| 57 | 113 | |
| 58 | 114 | return response->readAll(); |
| 59 | − | |
| 115 | + | |
| 60 | 116 | } |
| 61 | 117 | |
| 62 | 118 | // 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) { | |
| 64 | 120 | //QString firstItem = QString::fromUtf8(response->readAll()); |
| 65 | 121 | QJsonDocument doc = QJsonDocument::fromJson(*response); |
| 66 | − qCDebug(scriptengine) << "json doc is empty: " << doc.isEmpty(); | |
| 67 | 122 | QJsonObject obj = doc.object(); |
| 68 | 123 | qCDebug(scriptengine) << "json obj: " << obj; |
| 69 | 124 | qCDebug(scriptengine) << "json list: " << obj.keys(); |
| 70 | 125 | if (obj.keys().first() == "error") { |
| 71 | 126 | qCDebug(scriptengine) << "Invalid API key"; |
| 72 | − return obj; | |
| 127 | + return QJsonObject(); | |
| 73 | 128 | } |
| 74 | − qCDebug(scriptengine) << "total size: " << obj.value("totalSize"); | |
| 75 | 129 | 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 | + } | |
| 82 | 148 | } |
| 83 | 149 | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 84 | 155 | /*void GooglePolyScriptingInterface::getAssetList() { |
| 85 | 156 | authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A"; |
| 86 | 157 | QUrl url(listPolyUrl + "key=" + authCode); |