diff --git a/src/apis/Itunes.js b/src/apis/Itunes.js new file mode 100644 index 000000000..2b50d35ce --- /dev/null +++ b/src/apis/Itunes.js @@ -0,0 +1,25 @@ +const { APIWrapper } = require('..') +const axios = require('axios') + +module.exports = class ITunes extends APIWrapper { + constructor () { + super({ name: 'itunes' }) + } + + async search (media, term, country) { + try { + const { data } = await axios.get('https://itunes.apple.com/search', { + params: { + media, + term, + country, + limit: 10 + } + }) + + return [data.results, true] + } catch { + return [[], false] + } + } +} diff --git a/src/commands/misc/itunes.js b/src/commands/misc/itunes.js new file mode 100644 index 000000000..0fc63eadf --- /dev/null +++ b/src/commands/misc/itunes.js @@ -0,0 +1,69 @@ +const { SwitchbladeEmbed, Command, Constants, CommandError } = require('../../') + +const MEDIA_WHITE_LIST = ['movie', 'podcast', 'music', 'musicVideo', 'audiobook', 'shortFilm', 'tvShow', 'software', 'ebook', 'all'] + +module.exports = class Itunes extends Command { + constructor (client) { + super({ + name: 'itunes', + requirements: { apis: ['itunes'] }, + embedLogoURL: 'https://i.imgur.com/U4jjk5F.png', + parameters: [{ + type: 'string', + full: false, + name: 'media', + whitelist: MEDIA_WHITE_LIST, + missingError: 'commands:itunes.notFound', + required: true + }, { + type: 'string', + name: 'country', + required: true, + missingError: 'commands:itunes.notFound' + }, { + type: 'string', + full: true, + name: 'term', + required: true, + missingError: 'commands:itunes.notFound' + }] + }, + client) + } + + async run ({ t, author, message, channel }, media, country, term) { + term = term.replaceAll(' ', '+') + + const [data, response] = await this.client.apis.itunes.search(media, term, country) + + if (!response) { + throw new CommandError(t('commands:itunes.invalidTerm')) + } + + if (data.length === 0) { + throw new CommandError(t('commands:itunes.noResults')) + } + + channel.send(this.parseResponse(author, t, data, message.content)) + } + + searchResultFormatter (i) { + return `[${i.trackName}](${i.trackViewUrl}) - [${i.artistName}](${i.artistViewUrl})` + } + + getRatingEmojis (rating) { + return (this.getEmoji('ratingstar', '⭐').repeat(Math.floor(rating))) + + (this.getEmoji('ratinghalfstar') + .repeat(Math.ceil(rating - Math.floor(rating)))) + } + + parseResponse (author, t, data, title) { + const description = data.map((item, index) => `\`${String(index).padStart(2, '0')}\`: ${this.searchResultFormatter(item)}`) + + return new SwitchbladeEmbed(author) + .setThumbnail(this.embedUrl) + .setDescription(description) + .setColor(Constants.ITUNES_COLOR) + .setTitle(t('commands:itunes.title', { title })) + } +} diff --git a/src/locales/en-US/commands.json b/src/locales/en-US/commands.json index b2f1e50f2..45be36715 100644 --- a/src/locales/en-US/commands.json +++ b/src/locales/en-US/commands.json @@ -672,6 +672,14 @@ "commandUsage": "", "noText": "You have to give me some text to asciify!" }, + "itunes": { + "commandDescription": "Provides the content within the iTunes Store and Apple Books Store.", + "title": "Showing results for `{{title}}`", + "commandUsage": " ", + "notFound": "I wasn't able to find the term with the given media.", + "noResults": "No results found.", + "invalidTerm": "Invalid term! Make sure that you are giving correct term." + }, "lorem": { "commandDescription": "Generates a random lorem ipsum text with the specified amount of paragraphs.", "commandUsage": "", diff --git a/src/utils/Constants.js b/src/utils/Constants.js index adcd28e06..69e0dd535 100644 --- a/src/utils/Constants.js +++ b/src/utils/Constants.js @@ -6,6 +6,7 @@ module.exports = { ERROR_COLOR: '#FF3333', EIGHTBALL_COLOR: '#000000', NPM_COLOR: '#CB3837', + ITUNES_COLOR: '#FF6283', GENERIC_RED_COLOR: '#CB3837', E621_COLOR: '#258CF5', XKCD_COLOR: '#96A8C8',