Created
August 6, 2016 16:40
-
-
Save Dirrk/969d2338d500f77c01ae355a87ba885c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/** | |
* Created by Derek Rada on 3/10/2015. | |
*/ | |
var http = require('http'); | |
var qs = require("querystring"); | |
var api_key = 'dc6zaTOxFJmzC'; // default beta key (need to change) | |
var util = require('util'); | |
function GiphyApi(apiKey) { | |
if (apiKey) { | |
this.apiKey = apiKey; | |
} else { | |
this.apiKey = api_key; | |
} | |
this.site = "http://api.giphy.com/v1/gifs" | |
} | |
GiphyApi.prototype.random = function (callback) { | |
var self = this; | |
console.log("Inside Random"); | |
self.api( | |
{ | |
uri: 'random' | |
}, function (err, data) { | |
var message = "An error has occurred"; | |
if (!err && data && data.length >= 1) { | |
var url = data[0].fixed_height_downsampled_url; | |
message = "<" + url + "|Random>"; | |
} | |
callback([message]); | |
} | |
); | |
}; | |
GiphyApi.prototype.search = function (searchStrings, callback) { | |
var self = this; | |
var search_str = searchStrings.join(' '); | |
console.log("Searching for: " + search_str); | |
self.api( | |
{ | |
uri: 'search', | |
query: { | |
q: search_str | |
} | |
}, function (err, data) { | |
if (err) { | |
callback(["An error has occurred"]); | |
} else { | |
console.log(data); | |
// Math.floor((Math.random() * 20)) | |
var url = "Your search for: '" + search_str + "' returned no results"; | |
if (util.isArray(data) && data.length > 0) { | |
try { | |
var num = Math.floor(Math.random() * (data.length - 1)); | |
url = data[num].images.fixed_height_downsampled.url; | |
if (!url || url == "" || url.length == 0) { | |
url = data[num].images.fixed_height.url; | |
} | |
} catch (e) { | |
console.error(e); | |
} | |
} | |
callback(["<" + url + "|" + search_str + ">"]); | |
} | |
} | |
); | |
}; | |
GiphyApi.prototype.api = function (options, callback) { | |
var self = this; | |
console.log("Inside API"); | |
console.log(options); | |
var url = ""; | |
if (!callback && !options) { | |
console.log("Invalid paramaters"); | |
return null; | |
} | |
if (options.uri === null || options.uri === undefined || typeof options !== "object") { | |
url = self.site + '/' + options + "?api_key=" + self.apiKey; | |
} else { | |
if (options.uri.charAt(0) == '/') { | |
url = self.site + options.uri; | |
} else { | |
url = self.site + '/' + options.uri; | |
} | |
if (options.query) { | |
options.query.api_key = self.apiKey; | |
url = url + "?" + qs.stringify(options.query); | |
} else { | |
url = url + "?api_key=" + self.apiKey; | |
} | |
} | |
console.log("Making HTTP Request " + url); | |
var req = http.request(url, giphyRequestHandler); | |
req.end(); | |
req.on('error', function (err) { | |
console.log("Error in GiphyApi Request"); | |
console.error(err); | |
callback(err); | |
}); | |
function giphyRequestHandler(res) { | |
var data = ''; | |
res.setEncoding('utf8'); | |
res.on('data', function (d) { | |
data = data + d.toString(); | |
}); | |
res.on('end', function () { | |
try { | |
var giphyMessage = JSON.parse(data); | |
console.log(data); | |
if (giphyMessage.meta && giphyMessage.meta.status == 200) { | |
if (util.isArray(giphyMessage.data)) { | |
callback(null, giphyMessage.data); | |
} else { | |
callback(null, [giphyMessage.data]) | |
} | |
} else { | |
callback(new Error("Bad request")); | |
} | |
} catch (e) { | |
console.log("Error caught trying to parse data"); | |
console.log(data); | |
console.error(e); | |
callback(err); | |
} | |
}); | |
} | |
}; | |
module.exports = GiphyApi; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment