-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add new scrape endpoint * a lot of things
- Loading branch information
1 parent
cce8dcf
commit f904d1b
Showing
12 changed files
with
1,022 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.