mirror of
https://github.com/mjrode/WhatToWatch.git
synced 2026-04-28 16:40:43 -05:00
96 lines
2.1 KiB
JavaScript
96 lines
2.1 KiB
JavaScript
import plexApi from './plexApi';
|
|
import models from '../../db/models';
|
|
|
|
const importSections = async () => {
|
|
const sections = await plexApi.getSections();
|
|
createSections(sections);
|
|
return sections;
|
|
};
|
|
|
|
const createSections = sections => {
|
|
sections.forEach(async section => {
|
|
await models.PlexSection.upsert(
|
|
{
|
|
title: section.title,
|
|
type: section.type,
|
|
key: section.key,
|
|
},
|
|
{
|
|
where: {
|
|
title: section.title,
|
|
},
|
|
},
|
|
);
|
|
});
|
|
};
|
|
|
|
const importLibraries = async () => {
|
|
const sections = await plexApi.getSections();
|
|
sections.forEach(async section => {
|
|
await importLibrary(section.key);
|
|
});
|
|
};
|
|
|
|
const importMostWatched = async req => {
|
|
const mostWatched = await plexApi.getMostWatched(req);
|
|
mostWatched.forEach(async libraryData => {
|
|
await updateLibrary([libraryData]);
|
|
});
|
|
};
|
|
|
|
const importLibrary = async sectionId => {
|
|
const libraryData = await plexApi.getLibraryDataBySection({
|
|
sectionId,
|
|
});
|
|
createLibrary(libraryData);
|
|
return libraryData;
|
|
};
|
|
|
|
const updateLibrary = libraryData => {
|
|
libraryData.forEach(async data => {
|
|
await models.PlexLibrary.update(
|
|
{
|
|
title: data.title,
|
|
type: data.type,
|
|
views: data.globalViewCount,
|
|
rating_key: data.ratingKey,
|
|
metadata_path: data.key,
|
|
summary: data.summary,
|
|
rating: data.rating,
|
|
year: data.year,
|
|
genre: JSON.stringify(data.Genre),
|
|
},
|
|
{
|
|
where: {
|
|
title: data.title,
|
|
},
|
|
},
|
|
);
|
|
});
|
|
};
|
|
|
|
const createLibrary = libraryData => {
|
|
libraryData.forEach(async data => {
|
|
await models.PlexLibrary.upsert(
|
|
{
|
|
title: data.title,
|
|
type: data.type,
|
|
views: data.views,
|
|
rating_key: data.ratingKey,
|
|
metadata_path: data.key,
|
|
summary: data.summary,
|
|
rating: data.rating,
|
|
year: data.year,
|
|
genre: JSON.stringify(data.Genre),
|
|
},
|
|
{
|
|
where: {
|
|
title: data.title,
|
|
},
|
|
},
|
|
);
|
|
});
|
|
};
|
|
|
|
export default {importSections, importLibraries, importMostWatched};
|