Showing the changes to interface/src/scripting/GooglePolyScriptingInterface.cpp (1 of 2 files). Show all
Unified · Split
| @@ -43,13 +43,10 @@ void GooglePolyScriptingInterface::setAPIKey(QString key) { · 42 unmodified lines | |||
| 43 | QString GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) { | 43 | QString GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) { |
| 44 | QUrl url = formatURLQuery(keyword, category, format); | 44 | QUrl url = formatURLQuery(keyword, category, format); |
| 45 | if (!url.isEmpty()) { | 45 | if (!url.isEmpty()) { |
| 46 | //QJsonArray json = parseJSON(url, 0).toJsonArray(); | ||
| 47 | //qCDebug(scriptengine) << "first asset name: " << json.at(0).toObject().value("displayName"); | ||
| 48 | QByteArray json = parseJSON(url, 0).toJsonDocument().toJson(); | 46 | QByteArray json = parseJSON(url, 0).toJsonDocument().toJson(); |
| 49 | return (QString) json; | 47 | return (QString) json; |
| 50 | } else { | 48 | } else { |
| 51 | qCDebug(scriptengine) << "Invalid filters were specified."; | 49 | qCDebug(scriptengine) << "Invalid filters were specified."; |
| 52 | //return QJsonArray(); | ||
| 53 | return ""; | 50 | return ""; |
| 54 | } | 51 | } |
| 55 | } | 52 | } |
| @@ -87,7 +84,7 @@ QString GooglePolyScriptingInterface::getTilt(QString keyword, QString category) · 31 unmodified lines | |||
| 87 | } | 84 | } |
| 88 | */ | 85 | */ |
| 89 | 86 | ||
| 90 | // can provide asset name or full URL to model | 87 | // Can provide asset name or full URL to model |
| 91 | QString GooglePolyScriptingInterface::getModelInfo(QString name) { | 88 | QString GooglePolyScriptingInterface::getModelInfo(QString name) { |
| 92 | if (name.contains("poly.googleapis") || name.contains("poly.google.com")) { | 89 | if (name.contains("poly.googleapis") || name.contains("poly.google.com")) { |
| 93 | QStringList list = name.split("/"); | 90 | QStringList list = name.split("/"); |
| @@ -101,10 +98,6 @@ QString GooglePolyScriptingInterface::getModelInfo(QString name) { · 7 unmodified lines | |||
| 101 | return json; | 98 | return json; |
| 102 | } | 99 | } |
| 103 | 100 | ||
| 104 | void GooglePolyScriptingInterface::testPrint(QString input) { | ||
| 105 | qCDebug(scriptengine) << "Google message: " << input; | ||
| 106 | } | ||
| 107 | |||
| 108 | int GooglePolyScriptingInterface::getRandIntInRange(int length) { | 101 | int GooglePolyScriptingInterface::getRandIntInRange(int length) { |
| 109 | QTime time = QTime::currentTime(); | 102 | QTime time = QTime::currentTime(); |
| 110 | qsrand((uint)time.msec()); | 103 | qsrand((uint)time.msec()); |
| @@ -152,7 +145,7 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 41 unmodified lines | |||
| 152 | return response->readAll(); | 145 | return response->readAll(); |
| 153 | } | 146 | } |
| 154 | 147 | ||
| 155 | // since the list is a QJsonArray and a single model is a QJsonObject | 148 | // 0 = asset list, 1 = model from asset list, 2 = specific model |
| 156 | QVariant GooglePolyScriptingInterface::parseJSON(QUrl url, int fileType) { | 149 | QVariant GooglePolyScriptingInterface::parseJSON(QUrl url, int fileType) { |
| 157 | //QString firstItem = QString::fromUtf8(response->readAll()); | 150 | //QString firstItem = QString::fromUtf8(response->readAll()); |
| 158 | QByteArray jsonString = getHTTPRequest(url); | 151 | QByteArray jsonString = getHTTPRequest(url); |
| @@ -173,109 +166,9 @@ QVariant GooglePolyScriptingInterface::parseJSON(QUrl url, int fileType) { · 14 unmodified lines | |||
| 173 | return json.value("formats").toArray().at(0).toObject().value("root").toObject().value("url"); | 166 | return json.value("formats").toArray().at(0).toObject().value("root").toObject().value("url"); |
| 174 | } | 167 | } |
| 175 | // return whole asset list | 168 | // return whole asset list |
| 176 | //return arr; | ||
| 177 | return QJsonDocument(arr); | 169 | return QJsonDocument(arr); |
| 178 | // return specific object | 170 | // return specific object |
| 179 | } else { | 171 | } else { |
| 180 | return jsonString; | 172 | return jsonString; |
| 181 | } | 173 | } |
| 182 | } | 174 | } |
| 183 | |||
| 184 | |||
| 185 | /* | ||
| 186 | |||
| 187 | // FIXME: synchronous = not good code | ||
| 188 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { | ||
| 189 | QNetworkAccessManager manager; | ||
| 190 | QNetworkReply *response = manager.get(QNetworkRequest(url)); | ||
| 191 | QEventLoop event; | ||
| 192 | connect(response, SIGNAL(finished()), &event, SLOT(quit())); | ||
| 193 | event.exec(); | ||
| 194 | return response->readAll(); | ||
| 195 | } | ||
| 196 | |||
| 197 | |||
| 198 | |||
| 199 | /* NONE OF THESE HTTP GET METHODS WORK D: | ||
| 200 | https://stackoverflow.com/questions/28267477/getting-a-page-content-with-qt kinda used rn | ||
| 201 | |||
| 202 | this correctly returns the asset list but is apparently synchronous and not a good way to do it | ||
| 203 | https://stackoverflow.com/questions/24965972/qt-getting-source-html-code-of-a-web-page-hosted-on-the-internet | ||
| 204 | void GooglePolyScriptingInterface::getAssetList() { | ||
| 205 | authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A"; | ||
| 206 | QUrl url(listPolyUrl + "key=" + authCode); | ||
| 207 | QNetworkAccessManager manager; | ||
| 208 | QNetworkReply *response = manager.get(QNetworkRequest(url)); | ||
| 209 | QEventLoop event; | ||
| 210 | connect(response, SIGNAL(finished()), &event, SLOT(quit())); | ||
| 211 | event.exec(); | ||
| 212 | QString firstItem = response->readAll(); | ||
| 213 | //QJsonArray arr; | ||
| 214 | //QJsonObject jsonObject = QJsonDocument::fromJson(response->readAll()).object(); | ||
| 215 | //QString firstItem = jsonObject["assets"].toString(); | ||
| 216 | qCDebug(scriptengine) << "first item: " << firstItem; | ||
| 217 | //qCDebug(scriptengine) << "api key: " << authCode; | ||
| 218 | //return arr; | ||
| 219 | } | ||
| 220 | |||
| 221 | this didn't work either https://stackoverflow.com/a/24966317 | ||
| 222 | QScopedPointer<QNetworkAccessManager> manager(new QNetworkAccessManager); | ||
| 223 | QNetworkReply* response = manager->get(QNetworkRequest(url)); | ||
| 224 | QObject::connect(manager, &QNetworkAccessManager::finished, [manager, response] { | ||
| 225 | response->deleteLater(); | ||
| 226 | manager->deleteLater(); | ||
| 227 | if (response->error() != QNetworkReply::NoError) return; | ||
| 228 | QString contentType = | ||
| 229 | response->header(QNetworkRequest::ContentTypeHeader).toString(); | ||
| 230 | if (!contentType.contains("charset=utf-8")) { | ||
| 231 | qWarning() << "Content charsets other than utf-8 are not implemented yet."; | ||
| 232 | return; | ||
| 233 | } | ||
| 234 | QString html = QString::fromUtf8(response->readAll()); | ||
| 235 | // do something with the data | ||
| 236 | }) && manager.take(); | ||
| 237 | |||
| 238 | |||
| 239 | or this :( https://stackoverflow.com/questions/39518749/make-get-request-to-web-service-get-json-response-and-update-gui-in-qt | ||
| 240 | qCDebug(scriptengine) << "gonna get assets with " << url; | ||
| 241 | QNetworkAccessManager manager; | ||
| 242 | QNetworkReply* reply = manager.get(QNetworkRequest(url)); | ||
| 243 | QObject::connect(&manager, &QNetworkAccessManager::finished, | ||
| 244 | [reply]{ | ||
| 245 | qCDebug(scriptengine) << "boo radley"; | ||
| 246 | if (reply->error() != QNetworkReply::NoError) { | ||
| 247 | qCDebug(scriptengine) << "Poly API failed to respond"; | ||
| 248 | //manager.clearAccessCache(); | ||
| 249 | } else { | ||
| 250 | QJsonObject jsonObject = QJsonDocument::fromJson(reply->readAll()).object(); | ||
| 251 | QString firstItem = jsonObject["assets"].toString(); | ||
| 252 | qCDebug(scriptengine) << "first item: " << firstItem; | ||
| 253 | } | ||
| 254 | reply->deleteLater(); | ||
| 255 | }); | ||
| 256 | |||
| 257 | or this :(( http://blog.mathieu-leplatre.info/access-a-json-webservice-with-qt-c.html | ||
| 258 | //QJsonArray arr; | ||
| 259 | //QJsonObject jsonObject = QJsonDocument::fromJson(response->readAll()).object(); | ||
| 260 | //QString firstItem = jsonObject["assets"].toString(); | ||
| 261 | qCDebug(scriptengine) << "first item: " << firstItem; | ||
| 262 | //qCDebug(scriptengine) << "api key: " << authCode; | ||
| 263 | //return arr; | ||
| 264 | QNetworkAccessManager manager; | ||
| 265 | QNetworkReply* response = manager.get(QNetworkRequest(url)); | ||
| 266 | QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*))); | ||
| 267 | |||
| 268 | return ""; | ||
| 269 | } | ||
| 270 | |||
| 271 | void GooglePolyScriptingInterface::onResult(QNetworkReply* reply) { | ||
| 272 | if (reply->error() != QNetworkReply::NoError) { | ||
| 273 | qCDebug(scriptengine) << "Poly API failed to respond"; | ||
| 274 | return; | ||
| 275 | } | ||
| 276 | QString firstItem = reply->readAll(); | ||
| 277 | |||
| 278 | qCDebug(scriptengine) << "first item: " << firstItem; | ||
| 279 | } | ||
| 280 | |||
| 281 | */ | ||