mirror of
https://github.com/sct/overseerr.git
synced 2025-09-17 17:24:35 +02:00
feat: plex watchlist sync integration (#2885)
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
import { Router } from 'express';
|
||||
import { sortBy } from 'lodash';
|
||||
import PlexTvAPI from '../api/plextv';
|
||||
import TheMovieDb from '../api/themoviedb';
|
||||
import { MediaType } from '../constants/media';
|
||||
import { getRepository } from '../datasource';
|
||||
import Media from '../entity/Media';
|
||||
import type { User } from '../entity/User';
|
||||
import type { GenreSliderItem } from '../interfaces/api/discoverInterfaces';
|
||||
import { User } from '../entity/User';
|
||||
import type {
|
||||
GenreSliderItem,
|
||||
WatchlistItem,
|
||||
} from '../interfaces/api/discoverInterfaces';
|
||||
import { getSettings } from '../lib/settings';
|
||||
import logger from '../logger';
|
||||
import { mapProductionCompany } from '../models/Movie';
|
||||
@@ -704,4 +709,50 @@ discoverRoutes.get<{ language: string }, GenreSliderItem[]>(
|
||||
}
|
||||
);
|
||||
|
||||
discoverRoutes.get<
|
||||
{ page?: number },
|
||||
{
|
||||
page: number;
|
||||
totalPages: number;
|
||||
totalResults: number;
|
||||
results: WatchlistItem[];
|
||||
}
|
||||
>('/watchlist', async (req, res) => {
|
||||
const userRepository = getRepository(User);
|
||||
const itemsPerPage = 20;
|
||||
const page = req.params.page ?? 1;
|
||||
const offset = (page - 1) * itemsPerPage;
|
||||
|
||||
const activeUser = await userRepository.findOne({
|
||||
where: { id: req.user?.id },
|
||||
select: ['id', 'plexToken'],
|
||||
});
|
||||
|
||||
if (!activeUser?.plexToken) {
|
||||
// We will just return an empty array if the user has no plex token
|
||||
return res.json({
|
||||
page: 1,
|
||||
totalPages: 1,
|
||||
totalResults: 0,
|
||||
results: [],
|
||||
});
|
||||
}
|
||||
|
||||
const plexTV = new PlexTvAPI(activeUser?.plexToken);
|
||||
|
||||
const watchlist = await plexTV.getWatchlist({ offset });
|
||||
|
||||
return res.json({
|
||||
page,
|
||||
totalPages: Math.ceil(watchlist.size / itemsPerPage),
|
||||
totalResults: watchlist.size,
|
||||
results: watchlist.items.map((item) => ({
|
||||
ratingKey: item.ratingKey,
|
||||
title: item.title,
|
||||
mediaType: item.type === 'show' ? 'tv' : 'movie',
|
||||
tmdbId: item.tmdbId,
|
||||
})),
|
||||
});
|
||||
});
|
||||
|
||||
export default discoverRoutes;
|
||||
|
Reference in New Issue
Block a user