feat: a tiny bit of work to rip out letterboxd

This commit is contained in:
Cory Dransfeldt 2023-10-02 09:20:07 -07:00
parent 2f0c3d7ac0
commit 681f26e9ea
8 changed files with 80 additions and 53 deletions

View file

@ -1,32 +1,43 @@
const Parser = require('rss-parser')
const { AssetCache } = require('@11ty/eleventy-fetch')
const EleventyFetch = require('@11ty/eleventy-fetch')
module.exports = async function () {
const parser = new Parser()
const url = 'https://letterboxd.com/cdme/rss'
const asset = new AssetCache('movies_data')
if (asset.isCacheValid('1h')) return await asset.getCachedValue()
const res = await parser.parseURL(url).catch((error) => {
console.log(error.message)
const TV_KEY = process.env.API_KEY_TRAKT
const MOVIEDB_KEY = process.env.API_KEY_MOVIEDB
const url = 'https://api.trakt.tv/users/cdransf/history/movies?page=1&limit=30'
const res = EleventyFetch(url, {
duration: '1h',
type: 'json',
fetchOptions: {
headers: {
'Content-Type': 'application/json',
'trakt-api-version': 2,
'trakt-api-key': TV_KEY,
},
},
}).catch()
const data = await res
const movies = data.map((item) => {
return {
title: item['movie']['title'],
dateAdded: item['watched_at'],
url: `https://trakt.tv/movies/${item['movie']['ids']['slug']}`,
id: item['movie']['ids']['trakt'],
tmdbId: item['movie']['ids']['tmdb'],
type: 'movie',
}
})
const movies = res.items
.map((item) => {
const images = item['content']?.match(/<img [^>]*src="[^"]*"[^>]*>/gm) || []
return {
title: item['title'],
date: item['pubDate'],
description: item['contentSnippet'],
image: images.length
? images
.map((image) => image.replace(/.*src="([^"]*)".*/, '$1'))[0]
.replace('https://a.ltrbxd.com', 'https://movies.coryd.dev')
: 'https://cdn.coryd.dev/movies/missing-movie.jpg',
url: item['link'],
id: item['guid'],
type: 'movie',
}
for (const movie of movies) {
const tmdbId = movie['tmdbId']
const tmdbUrl = `https://api.themoviedb.org/3/movie/${tmdbId}?api_key=${MOVIEDB_KEY}`
const tmdbRes = EleventyFetch(tmdbUrl, {
duration: '1h',
type: 'json',
})
.filter((movie) => !movie.url.includes('/list/'))
await asset.save(movies, 'json')
const tmdbData = await tmdbRes
const posterPath = tmdbData['poster_path']
movie.image = `https://movies.coryd.dev/t/p/w500${posterPath}`
}
return movies
}

View file

@ -19,7 +19,6 @@ module.exports = async function () {
url: 'https://open.spotify.com/user/mdh0acvmvfsbunzt6ywnq2tg3',
icon: 'brand-spotify',
},
{ name: 'Letterboxd', url: 'https://letterboxd.com/cdme', icon: 'brand-letterboxd' },
{ name: 'Trakt', url: 'https://trakt.tv/users/cdransf', icon: 'device-tv' },
{ name: 'Goodreads', url: 'https://www.goodreads.com/cdransf', icon: 'books' },
{ name: 'Buy Me a Coffee', url: 'https://www.buymeacoffee.com/cory', icon: 'cup' },

View file

@ -2,6 +2,7 @@ const EleventyFetch = require('@11ty/eleventy-fetch')
module.exports = async function () {
const TV_KEY = process.env.API_KEY_TRAKT
const MOVIEDB_KEY = process.env.API_KEY_MOVIEDB
const url = 'https://api.trakt.tv/users/cdransf/history/shows?page=1&limit=30'
const res = EleventyFetch(url, {
duration: '1h',
@ -15,59 +16,66 @@ module.exports = async function () {
},
}).catch()
const data = await res
const episodes = []
const episodeData = []
data.reverse().forEach((episode) => {
const episodeNumber = episode['episode']['number']
const seasonNumber = episode['episode']['season']
if (episodes.find((e) => e.name === episode?.['show']?.['title'])) {
if (episodeData.find((e) => e.name === episode?.['show']?.['title'])) {
// cache the matched episode reference
const matchedEpisode = episodes.find((e) => e.name === episode?.['show']?.['title'])
const matchedEpisode = episodeData.find((e) => e.name === episode?.['show']?.['title'])
// remove the matched episode from the array
episodes.splice(
episodes.findIndex((e) => e.name === episode['show']['title']),
episodeData.splice(
episodeData.findIndex((e) => e.name === episode['show']['title']),
1
)
// push the new episode to the array
episodes.push({
episodeData.push({
name: matchedEpisode['name'],
title: matchedEpisode['title'],
url: `https://trakt.tv/shows/${episode['show']['ids']['slug']}`,
subtext: `S${matchedEpisode['startingSeason'] || matchedEpisode['season']}E${
matchedEpisode['startingEpisode'] || matchedEpisode['episode']
} - S${episode['episode']['season']}E${episode['episode']['number']}`,
image:
`https://cdn.coryd.dev/tv/${matchedEpisode['name']
.replace(':', '')
.replace(/\s+/g, '-')
.toLowerCase()}.jpg` || 'https://cdn.coryd.dev/tv/missing-tv.jpg',
startingEpisode: matchedEpisode['episode'],
startingSeason: matchedEpisode['season'],
episode: episodeNumber,
season: seasonNumber,
id: episode['show']['ids']['trakt'],
tmdbId: episode['show']['ids']['tmdb'],
type: 'tv-range',
})
} else {
// if an episode with the same show name doesn't exist, push it to the array
episodes.push({
episodeData.push({
name: episode['show']['title'],
title: episode['episode']['title'],
url: `https://trakt.tv/shows/${episode['show']['ids']['slug']}/seasons/${episode['episode']['season']}/episodes/${episode['episode']['number']}`,
subtext: `${episode['show']['title']} • S${episode['episode']['season']}E${episode['episode']['number']}`,
image:
`https://cdn.coryd.dev/tv/${episode['show']['title']
.replace(':', '')
.replace(/\s+/g, '-')
.toLowerCase()}.jpg` || 'https://cdn.coryd.dev/tv/missing-tv.jpg',
episode: episodeNumber,
season: seasonNumber,
id: episode['show']['ids']['trakt'],
tmdbId: episode['show']['ids']['tmdb'],
type: 'tv',
})
}
})
// return a reverse sorted array of episodes to match the watch order
return episodes.reverse()
const episodes = episodeData.reverse()
for (const episode of episodes) {
const tmdbId = episode['tmdbId']
const tmdbUrl = `https://api.themoviedb.org/3/tv/${tmdbId}?api_key=${MOVIEDB_KEY}`
const tmdbRes = EleventyFetch(tmdbUrl, {
duration: '1h',
type: 'json',
})
const tmdbData = await tmdbRes
const posterPath = tmdbData['poster_path']
episode.image = `https://movies.coryd.dev/t/p/w500${posterPath}`
}
return episodes
}