mirror of
https://github.com/mjrode/WhatToWatch.git
synced 2026-01-07 22:19:50 -06:00
Add Sonarr API
This commit is contained in:
@@ -62,6 +62,7 @@
|
||||
"passport-local": "^1.0.0",
|
||||
"pg": "^7.8.0",
|
||||
"plex-api": "^5.2.1",
|
||||
"request": "^2.88.0",
|
||||
"request-promise": "^4.2.4",
|
||||
"sequelize": "^4.42.0",
|
||||
"sequelize-cli": "^5.4.0",
|
||||
|
||||
9
server/controllers/sonarr.controller.js
Normal file
9
server/controllers/sonarr.controller.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import {Router} from 'express';
|
||||
import sonarrService from '../services/sonarr';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get('/search', sonarrService.search);
|
||||
router.get('/add-show', sonarrService.addShow);
|
||||
|
||||
export default router;
|
||||
@@ -26,6 +26,12 @@ module.exports = {
|
||||
plexToken: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
sonarrUrl: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
sonarrApiKey: {
|
||||
type: Sequelize.STRING,
|
||||
},
|
||||
createdAt: {
|
||||
allowNull: false,
|
||||
type: Sequelize.DATE,
|
||||
|
||||
@@ -8,6 +8,8 @@ module.exports = (sequelize, DataTypes) => {
|
||||
email: {type: DataTypes.STRING, unique: true},
|
||||
plexUrl: DataTypes.STRING,
|
||||
plexToken: DataTypes.STRING,
|
||||
sonarrUrl: DataTypes.STRING,
|
||||
sonarrApiKey: DataTypes.STRING,
|
||||
},
|
||||
{},
|
||||
);
|
||||
|
||||
@@ -8,6 +8,7 @@ import keys from '../config';
|
||||
import plex from './routes/plex.route';
|
||||
import tdaw from './routes/tdaw.route';
|
||||
import movieDb from './routes/movieDb.route';
|
||||
import sonarr from './routes/sonarr.route';
|
||||
import auth from './routes/auth.route';
|
||||
import recommend from './routes/recommend.route';
|
||||
require('./services/auth/passport');
|
||||
@@ -39,6 +40,7 @@ export default () => {
|
||||
server.use('/api/plex', plex);
|
||||
server.use('/api/tdaw', tdaw);
|
||||
server.use('/api/moviedb', movieDb);
|
||||
server.use('/api/sonarr', sonarr);
|
||||
server.use('/api/recommend', recommend);
|
||||
server.use('/auth', auth);
|
||||
server.use('/api/auth', auth);
|
||||
|
||||
9
server/routes/sonarr.route.js
Normal file
9
server/routes/sonarr.route.js
Normal file
@@ -0,0 +1,9 @@
|
||||
import sonarrController from '../controllers/sonarr.controller';
|
||||
|
||||
const express = require('express');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use(sonarrController);
|
||||
|
||||
export default router;
|
||||
@@ -43,6 +43,7 @@ const request = async function(url) {
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.response) {
|
||||
console.log('Error: Response --', error.response);
|
||||
console.log('Error: Status--', error.response.status);
|
||||
console.log('Error: Headers--', error.response.headers);
|
||||
return reject(error.response);
|
||||
|
||||
@@ -75,7 +75,6 @@ const importMostWatched = async (req, res) => {
|
||||
|
||||
const importAll = async (req, res) => {
|
||||
try {
|
||||
console.log('user', req.user);
|
||||
await importData.importSections(req.user);
|
||||
await importData.importLibraries(req.user);
|
||||
await importData.importMostWatched(req.user);
|
||||
|
||||
37
server/services/sonarr/index.js
Normal file
37
server/services/sonarr/index.js
Normal file
@@ -0,0 +1,37 @@
|
||||
import sonarrApi from './sonarrApi';
|
||||
import models from '../../db/models';
|
||||
import helpers from '../helpers';
|
||||
import {Op} from 'sequelize';
|
||||
|
||||
const search = async (req, res) => {
|
||||
const {showName} = req.query;
|
||||
console.log(showName, req.user.sonarrUrl);
|
||||
const response = await sonarrApi.search(showName, req.user);
|
||||
res.json(response);
|
||||
};
|
||||
|
||||
const addShow = async (req, res) => {
|
||||
const {showName} = req.query;
|
||||
const response = await sonarrApi.addShow(showName, req.user);
|
||||
res.json(response);
|
||||
};
|
||||
|
||||
// const similarTv = async (req, res) => {
|
||||
// const {showName} = req.query;
|
||||
// const searchResponse = await movieDbApi.searchTv(showName);
|
||||
// const similarResponse = await movieDbApi.similarTV(searchResponse.id);
|
||||
// const library = await models.PlexLibrary.findAll({
|
||||
// userId: req.user.id,
|
||||
// type: 'show',
|
||||
// });
|
||||
// const libraryTitles = library.map(show => show.title.toLowerCase());
|
||||
// const filteredResponse = similarResponse.results.filter(
|
||||
// show => !libraryTitles.includes(show.name.toLowerCase()),
|
||||
// );
|
||||
// res.json(filteredResponse);
|
||||
// };
|
||||
|
||||
export default {
|
||||
search,
|
||||
addShow,
|
||||
};
|
||||
57
server/services/sonarr/sonarrApi.js
Normal file
57
server/services/sonarr/sonarrApi.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import config from '../../../config';
|
||||
import helpers from '../helpers';
|
||||
import models from '../../db/models';
|
||||
import request from 'request-promise';
|
||||
|
||||
const search = async (showName, user) => {
|
||||
try {
|
||||
const params = {
|
||||
uri: user.sonarrUrl + '/api/series/lookup',
|
||||
headers: {'x-api-key': user.sonarrApiKey},
|
||||
qs: {
|
||||
term: showName,
|
||||
},
|
||||
};
|
||||
const res = await request(params);
|
||||
const jsonData = await JSON.parse(res);
|
||||
return jsonData[0];
|
||||
} catch (error) {
|
||||
helpers.handleError(error, 'searchSonarr');
|
||||
}
|
||||
};
|
||||
|
||||
const addShow = async (showName, user) => {
|
||||
try {
|
||||
const body = await search(showName, user);
|
||||
body.profileId = 1;
|
||||
const rootFolder = await getRootFolder(user);
|
||||
console.log('mike---', JSON.parse(rootFolder)[0].path);
|
||||
body.rootFolderPath = JSON.parse(rootFolder)[0].path;
|
||||
const params = {
|
||||
baseUrl: user.sonarrUrl,
|
||||
uri: '/api/series',
|
||||
headers: {'x-api-key': user.sonarrApiKey},
|
||||
body: body,
|
||||
json: true,
|
||||
};
|
||||
|
||||
console.log(params);
|
||||
const res = await request.post(params);
|
||||
console.log(res);
|
||||
return res;
|
||||
} catch (error) {
|
||||
console.log('error--', error);
|
||||
return error.error[0].errorMessage;
|
||||
}
|
||||
};
|
||||
|
||||
const getRootFolder = async user => {
|
||||
const params = {
|
||||
uri: user.sonarrUrl + '/api/rootfolder',
|
||||
headers: {'x-api-key': user.sonarrApiKey},
|
||||
};
|
||||
const res = await request(params);
|
||||
return res;
|
||||
};
|
||||
|
||||
export default {search, addShow};
|
||||
Reference in New Issue
Block a user