mirror of
https://github.com/overte-org/overte.git
synced 2025-08-08 21:17:01 +02:00
added getAssetList() and getFBX()
This commit is contained in:
parent
f7be67b5b2
commit
e49181c3a2
2 changed files with 99 additions and 26 deletions
|
@ -10,6 +10,7 @@
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <QEventLoop>
|
#include <QEventLoop>
|
||||||
|
#include <QGlobal.h>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
@ -18,7 +19,9 @@
|
||||||
#include <QNetworkRequest>
|
#include <QNetworkRequest>
|
||||||
//#include <QScopedPointer>
|
//#include <QScopedPointer>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QTime>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
#include "GooglePolyScriptingInterface.h"
|
#include "GooglePolyScriptingInterface.h"
|
||||||
#include "ScriptEngineLogging.h"
|
#include "ScriptEngineLogging.h"
|
||||||
|
@ -26,28 +29,81 @@
|
||||||
QString listPolyUrl = "https://poly.googleapis.com/v1/assets?";
|
QString listPolyUrl = "https://poly.googleapis.com/v1/assets?";
|
||||||
QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?";
|
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() {
|
GooglePolyScriptingInterface::GooglePolyScriptingInterface() {
|
||||||
// nothing to be implemented
|
// nothing to be implemented
|
||||||
}
|
}
|
||||||
|
|
||||||
void GooglePolyScriptingInterface::testPrint() {
|
QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) {
|
||||||
qCDebug(scriptengine) << "Google Poly interface exists";
|
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) {
|
void GooglePolyScriptingInterface::setAPIKey(QString key) {
|
||||||
authCode = key;
|
authCode = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GooglePolyScriptingInterface::getAssetList() {
|
int GooglePolyScriptingInterface::getRandIntInRange(int length) {
|
||||||
//authCode = "broke";
|
QTime time = QTime::currentTime();
|
||||||
QUrl url(listPolyUrl + "key=" + authCode);
|
qsrand((uint)time.msec());
|
||||||
QByteArray jsonString = getHTTPRequest(url);
|
return qrand() % length;
|
||||||
qCDebug(scriptengine) << "the list: " << jsonString;
|
|
||||||
QJsonObject json = makeJSON(&jsonString, true).toJsonObject;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) {
|
||||||
QNetworkAccessManager manager;
|
QNetworkAccessManager manager;
|
||||||
QNetworkReply *response = manager.get(QNetworkRequest(url));
|
QNetworkReply *response = manager.get(QNetworkRequest(url));
|
||||||
|
@ -56,31 +112,46 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) {
|
||||||
event.exec();
|
event.exec();
|
||||||
|
|
||||||
return response->readAll();
|
return response->readAll();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// since the list is a QJsonArray and a single model is a QJsonObject
|
// 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());
|
//QString firstItem = QString::fromUtf8(response->readAll());
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(*response);
|
QJsonDocument doc = QJsonDocument::fromJson(*response);
|
||||||
qCDebug(scriptengine) << "json doc is empty: " << doc.isEmpty();
|
|
||||||
QJsonObject obj = doc.object();
|
QJsonObject obj = doc.object();
|
||||||
qCDebug(scriptengine) << "json obj: " << obj;
|
qCDebug(scriptengine) << "json obj: " << obj;
|
||||||
qCDebug(scriptengine) << "json list: " << obj.keys();
|
qCDebug(scriptengine) << "json list: " << obj.keys();
|
||||||
if (obj.keys().first() == "error") {
|
if (obj.keys().first() == "error") {
|
||||||
qCDebug(scriptengine) << "Invalid API key";
|
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;
|
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() {
|
/*void GooglePolyScriptingInterface::getAssetList() {
|
||||||
authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A";
|
authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A";
|
||||||
QUrl url(listPolyUrl + "key=" + authCode);
|
QUrl url(listPolyUrl + "key=" + authCode);
|
||||||
|
|
|
@ -21,18 +21,20 @@ public:
|
||||||
GooglePolyScriptingInterface();
|
GooglePolyScriptingInterface();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void testPrint();
|
void testPrint(QString input);
|
||||||
void setAPIKey(QString key);
|
void setAPIKey(QString key);
|
||||||
|
|
||||||
//QJsonArray GooglePolyScriptingInterface::getAssetList();
|
QJsonArray getAssetList(QString keyword, QString category, QString format);
|
||||||
void getAssetList();
|
QString getFBX(QString keyword, QString category);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QUrl formatURLQuery(QString keyword, QString category, QString format);
|
||||||
QByteArray getHTTPRequest(QUrl url);
|
QByteArray getHTTPRequest(QUrl url);
|
||||||
QVariant makeJSON(QByteArray* response, bool isList);
|
QVariant parseJSON(QByteArray* response, int fileType);
|
||||||
|
int getRandIntInRange(int length);
|
||||||
//void onResult(QNetworkReply* reply);
|
//void onResult(QNetworkReply* reply);
|
||||||
|
|
||||||
QString authCode;
|
//QString authCode;
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue