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

Commit

Permalink
Merge pull request #76 from v0idp/api_rework
Browse files Browse the repository at this point in the history
Api rework
  • Loading branch information
v0idp authored Jan 22, 2020
2 parents 9f67d21 + f95d0d0 commit 4f4df71
Show file tree
Hide file tree
Showing 19 changed files with 425 additions and 321 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="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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/BotClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Commando = require('discord.js-commando');
const path = require('path');
const sqlite = require('sqlite');
const fs = require('fs');
const APIHandler = require('./api_handlers/api.js');

class BotClient extends Commando.Client {
constructor (webDatabase, ownerid, commandprefix) {
Expand All @@ -10,6 +11,7 @@ class BotClient extends Commando.Client {
"commandPrefix": (commandprefix !== '') ? commandprefix : '$'
});
this.webDatabase = webDatabase;
this.API = new APIHandler(webDatabase.getConfig());
this.isReady = false;
}

Expand Down Expand Up @@ -72,7 +74,7 @@ class BotClient extends Commando.Client {
}
return (message.channel.name.toLowerCase() !== bot.channelname.toLowerCase()) ? 'Not allowed in this channel' : false;
});

// login client with bot token
this.login(this.webDatabase.webConfig.bot.token)
.then((token) => resolve(token))
Expand Down
15 changes: 15 additions & 0 deletions src/api_handlers/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const Ombi = require('./ombi.js');
const Radarr = require('./radarr.js');
const Sonarr = require('./sonarr.js');
const Tautulli = require('./tautulli.js');

class APIHandler {
constructor (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);
}
}

module.exports = APIHandler;
102 changes: 102 additions & 0 deletions src/api_handlers/ombi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const { get, post, getURL, replacePlaceholders } = require('./../util.js');

module.exports = class Ombi {
constructor (config) {
this.config = config;
this.endpoints = {
"searchContent" : getURL(config.host, config.port, config.ssl, config.baseurl + '/api/v1/Search/%TYPE%/%NAME%'),
"getContentInformation" : getURL(config.host, config.port, config.ssl, config.baseurl + '/api/v1/Search/%TYPE%/info/%DBID%'),
"requestContent" : getURL(config.host, config.port, config.ssl, config.baseurl + '/api/v1/Request/%TYPE%/')
};
}

searchContent(type, name) {
return new Promise((resolve, reject) => {
get({
headers: {'accept' : 'application/json',
'ApiKey': this.config.apikey,
'User-Agent': `Mellow/${process.env.npm_package_version}`},
url: replacePlaceholders(this.endpoints['searchContent'], { "%TYPE%":type, "%NAME%":name })
}).then(({response, body}) => {
if (response.statusCode === 200) {
const data = JSON.parse(body);
resolve(data);
}
else {
console.log(response);
reject()
}
}).catch((err) => {
console.log(err);
reject();
});
});
}

getContentInformation(type, dbid) {
return new Promise((resolve, reject) => {
get({
headers: {'accept' : 'application/json',
'ApiKey': this.config.apikey,
'User-Agent': `Mellow/${process.env.npm_package_version}`},
url: replacePlaceholders(this.endpoints['getContentInformation'], { "%TYPE%":type, "%DBID%":dbid })
}).then(({response, body}) => {
if (response.statusCode === 200) {
const data = JSON.parse(body);
resolve(data);
}
else {
console.log(response);
reject()
}
}).catch((err) => {
console.log(err);
reject();
});
});
}

requestContent(type, body, name) {
return new Promise((resolve, reject) => {
post({
headers: {'accept' : 'application/json',
'Content-Type' : 'application/json',
'ApiKey': this.config.apikey,
'ApiAlias' : name,
'UserName' : this.config.username ? this.config.username : '',
'User-Agent': `Mellow/${process.env.npm_package_version}`},
url: replacePlaceholders(this.endpoints['requestContent'], { "%TYPE%":type }),
body: JSON.stringify(body)
}).then(() => {
resolve();
}).catch((err) => {
console.log(err);
reject();
});
});
}

searchMovie(name) {
return this.searchContent('movie', name);
}

getMovieInformation(tmdbid) {
return this.getContentInformation('movie', tmdbid);
}

requestMovie(tmdbid, name) {
return this.requestContent('movie', { 'theMovieDbId': tmdbid }, name);
}

searchTVShow(name) {
return this.searchContent('tv', name);
}

getTVShowInformation(tvdbid) {
return this.getContentInformation('tv', tvdbid);
}

requestTVShow(tvdbid, name) {
return this.requestContent('tv', { 'tvDbId': tvdbid, "requestAll": true}, name);
}
}
5 changes: 5 additions & 0 deletions src/api_handlers/radarr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = class Radarr {
constructor(config) {
this.config = config;
}
}
5 changes: 5 additions & 0 deletions src/api_handlers/sonarr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = class Sonarr {
constructor(config) {
this.config = config;
}
}
42 changes: 42 additions & 0 deletions src/api_handlers/tautulli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const { get, getURL } = require('./../util.js');

module.exports = 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) => {
const 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();
});
});
}
}
Loading

0 comments on commit 4f4df71

Please sign in to comment.