Change to xml2js

This commit is contained in:
Daniel Odendahl Jr
2017-10-28 19:03:49 +00:00
parent c3875c8f80
commit 7a83778178
5 changed files with 42 additions and 32 deletions
+7 -4
View File
@@ -1,6 +1,8 @@
const { Command } = require('discord.js-commando');
const snekfetch = require('snekfetch');
const { xml2js } = require('xml-js');
const { parseString } = require('xml2js');
const { promisify } = require('util');
const xml = promisify(parseString);
module.exports = class GelbooruCommand extends Command {
constructor(client) {
@@ -31,9 +33,10 @@ module.exports = class GelbooruCommand extends Command {
q: 'index',
tags: query
});
const parsed = xml2js(text, { compact: true }).posts;
if (!parsed.post || !parsed.post.length) return msg.say('Could not find any results.');
return msg.say(parsed.post[Math.floor(Math.random() * parsed.post.length)]._attributes.file_url);
const body = await xml(text);
const data = body.posts.post;
if (!data || !data.length) return msg.say('Could not find any results.');
return msg.say(data[Math.floor(Math.random() * data.length)].$.file_url);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
+13 -11
View File
@@ -1,7 +1,9 @@
const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { xml2js } = require('xml-js');
const { parseString } = require('xml2js');
const { promisify } = require('util');
const xml = promisify(parseString);
const { shorten, cleanXML } = require('../../util/Util');
const { MAL_LOGIN } = process.env;
@@ -29,23 +31,23 @@ module.exports = class MyAnimeListAnimeCommand extends Command {
const { text } = await snekfetch
.get(`https://${MAL_LOGIN}@myanimelist.net/api/anime/search.xml`)
.query({ q: query });
const body = xml2js(text, { compact: true }).anime;
const data = body.entry.length ? body.entry[0] : body.entry;
const body = await xml(text);
const data = body.anime.entry[0];
const embed = new MessageEmbed()
.setColor(0x2D54A2)
.setAuthor('My Anime List', 'https://i.imgur.com/5rivpMM.png')
.setURL(`https://myanimelist.net/anime/${data.id._text}`)
.setThumbnail(data.image._text)
.setTitle(data.title._text)
.setDescription(shorten(cleanXML(data.synopsis._text)))
.setURL(`https://myanimelist.net/anime/${data.id[0]}`)
.setThumbnail(data.image[0])
.setTitle(data.title[0])
.setDescription(shorten(cleanXML(data.synopsis[0])))
.addField(' Type',
`${data.type._text} - ${data.status._text}`, true)
`${data.type[0]} - ${data.status[0]}`, true)
.addField(' Episodes',
data.episodes._text, true)
data.episodes[0], true)
.addField(' Start Date',
data.start_date._text !== '0000-00-00' ? data.start_date._text : '???', true)
data.start_date[0] !== '0000-00-00' ? data.start_date[0] : '???', true)
.addField(' End Date',
data.end_date._text !== '0000-00-00' ? data.end_date._text : '???', true);
data.end_date[0] !== '0000-00-00' ? data.end_date[0] : '???', true);
return msg.embed(embed);
} catch (err) {
if (err.message === 'Parse Error') return msg.say('Could not find any results.');
+13 -11
View File
@@ -1,7 +1,9 @@
const { Command } = require('discord.js-commando');
const { MessageEmbed } = require('discord.js');
const snekfetch = require('snekfetch');
const { xml2js } = require('xml-js');
const { parseString } = require('xml2js');
const { promisify } = require('util');
const xml = promisify(parseString);
const { shorten, cleanXML } = require('../../util/Util');
const { MAL_LOGIN } = process.env;
@@ -29,23 +31,23 @@ module.exports = class MyAnimeListMangaCommand extends Command {
const { text } = await snekfetch
.get(`https://${MAL_LOGIN}@myanimelist.net/api/manga/search.xml`)
.query({ q: query });
const body = xml2js(text, { compact: true }).manga;
const data = body.entry.length ? body.entry[0] : body.entry;
const body = await xml(text);
const data = body.manga.entry[0];
const embed = new MessageEmbed()
.setColor(0x2D54A2)
.setAuthor('My Anime List', 'https://i.imgur.com/5rivpMM.png')
.setURL(`https://myanimelist.net/manga/${data.id._text}`)
.setThumbnail(data.image._text)
.setTitle(data.title._text)
.setDescription(shorten(cleanXML(data.synopsis._text)))
.setURL(`https://myanimelist.net/manga/${data.id[0]}`)
.setThumbnail(data.image[0])
.setTitle(data.title[0])
.setDescription(shorten(cleanXML(data.synopsis[0])))
.addField(' Type',
`${data.type._text} - ${data.status._text}`, true)
`${data.type[0]} - ${data.status[0]}`, true)
.addField(' Volumes / Chapters',
`${parseInt(data.volumes._text, 10) || '???'} / ${parseInt(data.chapters._text, 10) || '???'}`, true)
`${parseInt(data.volumes[0], 10) || '???'} / ${parseInt(data.chapters[0], 10) || '???'}`, true)
.addField(' Start Date',
data.start_date._text !== '0000-00-00' ? data.start_date._text : '???', true)
data.start_date[0] !== '0000-00-00' ? data.start_date[0] : '???', true)
.addField(' End Date',
data.end_date._text !== '0000-00-00' ? data.end_date._text : '???', true);
data.end_date[0] !== '0000-00-00' ? data.end_date[0] : '???', true);
return msg.embed(embed);
} catch (err) {
if (err.message === 'Parse Error') return msg.say('Could not find any results.');
+7 -4
View File
@@ -1,6 +1,8 @@
const { Command } = require('discord.js-commando');
const snekfetch = require('snekfetch');
const { xml2js } = require('xml-js');
const { parseString } = require('xml2js');
const { promisify } = require('util');
const xml = promisify(parseString);
module.exports = class SafebooruCommand extends Command {
constructor(client) {
@@ -30,9 +32,10 @@ module.exports = class SafebooruCommand extends Command {
q: 'index',
tags: query
});
const parsed = xml2js(text, { compact: true }).posts;
if (!parsed.post || !parsed.post.length) return msg.say('Could not find any results.');
return msg.say(`https:${parsed.post[Math.floor(Math.random() * parsed.post.length)]._attributes.file_url}`);
const body = await xml(text);
const data = body.posts.post;
if (!data || !data.length) return msg.say('Could not find any results.');
return msg.say(`https:${data[Math.floor(Math.random() * data.length)].$.file_url}`);
} catch (err) {
return msg.reply(`Oh no, an error occurred: \`${err.message}\`. Try again later!`);
}
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "xiaobot",
"version": "51.0.0",
"version": "51.0.1",
"description": "Your personal server companion.",
"main": "XiaoBot.js",
"scripts": {
@@ -40,7 +40,7 @@
"node-opus": "^0.2.7",
"snekfetch": "^3.5.7",
"uws": "^8.14.1",
"xml-js": "^1.5.1",
"xml2js": "^0.4.19",
"zlib-sync": "^0.1.3"
},
"devDependencies": {