From a5821aceb4a1e865559f131d9ce1140feea2a636 Mon Sep 17 00:00:00 2001 From: LaneLR Date: Mon, 12 May 2025 09:55:33 -0500 Subject: [PATCH 1/2] initial commit --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index 5d6a968..f939682 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ require('dotenv').config(); +const giphy = 'https://api.giphy.com/v1/gifs/search?api_key=gfMs7vGi9QVQkckQYuOcXeOqMxU3hFbv&q=spongebob&limit=25&offset=0&rating=g&lang=en&bundle=messaging_non_clips' // Print out value of API key stored in .env file console.log(process.env.API_KEY) \ No newline at end of file From eeede1d5831e588b7f8dbecff2a056e601b511c2 Mon Sep 17 00:00:00 2001 From: LaneLR Date: Mon, 12 May 2025 11:19:37 -0500 Subject: [PATCH 2/2] extension 1 --- index.js | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f939682..7abb0bf 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,27 @@ -require('dotenv').config(); -const giphy = 'https://api.giphy.com/v1/gifs/search?api_key=gfMs7vGi9QVQkckQYuOcXeOqMxU3hFbv&q=spongebob&limit=25&offset=0&rating=g&lang=en&bundle=messaging_non_clips' +require("dotenv").config(); // Print out value of API key stored in .env file -console.log(process.env.API_KEY) \ No newline at end of file +async function getImage(query) { + try { + const response = await fetch( + `https://api.giphy.com/v1/gifs/search?api_key=gfMs7vGi9QVQkckQYuOcXeOqMxU3hFbv&q=${query}&limit=25&offset=0&rating=g&lang=en&bundle=messaging_non_clips` + ); + const data = await response.json(); + + const randomNum = Math.floor(Math.random() * 26); //pick random number between 0 and 25 + + if (data.data[randomNum]) { + //if the object at index randomNum exists + const url = data.data[randomNum].images.original.url; + console.log(url); + return url; + } else { + console.warn("This data does not exist."); + return null; + } + } catch (err) { + console.error("Ran into an error: ", err); + } +} + +getImage("spongebob");