Skip to content

Commit

Permalink
2.7.0 (#51)
Browse files Browse the repository at this point in the history
* add new scrape endpoint

* a lot of things
  • Loading branch information
yowmamasita authored Aug 29, 2023
1 parent cce8dcf commit f904d1b
Show file tree
Hide file tree
Showing 12 changed files with 1,022 additions and 100 deletions.
16 changes: 16 additions & 0 deletions next.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
remotePatterns: [
{
protocol: 'https',
hostname: 'image.tmdb.org',
port: '',
pathname: '/t/p/w200/**',
},
{
protocol: 'https',
hostname: 'picsum.photos',
port: '',
pathname: '/**',
},
],
},
reactStrictMode: false,
publicRuntimeConfig: {
// Will be available on both server and client
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "debrid-media-manager",
"version": "2.6.0",
"version": "2.7.0",
"private": false,
"scripts": {
"dev": "next dev",
Expand Down
38 changes: 38 additions & 0 deletions src/components/poster.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import Image from 'next/image';
import { getTmdbKey } from '@/utils/freekeys';

const TMDBPoster = ({ imdbId }: Record<string, string>) => {
const [posterUrl, setPosterUrl] = useState('');

useEffect(() => {
const fetchData = async () => {
const response = await axios.get(`https://api.themoviedb.org/3/find/${imdbId}?api_key=${getTmdbKey()}&external_source=imdb_id`);
const baseUrl = 'https://image.tmdb.org/t/p/w200';
if (response.data.movie_results.length > 0 && response.data.movie_results[0].poster_path) {
setPosterUrl(baseUrl + response.data.movie_results[0].poster_path);
} if (response.data.tv_results.length > 0 && response.data.tv_results[0].poster_path) {
setPosterUrl(baseUrl + response.data.tv_results[0].poster_path);
} else {
// If no poster_path, set a placeholder image URL
setPosterUrl(`https://picsum.photos/seed/${imdbId}/200/300`);
}
};

try {
if (imdbId) fetchData();
else setPosterUrl(`https://picsum.photos/seed/${imdbId}/200/300`);
} catch (error: any) {
setPosterUrl(`https://picsum.photos/seed/${imdbId}/200/300`);
}
}, [imdbId]);

return (
<div>
{posterUrl && <Image width={200} height={300} src={posterUrl} alt="Movie poster" />}
</div>
);
};

export default TMDBPoster;
50 changes: 50 additions & 0 deletions src/pages/api/keywordsearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { PlanetScaleCache } from '@/services/planetscale';
import axios from 'axios';
import { NextApiHandler } from 'next';

const mdblistKey = process.env.MDBLIST_KEY;
const searchMdb = (keyword: string) => `https://mdblist.com/api/?apikey=${mdblistKey}&s=${keyword}`;
const getMdbInfo = (imdbId: string) => `https://mdblist.com/api/?apikey=${mdblistKey}&i=${imdbId}`;
const db = new PlanetScaleCache();

const handler: NextApiHandler = async (req, res) => {
const { keyword } = req.query;

if (!keyword || !(typeof keyword === 'string')) {
res.status(400).json({ status: 'error', errorMessage: 'Missing "keyword" query parameter' });
return;
}

try {
const searchResults = await db.getSearchResults<any[]>(keyword.toString().trim());
if (searchResults) {
res.status(200).json({ results: searchResults.filter(r => r.imdbid) });
return;
}

const searchResponse = await axios.get(searchMdb(keyword.toString().trim()));
const results = ([...searchResponse.data.search]).filter((result: any) => result.imdbid);

for (let i = 0; i < results.length; i++) {
if (results[i].type === 'show') {
const showResponse = await axios.get(getMdbInfo(results[i].imdbid));
const seasons = showResponse.data.seasons.filter((season: any) => season.season_number > 0)
.map((season: any) => {
return season.season_number;
});
results[i].season_count = Math.max(...seasons);
}
}

console.log('search results', results.length);

await db.saveSearchResults(keyword.toString().trim(), results);

res.status(200).json({ results });
} catch (error: any) {
console.error('encountered a search issue', error);
res.status(500).json({ status: 'error', errorMessage: error.message });
}
};

export default handler;
28 changes: 28 additions & 0 deletions src/pages/api/moviesearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { PlanetScaleCache } from '@/services/planetscale';
import { NextApiHandler } from 'next';

const db = new PlanetScaleCache();

const handler: NextApiHandler = async (req, res) => {
const { imdbId } = req.query;

if (!imdbId || !(typeof imdbId === 'string')) {
res.status(400).json({ errorMessage: 'Missing "imdbId" query parameter' });
return;
}

try {
const searchResults = await db.getScrapedResults<any[]>(`movie:${imdbId.toString().trim()}`);
if (searchResults) {
res.status(200).json({ results: searchResults });
return;
}

res.status(204).json({ results: [] });
} catch (error: any) {
console.error('encountered a db issue', error);
res.status(500).json({ errorMessage: error.message });
}
};

export default handler;
4 changes: 2 additions & 2 deletions src/pages/api/scrape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default async function handler(
let itemType: 'movie' | 'tv' = 'movie';

if (tmdbResponse.data.movie_results.length > 0) {
if (override && override !== 'true') {
if (!override || override !== 'true') {
const keyExists = await db.keyExists(`movie:${imdbId}`);
if (keyExists) {
res.status(200).json({ status: 'skipped' });
Expand Down Expand Up @@ -128,7 +128,7 @@ export default async function handler(
}

if (tmdbResponse.data.tv_results.length > 0) {
if (override && override !== 'true') {
if (!override || override !== 'true') {
const keyExists = await db.keyExists(`tv:${imdbId}:1`);
if (keyExists) {
res.status(200).json({ status: 'skipped' });
Expand Down
36 changes: 36 additions & 0 deletions src/pages/api/tvsearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PlanetScaleCache } from '@/services/planetscale';
import { NextApiHandler } from 'next';

const db = new PlanetScaleCache();

const handler: NextApiHandler = async (req, res) => {
const { imdbId, seasonNum } = req.query;

if (!imdbId || !(typeof imdbId === 'string')) {
res.status(400).json({ errorMessage: 'Missing "imdbId" query parameter' });
return;
}
if (!seasonNum || !(typeof seasonNum === 'string')) {
res.status(400).json({
errorMessage: 'Missing "seasonNum" query parameter',
});
return;
}

try {
const searchResults = await db.getScrapedResults<any[]>(
`tv:${imdbId.toString().trim()}:${parseInt(seasonNum.toString().trim(), 10)}`
);
if (searchResults) {
res.status(200).json({ results: searchResults });
return;
}

res.status(204).json({ results: [] });
} catch (error: any) {
console.error('encountered a db issue', error);
res.status(500).json({ errorMessage: error.message });
}
};

export default handler;
Loading

0 comments on commit f904d1b

Please sign in to comment.