Showing the changes to interface/src/scripting/GooglePolyScriptingInterface.cpp (1 of 2 files). Show all
Unified · Split
| @@ -9,13 +9,188 @@ · 8 unmodified lines | |||
| 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | 9 | // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html |
| 10 | // | 10 | // |
| 11 | 11 | ||
| 12 | #include <QEventLoop> | ||
| 13 | #include <QJsonArray> | ||
| 14 | #include <QJsonDocument> | ||
| 15 | #include <QJsonObject> | ||
| 16 | #include <QNetworkAccessManager> | ||
| 17 | #include <QNetworkReply> | ||
| 18 | #include <QNetworkRequest> | ||
| 19 | //#include <QScopedPointer> | ||
| 20 | #include <QString> | ||
| 21 | #include <QUrl> | ||
| 22 | |||
| 12 | #include "GooglePolyScriptingInterface.h" | 23 | #include "GooglePolyScriptingInterface.h" |
| 13 | #include "ScriptEngineLogging.h" | 24 | #include "ScriptEngineLogging.h" |
| 14 | 25 | ||
| 15 | GooglePolyScriptingInterface::GooglePolyScriptingInterface() { | 26 | QString listPolyUrl = "https://poly.googleapis.com/v1/assets?"; |
| 27 | QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?"; | ||
| 16 | 28 | ||
| 29 | GooglePolyScriptingInterface::GooglePolyScriptingInterface() { | ||
| 30 | // nothing to be implemented | ||
| 17 | } | 31 | } |
| 18 | 32 | ||
| 19 | void GooglePolyScriptingInterface::testPrint() { | 33 | void GooglePolyScriptingInterface::testPrint() { |
| 20 | qCDebug(scriptengine) << "Google Poly interface exists"; | 34 | qCDebug(scriptengine) << "Google Poly interface exists"; |
| 21 | } | 35 | } |
| 36 | |||
| 37 | void GooglePolyScriptingInterface::setAPIKey(QString key) { | ||
| 38 | authCode = key; | ||
| 39 | } | ||
| 40 | |||
| 41 | void GooglePolyScriptingInterface::getAssetList() { | ||
| 42 | authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A"; | ||
| 43 | //authCode = "broke"; | ||
| 44 | QUrl url(listPolyUrl + "key=" + authCode); | ||
| 45 | QByteArray jsonString = getHTTPRequest(url); | ||
| 46 | qCDebug(scriptengine) << "the list: " << jsonString; | ||
| 47 | QJsonObject json = makeJSONObject(&jsonString, true); | ||
| 48 | |||
| 49 | } | ||
| 50 | |||
| 51 | // FIXME: synchronous = not good code | ||
| 52 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { | ||
| 53 | QNetworkAccessManager manager; | ||
| 54 | QNetworkReply *response = manager.get(QNetworkRequest(url)); | ||
| 55 | QEventLoop event; | ||
| 56 | connect(response, SIGNAL(finished()), &event, SLOT(quit())); | ||
| 57 | event.exec(); | ||
| 58 | |||
| 59 | return response->readAll(); | ||
| 60 | |||
| 61 | } | ||
| 62 | |||
| 63 | QJsonObject GooglePolyScriptingInterface::makeJSONObject(QByteArray* response, bool isList) { | ||
| 64 | //QString firstItem = QString::fromUtf8(response->readAll()); | ||
| 65 | QJsonDocument doc = QJsonDocument::fromJson(*response); | ||
| 66 | qCDebug(scriptengine) << "json doc is empty: " << doc.isEmpty(); | ||
| 67 | QJsonObject obj = doc.object(); | ||
| 68 | qCDebug(scriptengine) << "json obj: " << obj; | ||
| 69 | qCDebug(scriptengine) << "json list: " << obj.keys(); | ||
| 70 | if (obj.keys().first() == "error") { | ||
| 71 | qCDebug(scriptengine) << "Invalid API key"; | ||
| 72 | return obj; | ||
| 73 | } | ||
| 74 | qCDebug(scriptengine) << "total size: " << obj.value("totalSize").toString(); | ||
| 75 | qCDebug(scriptengine) << "in assets: " << obj.value("assets"); | ||
| 76 | return obj; | ||
| 77 | } | ||
| 78 | |||
| 79 | /*void GooglePolyScriptingInterface::getAssetList() { | ||
| 80 | authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A"; | ||
| 81 | QUrl url(listPolyUrl + "key=" + authCode); | ||
| 82 | QByteArray reply = getHTTPRequest(url); | ||
| 83 | qCDebug(scriptengine) << "the list: " << test; | ||
| 84 | } | ||
| 85 | |||
| 86 | // FIXME: synchronous = not good code | ||
| 87 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { | ||
| 88 | QNetworkAccessManager manager; | ||
| 89 | QNetworkReply *response = manager.get(QNetworkRequest(url)); | ||
| 90 | QEventLoop event; | ||
| 91 | connect(response, SIGNAL(finished()), &event, SLOT(quit())); | ||
| 92 | event.exec(); | ||
| 93 | return response->readAll(); | ||
| 94 | } | ||
| 95 | |||
| 96 | */ | ||
| 97 | |||
| 98 | /* useful for constructing the url? | ||
| 99 | |||
| 100 | QUrl url("http://gdata.youtube.com/feeds/api/standardfeeds/"); | ||
| 101 | QString method = "most_popular"; | ||
| 102 | url.setPath(QString("%1%2").arg(url.path()).arg(method)); | ||
| 103 | |||
| 104 | QMap<QString, QVariant> params; | ||
| 105 | params["alt"] = "json"; | ||
| 106 | params["v"] = "2"; | ||
| 107 | |||
| 108 | foreach(QString param, params.keys()) { | ||
| 109 | url.addQueryItem(param, params[param].toString()); | ||
| 110 | } | ||
| 111 | |||
| 112 | */ | ||
| 113 | |||
| 114 | /* NONE OF THESE HTTP GET METHODS WORK D: | ||
| 115 | https://stackoverflow.com/questions/28267477/getting-a-page-content-with-qt kinda used rn | ||
| 116 | |||
| 117 | this correctly returns the asset list but is apparently synchronous and not a good way to do it | ||
| 118 | https://stackoverflow.com/questions/24965972/qt-getting-source-html-code-of-a-web-page-hosted-on-the-internet | ||
| 119 | void GooglePolyScriptingInterface::getAssetList() { | ||
| 120 | authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A"; | ||
| 121 | QUrl url(listPolyUrl + "key=" + authCode); | ||
| 122 | QNetworkAccessManager manager; | ||
| 123 | QNetworkReply *response = manager.get(QNetworkRequest(url)); | ||
| 124 | QEventLoop event; | ||
| 125 | connect(response, SIGNAL(finished()), &event, SLOT(quit())); | ||
| 126 | event.exec(); | ||
| 127 | QString firstItem = response->readAll(); | ||
| 128 | //QJsonArray arr; | ||
| 129 | //QJsonObject jsonObject = QJsonDocument::fromJson(response->readAll()).object(); | ||
| 130 | //QString firstItem = jsonObject["assets"].toString(); | ||
| 131 | qCDebug(scriptengine) << "first item: " << firstItem; | ||
| 132 | //qCDebug(scriptengine) << "api key: " << authCode; | ||
| 133 | //return arr; | ||
| 134 | } | ||
| 135 | |||
| 136 | this didn't work either https://stackoverflow.com/a/24966317 | ||
| 137 | QScopedPointer<QNetworkAccessManager> manager(new QNetworkAccessManager); | ||
| 138 | QNetworkReply* response = manager->get(QNetworkRequest(url)); | ||
| 139 | QObject::connect(manager, &QNetworkAccessManager::finished, [manager, response] { | ||
| 140 | response->deleteLater(); | ||
| 141 | manager->deleteLater(); | ||
| 142 | if (response->error() != QNetworkReply::NoError) return; | ||
| 143 | QString contentType = | ||
| 144 | response->header(QNetworkRequest::ContentTypeHeader).toString(); | ||
| 145 | if (!contentType.contains("charset=utf-8")) { | ||
| 146 | qWarning() << "Content charsets other than utf-8 are not implemented yet."; | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | QString html = QString::fromUtf8(response->readAll()); | ||
| 150 | // do something with the data | ||
| 151 | }) && manager.take(); | ||
| 152 | |||
| 153 | |||
| 154 | or this :( https://stackoverflow.com/questions/39518749/make-get-request-to-web-service-get-json-response-and-update-gui-in-qt | ||
| 155 | qCDebug(scriptengine) << "gonna get assets with " << url; | ||
| 156 | QNetworkAccessManager manager; | ||
| 157 | QNetworkReply* reply = manager.get(QNetworkRequest(url)); | ||
| 158 | QObject::connect(&manager, &QNetworkAccessManager::finished, | ||
| 159 | [reply]{ | ||
| 160 | qCDebug(scriptengine) << "boo radley"; | ||
| 161 | if (reply->error() != QNetworkReply::NoError) { | ||
| 162 | qCDebug(scriptengine) << "Poly API failed to respond"; | ||
| 163 | //manager.clearAccessCache(); | ||
| 164 | } else { | ||
| 165 | QJsonObject jsonObject = QJsonDocument::fromJson(reply->readAll()).object(); | ||
| 166 | QString firstItem = jsonObject["assets"].toString(); | ||
| 167 | qCDebug(scriptengine) << "first item: " << firstItem; | ||
| 168 | } | ||
| 169 | reply->deleteLater(); | ||
| 170 | }); | ||
| 171 | |||
| 172 | or this :(( http://blog.mathieu-leplatre.info/access-a-json-webservice-with-qt-c.html | ||
| 173 | //QJsonArray arr; | ||
| 174 | //QJsonObject jsonObject = QJsonDocument::fromJson(response->readAll()).object(); | ||
| 175 | //QString firstItem = jsonObject["assets"].toString(); | ||
| 176 | qCDebug(scriptengine) << "first item: " << firstItem; | ||
| 177 | //qCDebug(scriptengine) << "api key: " << authCode; | ||
| 178 | //return arr; | ||
| 179 | QNetworkAccessManager manager; | ||
| 180 | QNetworkReply* response = manager.get(QNetworkRequest(url)); | ||
| 181 | QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(onResult(QNetworkReply*))); | ||
| 182 | |||
| 183 | return ""; | ||
| 184 | } | ||
| 185 | |||
| 186 | void GooglePolyScriptingInterface::onResult(QNetworkReply* reply) { | ||
| 187 | if (reply->error() != QNetworkReply::NoError) { | ||
| 188 | qCDebug(scriptengine) << "Poly API failed to respond"; | ||
| 189 | return; | ||
| 190 | } | ||
| 191 | QString firstItem = reply->readAll(); | ||
| 192 | |||
| 193 | qCDebug(scriptengine) << "first item: " << firstItem; | ||
| 194 | } | ||
| 195 | |||
| 196 | */ | ||