Showing the changes to interface/src/scripting/GooglePolyScriptingInterface.cpp (1 of 2 files). Show all
Unified · Split
| @@ -17,7 +17,6 @@ · 16 unmodified lines | ||
| 17 | 17 | #include <QNetworkAccessManager> |
| 18 | 18 | #include <QNetworkReply> |
| 19 | 19 | #include <QNetworkRequest> |
| 20 | −//#include <QScopedPointer> | |
| 21 | 20 | #include <QString> |
| 22 | 21 | #include <QTime> |
| 23 | 22 | #include <QUrl> |
| @@ -26,10 +25,8 @@ · 2 unmodified lines | ||
| 26 | 25 | #include "GooglePolyScriptingInterface.h" |
| 27 | 26 | #include "ScriptEngineLogging.h" |
| 28 | 27 | |
| 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?"; | |
| 33 | 30 | |
| 34 | 31 | QStringList validFormats = QStringList() << "BLOCKS" << "FBX" << "GLTF" << "GLTF2" << "OBJ" << "TILT" << ""; |
| 35 | 32 | QStringList validCategories = QStringList() << "animals" << "architecture" << "art" << "food" << |
| @@ -42,8 +39,7 @@ GooglePolyScriptingInterface::GooglePolyScriptingInterface() { · 6 unmodified lines | ||
| 42 | 39 | QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) { |
| 43 | 40 | QUrl url = formatURLQuery(keyword, category, format); |
| 44 | 41 | if (!url.isEmpty()) { |
| 45 | − QByteArray jsonString = getHTTPRequest(url); | |
| 46 | − QJsonArray json = parseJSON(&jsonString, 0).toJsonArray(); | |
| 42 | + QJsonArray json = parseJSON(url, 0).toJsonArray(); | |
| 47 | 43 | qCDebug(scriptengine) << "first asset name: " << json.at(0).toObject().value("displayName"); |
| 48 | 44 | return json; |
| 49 | 45 | } else { |
| @@ -54,19 +50,40 @@ QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString c · 4 unmodified lines | ||
| 54 | 50 | |
| 55 | 51 | QString GooglePolyScriptingInterface::getFBX(QString keyword, QString category) { |
| 56 | 52 | 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 | +*/ | |
| 69 | 74 | |
| 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; | |
| 70 | 87 | } |
| 71 | 88 | |
| 72 | 89 | void GooglePolyScriptingInterface::testPrint(QString input) { |
| @@ -103,6 +120,16 @@ QUrl GooglePolyScriptingInterface::formatURLQuery(QString keyword, QString categ · 30 unmodified lines | ||
| 103 | 120 | } |
| 104 | 121 | } |
| 105 | 122 | |
| 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 | + | |
| 106 | 133 | // FIXME: synchronous |
| 107 | 134 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { |
| 108 | 135 | QNetworkAccessManager manager; |
| @@ -115,26 +142,23 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 6 unmodified lines | ||
| 115 | 142 | } |
| 116 | 143 | |
| 117 | 144 | // 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) { | |
| 119 | 146 | //QString firstItem = QString::fromUtf8(response->readAll()); |
| 120 | − QJsonDocument doc = QJsonDocument::fromJson(*response); | |
| 147 | + QByteArray jsonString = getHTTPRequest(url); | |
| 148 | + QJsonDocument doc = QJsonDocument::fromJson(jsonString); | |
| 121 | 149 | QJsonObject obj = doc.object(); |
| 122 | − qCDebug(scriptengine) << "json obj: " << obj; | |
| 123 | − qCDebug(scriptengine) << "json list: " << obj.keys(); | |
| 124 | 150 | 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; | |
| 126 | 153 | return QJsonObject(); |
| 127 | 154 | } |
| 128 | 155 | qCDebug(scriptengine) << "the assets: " << obj.value("assets"); |
| 129 | 156 | if (fileType == 0 || fileType == 1) { |
| 130 | 157 | QJsonArray arr = obj.value("assets").toArray(); |
| 131 | − qCDebug(scriptengine) << "in array: " << arr; | |
| 132 | 158 | // return model url |
| 133 | 159 | if (fileType == 1) { |
| 134 | 160 | int random = getRandIntInRange(arr.size()); |
| 135 | 161 | QJsonObject json = arr.at(random).toObject(); |
| 136 | − qCDebug(scriptengine) << random << "th object: " << json; | |
| 137 | − qCDebug(scriptengine) << random << "th asset name: " << json.value("displayName"); | |
| 138 | 162 | // nested JSONs |
| 139 | 163 | return json.value("formats").toArray().at(0).toObject().value("root").toObject().value("url"); |
| 140 | 164 | } |
| @@ -142,17 +166,14 @@ QVariant GooglePolyScriptingInterface::parseJSON(QByteArray* response, int fileT · 1 unmodified line | ||
| 142 | 166 | return arr; |
| 143 | 167 | // return specific object |
| 144 | 168 | } else { |
| 145 | − return obj; | |
| 169 | + //return obj; | |
| 170 | + //return doc.toJson(); | |
| 171 | + return jsonString; | |
| 146 | 172 | } |
| 147 | 173 | } |
| 148 | 174 | |
| 149 | 175 | |
| 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 | +/* | |
| 156 | 177 | |
| 157 | 178 | // FIXME: synchronous = not good code |
| 158 | 179 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { |
| @@ -164,23 +185,7 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 5 unmodified lines | ||
| 164 | 185 | return response->readAll(); |
| 165 | 186 | } |
| 166 | 187 | |
| 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)); | |
| 174 | 188 | |
| 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 | −*/ | |
| 184 | 189 | |
| 185 | 190 | /* NONE OF THESE HTTP GET METHODS WORK D: |
| 186 | 191 | https://stackoverflow.com/questions/28267477/getting-a-page-content-with-qt kinda used rn |