From d03caa0246651d3dd4b611a18026d65bffcda3f1 Mon Sep 17 00:00:00 2001 From: Wolfgang Rumpler Date: Tue, 16 Jan 2018 18:51:23 +0100 Subject: [PATCH] update unsplash source adapter to correspond to new unsplash API guidelines --- randomwallpaper@iflow.space/sourceAdapter.js | 33 +++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/randomwallpaper@iflow.space/sourceAdapter.js b/randomwallpaper@iflow.space/sourceAdapter.js index 6b80657..ad80e7a 100644 --- a/randomwallpaper@iflow.space/sourceAdapter.js +++ b/randomwallpaper@iflow.space/sourceAdapter.js @@ -37,7 +37,7 @@ let BaseAdapter = new Lang.Class({ fileName: function (uri) { let base = decodeURIComponent(uri); base = base.substring(base.lastIndexOf('/') + 1); - if(base.indexOf('?') >= 0) { + if (base.indexOf('?') >= 0) { base = base.substr(0, base.indexOf('?')); } return base; @@ -114,27 +114,36 @@ let UnsplashAdapter = new Lang.Class({ this._readOptionsFromSettings(); let optionsString = this._generateOptionsString(); - let url = 'https://api.unsplash.com/photos/random?' + optionsString; - url += 'client_id=64daf439e9b579dd566620c0b07022706522d87b255d06dd01d5470b7f193b8d'; + let clientParam = 'client_id=2e4e49adee7dcaf75515d383086909e1ed5f0b08db60becf8ec58bc63cbc418a'; + + let url = 'https://api.unsplash.com/photos/random?' + optionsString + clientParam; url = encodeURI(url); let message = Soup.Message.new('GET', url); - let utmParameters = '?utm_source=RandomWallpaperGnome3&utm_medium=referral&utm_campaign=api-credit'; + let utmParameters = 'utm_source=RandomWallpaperGnome3&utm_medium=referral&utm_campaign=api-credit'; session.queue_message(message, (session, message) => { let data = JSON.parse(message.response_body.data); - let imageUrl = encodeURI(data.links.download + utmParameters); let authorName = data.user.name; let authorUrl = encodeURI(data.user.links.html); - if (callback) { - let historyEntry = new HistoryModule.HistoryEntry(authorName, this.sourceName, encodeURI(imageUrl)); - historyEntry.source.sourceUrl = encodeURI(this.sourceUrl + utmParameters); - historyEntry.source.authorUrl = encodeURI(authorUrl + utmParameters); - callback(historyEntry); - } + let downloadLocation = data.links.download_location + '?' + clientParam; + let downloadMessage = Soup.Message.new('GET', downloadLocation); + + session.queue_message(downloadMessage, (session, message) => { + let downloadData = JSON.parse(message.response_body.data); + + let imageUrl = encodeURI(downloadData.url + '&' + utmParameters); + + if (callback) { + let historyEntry = new HistoryModule.HistoryEntry(authorName, this.sourceName, encodeURI(imageUrl)); + historyEntry.source.sourceUrl = encodeURI(this.sourceUrl + '?' + utmParameters); + historyEntry.source.authorUrl = encodeURI(authorUrl + '?' + utmParameters); + callback(historyEntry); + } + }); }); }, @@ -296,7 +305,7 @@ let GenericJsonAdapter = new Lang.Class({ let response = JSON.parse(message.response_body.data); let JSONPath = this._settings.get("generic-json-response-path", "string"); let imageUrl = this._jsonPathParser.access(response, JSONPath); - imageUrl = this._settings.get("generic-json-url-prefix", "string")+imageUrl; + imageUrl = this._settings.get("generic-json-url-prefix", "string") + imageUrl; if (callback) { let historyEntry = new HistoryModule.HistoryEntry(null, 'Generic JSON Source', imageUrl);