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 | #include <QNetworkAccessManager> | 17 | #include <QNetworkAccessManager> |
| 18 | #include <QNetworkReply> | 18 | #include <QNetworkReply> |
| 19 | #include <QNetworkRequest> | 19 | #include <QNetworkRequest> |
| 20 | //#include <QScopedPointer> | ||
| 21 | #include <QString> | 20 | #include <QString> |
| 22 | #include <QTime> | 21 | #include <QTime> |
| 23 | #include <QUrl> | 22 | #include <QUrl> |
| @@ -26,10 +25,8 @@ · 2 unmodified lines | |||
| 26 | #include "GooglePolyScriptingInterface.h" | 25 | #include "GooglePolyScriptingInterface.h" |
| 27 | #include "ScriptEngineLogging.h" | 26 | #include "ScriptEngineLogging.h" |
| 28 | 27 | ||
| 29 | QString listPolyUrl = "https://poly.googleapis.com/v1/assets?"; | 28 | const QString listPolyUrl = "https://poly.googleapis.com/v1/assets?"; |
| 30 | QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?"; | 29 | const QString getPolyUrl = "https://poly.googleapis.com/v1/assets/model?"; |
| 31 | |||
| 32 | QString authCode = "broke"; | ||
| 33 | 30 | ||
| 34 | QStringList validFormats = QStringList() << "BLOCKS" << "FBX" << "GLTF" << "GLTF2" << "OBJ" << "TILT" << ""; | 31 | QStringList validFormats = QStringList() << "BLOCKS" << "FBX" << "GLTF" << "GLTF2" << "OBJ" << "TILT" << ""; |
| 35 | QStringList validCategories = QStringList() << "animals" << "architecture" << "art" << "food" << | 32 | QStringList validCategories = QStringList() << "animals" << "architecture" << "art" << "food" << |
| @@ -42,8 +39,7 @@ GooglePolyScriptingInterface::GooglePolyScriptingInterface() { · 6 unmodified lines | |||
| 42 | QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) { | 39 | QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString category, QString format) { |
| 43 | QUrl url = formatURLQuery(keyword, category, format); | 40 | QUrl url = formatURLQuery(keyword, category, format); |
| 44 | if (!url.isEmpty()) { | 41 | if (!url.isEmpty()) { |
| 45 | QByteArray jsonString = getHTTPRequest(url); | 42 | QJsonArray json = parseJSON(url, 0).toJsonArray(); |
| 46 | QJsonArray json = parseJSON(&jsonString, 0).toJsonArray(); | ||
| 47 | qCDebug(scriptengine) << "first asset name: " << json.at(0).toObject().value("displayName"); | 43 | qCDebug(scriptengine) << "first asset name: " << json.at(0).toObject().value("displayName"); |
| 48 | return json; | 44 | return json; |
| 49 | } else { | 45 | } else { |
| @@ -54,19 +50,40 @@ QJsonArray GooglePolyScriptingInterface::getAssetList(QString keyword, QString c · 4 unmodified lines | |||
| 54 | 50 | ||
| 55 | QString GooglePolyScriptingInterface::getFBX(QString keyword, QString category) { | 51 | QString GooglePolyScriptingInterface::getFBX(QString keyword, QString category) { |
| 56 | QUrl url = formatURLQuery(keyword, category, "FBX"); | 52 | QUrl url = formatURLQuery(keyword, category, "FBX"); |
| 57 | qCDebug(scriptengine) << "google url: " << url; | 53 | return getModelURL(url); |
| 58 | if (!url.isEmpty()) { | 54 | } |
| 59 | QByteArray jsonString = getHTTPRequest(url); | 55 | |
| 60 | qCDebug(scriptengine) << "the list: " << jsonString; | 56 | QString GooglePolyScriptingInterface::getOBJ(QString keyword, QString category) { |
| 61 | QString modelURL = parseJSON(&jsonString, 1).toString(); | 57 | QUrl url = formatURLQuery(keyword, category, "OBJ"); |
| 62 | 58 | return getModelURL(url); | |
| 63 | qCDebug(scriptengine) << "the model url: " << modelURL; | 59 | } |
| 64 | return modelURL; | 60 | |
| 65 | } else { | 61 | QString GooglePolyScriptingInterface::getBlocks(QString keyword, QString category) { |
| 66 | qCDebug(scriptengine) << "Invalid filters were specified."; | 62 | QUrl url = formatURLQuery(keyword, category, "BLOCKS"); |
| 67 | return ""; | 63 | qCDebug(scriptengine) << "Google url request: " << url; |
| 68 | } | 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 | void GooglePolyScriptingInterface::testPrint(QString input) { | 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 | // FIXME: synchronous | 133 | // FIXME: synchronous |
| 107 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { | 134 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { |
| 108 | QNetworkAccessManager manager; | 135 | QNetworkAccessManager manager; |
| @@ -115,26 +142,23 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 6 unmodified lines | |||
| 115 | } | 142 | } |
| 116 | 143 | ||
| 117 | // since the list is a QJsonArray and a single model is a QJsonObject | 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 | //QString firstItem = QString::fromUtf8(response->readAll()); | 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 | QJsonObject obj = doc.object(); | 149 | QJsonObject obj = doc.object(); |
| 122 | qCDebug(scriptengine) << "json obj: " << obj; | ||
| 123 | qCDebug(scriptengine) << "json list: " << obj.keys(); | ||
| 124 | if (obj.keys().first() == "error") { | 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 | return QJsonObject(); | 153 | return QJsonObject(); |
| 127 | } | 154 | } |
| 128 | qCDebug(scriptengine) << "the assets: " << obj.value("assets"); | 155 | qCDebug(scriptengine) << "the assets: " << obj.value("assets"); |
| 129 | if (fileType == 0 || fileType == 1) { | 156 | if (fileType == 0 || fileType == 1) { |
| 130 | QJsonArray arr = obj.value("assets").toArray(); | 157 | QJsonArray arr = obj.value("assets").toArray(); |
| 131 | qCDebug(scriptengine) << "in array: " << arr; | ||
| 132 | // return model url | 158 | // return model url |
| 133 | if (fileType == 1) { | 159 | if (fileType == 1) { |
| 134 | int random = getRandIntInRange(arr.size()); | 160 | int random = getRandIntInRange(arr.size()); |
| 135 | QJsonObject json = arr.at(random).toObject(); | 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 | // nested JSONs | 162 | // nested JSONs |
| 139 | return json.value("formats").toArray().at(0).toObject().value("root").toObject().value("url"); | 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 | return arr; | 166 | return arr; |
| 143 | // return specific object | 167 | // return specific object |
| 144 | } else { | 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() { | 176 | /* |
| 151 | authCode = "AIzaSyDamk7Vth52j7aU9JVKn3ungFS0kGJYc8A"; | ||
| 152 | QUrl url(listPolyUrl + "key=" + authCode); | ||
| 153 | QByteArray reply = getHTTPRequest(url); | ||
| 154 | qCDebug(scriptengine) << "the list: " << test; | ||
| 155 | } | ||
| 156 | 177 | ||
| 157 | // FIXME: synchronous = not good code | 178 | // FIXME: synchronous = not good code |
| 158 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { | 179 | QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { |
| @@ -164,23 +185,7 @@ QByteArray GooglePolyScriptingInterface::getHTTPRequest(QUrl url) { · 5 unmodified lines | |||
| 164 | return response->readAll(); | 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 | /* NONE OF THESE HTTP GET METHODS WORK D: | 190 | /* NONE OF THESE HTTP GET METHODS WORK D: |
| 186 | https://stackoverflow.com/questions/28267477/getting-a-page-content-with-qt kinda used rn | 191 | https://stackoverflow.com/questions/28267477/getting-a-page-content-with-qt kinda used rn |