Skip to content
This repository has been archived by the owner on Feb 1, 2021. It is now read-only.

Commit

Permalink
reworked Tautulli commands to use new API handler, moved resources to…
Browse files Browse the repository at this point in the history
… resources folder, renamed refreshlibrary command to refreshlibraries
  • Loading branch information
v0idp committed Jan 21, 2020
1 parent 3be7652 commit 91a243a
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Mellow [![Discord](https://img.shields.io/badge/Discord-Invite-7289DA.svg?style=flat-square)](https://discord.gg/zx2BWp2) [![Docker](https://img.shields.io/badge/Docker-Hub-lightblue.svg?style=flat-square)](https://cloud.docker.com/u/voidp/repository/docker/voidp/mellow) [![Run on Repl.it](https://repl.it/badge/github/v0idp/Mellow)](https://repl.it/github/v0idp/Mellow)

<p align="center">
<img src="logo.png" width="256" height="256" link="https://discord.gg/zx2BWp2">
<img src="resources/logo.png" width="256" height="256" link="https://discord.gg/zx2BWp2">
</p>
<p align="center">
Mellow can communicate with several APIs like Ombi, Sonarr, Radarr and Tautulli which are related to home streaming to use those services directly in your Discord client.
Expand Down
8 changes: 4 additions & 4 deletions src/api_handlers/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ const Tautulli = require('./tautulli.js');

class APIHandler {
constructor (config) {
this.ombi = new Ombi(config);
this.radarr = new Radarr(config);
this.sonarr = new Sonarr(config);
this.tautulli = new Tautulli(config);
this.ombi = new Ombi(config.ombi);
this.radarr = new Radarr(config.radarr);
this.sonarr = new Sonarr(config.sonarr);
this.tautulli = new Tautulli(config.tautulli);
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/api_handlers/tautulli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,43 @@
const { get, getURL } = require('./../util.js');

class Tautulli {
constructor(config) {
this.config = config;
this.endpoints = {
"libraries": getURL(config.host, config.port, config.ssl, config.baseurl + '/api/v2?apikey=' + config.apikey + '&cmd=get_libraries'),
"refresh": getURL(config.host, config.port, config.ssl, config.baseurl + '/api/v2?apikey=' + config.apikey + '&cmd=refresh_libraries_list')
};
}

getLibraries() {
return new Promise((resolve, reject) => {
get({
headers: {'accept' : 'application/json',
'User-Agent': `Mellow/${process.env.npm_package_version}`},
url: this.endpoints["libraries"]
}).then((response) => {
let jsonResponse = JSON.parse(response.body);
resolve(jsonResponse);
}).catch((err) => {
console.log(err);
reject();
});
});
}

refreshLibraries() {
return new Promise((resolve, reject) => {
get({
headers: {'accept' : 'application/json',
'User-Agent': `Mellow/${process.env.npm_package_version}`},
url: this.endpoints["refresh"]
}).then(() => {
resolve();
}).catch((err) => {
console.log(err);
reject();
});
});
}
}

Expand Down
28 changes: 12 additions & 16 deletions src/commands/tautulli/libraries.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Discord = require('discord.js');
const commando = require('discord.js-commando');
const {deleteCommandMessages, get, getURL} = require('../../util.js');
const path = require('path');
const { deleteCommandMessages } = require('../../util.js');

module.exports = class librariesCommand extends commando.Command {
constructor (client) {
Expand All @@ -15,30 +16,25 @@ module.exports = class librariesCommand extends commando.Command {
}

run (msg) {
const tautulli = this.client.webDatabase.webConfig.tautulli;
get({
headers: {'accept' : 'application/json',
'User-Agent': `Mellow/${process.env.npm_package_version}`},
url: getURL(tautulli.host, tautulli.port, tautulli.ssl, tautulli.baseurl + '/api/v2?apikey=' + tautulli.apikey + '&cmd=get_libraries')
}).then((resolve) => {
let jsonObject = JSON.parse(resolve.body);
this.client.API.tautulli.getLibraries().then((jsonResponse) => {
let libraryEmbed = new Discord.MessageEmbed()
.setTitle('Server Libraries')
.setTimestamp(new Date())
.setThumbnail('https://i.imgur.com/pz9PoqR.png');
for (let i = 0; i < Object.keys(jsonObject.response.data).length; i++) {
let obj = jsonObject.response.data[i];
.attachFiles(path.join(__dirname, '..', '..', 'resources', 'libraries.png'))
.setThumbnail('attachment://libraries.png');
for (let i = 0; i < Object.keys(jsonResponse.response.data).length; i++) {
let obj = jsonResponse.response.data[i];
if (obj.section_type == 'movie') {
libraryEmbed.addField(obj.section_name, obj.count, true);
} else if (obj.section_type == 'show') {
libraryEmbed.addField(obj.section_name, `${obj.count} Shows\n${obj.parent_count} Seasons\n${obj.child_count} Episodes`, true);
}
}
deleteCommandMessages(msg, this.client);
msg.embed(libraryEmbed);
}).catch((error) => {
console.error(error);
return msg.reply('There was an error in your request.');
deleteCommandMessages(msg);
return msg.embed(libraryEmbed);
}).catch(() => {
deleteCommandMessages(msg);
return msg.reply('Couldn\'t get libraries! Something went wrong.');
});
}
};
24 changes: 24 additions & 0 deletions src/commands/tautulli/refreshlibraries.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const commando = require('discord.js-commando');
const {deleteCommandMessages, get, getURL} = require('../../util.js');

module.exports = class refreshLibrariesCommand extends commando.Command {
constructor (client) {
super(client, {
'name': 'refreshlibraries',
'memberName': 'refreshlibraries',
'group': 'tautulli',
'description': 'refresh all libraries in tautulli',
'examples': ['refreshlibraries'],
'guildOnly': true
});
}

run (msg) {
this.client.API.tautulli.refreshLibraries().then(() => {
deleteCommandMessages(msg);
return msg.reply('Refreshed all libraries in Tautulli.');
}).catch(() => {
return msg.reply('Couldn\'t refresh libraries! Something went wrong.');
});
}
};
30 changes: 0 additions & 30 deletions src/commands/tautulli/refreshlibrary.js

This file was deleted.

4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const start = function () {
if (botConfig && botConfig.token) {
bot = new BotClient(webDatabase, botConfig.ownerid, botConfig.commandprefix);
bot.init().catch((err) => {
console.log('Failed initializing DiscordBot! Please check your bot settings.');
console.log('Failed initializing DiscordBot! Please check your bot configurations.');
console.error(err);
});
} else console.log('There is no bot token provided. Please check your settings!');
} else console.log('There is no bot token provided. Please check your configurations.');
new WebServer(webDatabase, bot).init();
}).catch(console.error);
}
Expand Down
Binary file added src/resources/libraries.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes

0 comments on commit 91a243a

Please sign in to comment.