Created
August 2, 2016 18:06
-
-
Save sonerb/2de26fed6b0695a141633f5c598e0360 to your computer and use it in GitHub Desktop.
giphy gifs lister
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var saveData = (function() { | |
var a = document.createElement("a"); | |
document.body.appendChild(a); | |
a.style = "display: none"; | |
return function(data, fileName) { | |
blob = new Blob([data], { | |
type: "text/plain;charset=utf-8" | |
}), | |
url = window.URL.createObjectURL(blob); | |
a.href = url; | |
a.download = fileName; | |
a.click(); | |
window.URL.revokeObjectURL(url); | |
}; | |
}()); | |
function reqListener() { | |
var myArr = JSON.parse(this.responseText); | |
var liste = ""; | |
for (var i = 0; i < myArr.data.gifs.length; i++) { | |
var gif = myArr.data.gifs[i]; | |
liste += gif.images.original.url + "\r\n"; | |
} | |
saveData(liste, this.mydata.category+"_"+this.mydata.page+".txt"); | |
} | |
function downloadGif(category, page) { | |
var oReq = new XMLHttpRequest(); | |
oReq.mydata = {category: category, page: page}; | |
oReq.addEventListener("load", reqListener); | |
oReq.open("GET", "http://giphy.com/search/"+category+"/2?is="+page+"&json=true"); | |
oReq.setRequestHeader("Authorization", "ApiKey giphy-website:e0771ed7b244ec9c942bea646ad08e6bf514f51a"); | |
oReq.setRequestHeader("X-NewRelic-ID", "VwQBUlZWGwEAVlJSAwI="); | |
oReq.setRequestHeader("X-Requested-With", "XMLHttpRequest"); | |
oReq.send(); | |
} | |
/* | |
Example Usage: | |
downloadGif({category-search}, {page}); | |
downloadGif('adventure-time', 1); | |
downloadGif('adventure-time', 2); | |
downloadGif('8bit', 1); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment