Skip to content

Add NASA Random Image Search Functionality #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 43 additions & 3 deletions astroBotDiscord.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ const commands = {
}
});
},
'search': (message, argument) => {
if (!argument) return message.channel.send('You need to include a search term.');

getNASAImageSearch(argument).then(data => {
message.channel.send(data.imageUrl);
message.channel.send(`**${data.title}**\n${data.description}`);
}).catch(err => {
message.channel.send(err.message);
});
}
};

NASA.on('message', (message) => {
Expand All @@ -55,9 +65,7 @@ NASA.on('message', (message) => {
const split = message.content.split(' ');
if (dm) split.splice(split.length - 1, 1);
const command = split[1];
let argument;
if (split.length == 3) argument = split[2];
if (split.length > 3) argument = `${split[2]} ${parseInt(split[3].replace(/[a-z]/gi, ''))-1} ${split[4]}`;
let argument = split.slice(2).join(' ');

if (!command) return;
switch (command.toLowerCase()) {
Expand Down Expand Up @@ -87,6 +95,13 @@ function sendHelp(message) {
today: display today's APOD image.
yesterday: display yesterday's APOD image.
{date} displays a specific date's APOD image with {date} in the format: YYYY-MM-DD (Ex: 2016-10-24)

Search: Nasa Random Image Search.
Supply topic and optional keywords to narrow down search results.
**Options:**
Search <topic> <keyword> <etc...>:
Randomly displays a NASA image, title, and description based on search parameters.
Example - "search mars curiosity rover"
`));
}

Expand All @@ -101,6 +116,31 @@ function getAPODImage(date) {
});
}

function getNASAImageSearch(argument) {
return new Promise((resolve, reject) => {
const keywords = argument.split(' ').join(',');
superagent.get(`https://images-api.nasa.gov/search?q=${argument}&media_type=image&keywords=${keywords}`)
.then(res => {
const items = res.body.collection.items;
if (items.length > 0) {
const randomIndex = Math.floor(Math.random() * items.length);
const item = items[randomIndex];
const imageUrl = item.links && item.links.length > 0 ? item.links[0].href : null;
const title = item.data && item.data.length > 0 ? item.data[0].title : "No title available.";
const description = item.data && item.data.length > 0 ? item.data[0].description : "No description available.";

if (imageUrl) {
resolve({ imageUrl, title, description });
}
} else {
reject(new Error('No images found. Be sure keywords are separated by spaces.'));
}
})
.catch(err => {
reject(err);
});
});
}

function formatDate(date) {
let d = new Date(date);
Expand Down
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"token": "TOKEN HERE"
}

}
}