feat: move images to b2 and route through bunny

This commit is contained in:
Cory Dransfeldt 2024-07-10 08:57:00 -07:00
parent cb2d536189
commit 6a338ceb92
No known key found for this signature in database
32 changed files with 322 additions and 375 deletions

View file

@ -1,7 +1,7 @@
import { createClient } from '@supabase/supabase-js'
const SUPABASE_URL = process.env.SUPABASE_URL
const SUPABASE_KEY = process.env.SUPABASE_KEY
const SUPABASE_URL = process.env['SUPABASE_URL']
const SUPABASE_KEY = process.env['SUPABASE_KEY']
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY)
const PAGE_SIZE = 1000
@ -21,6 +21,8 @@ const fetchAllShows = async () => {
year,
description,
review,
art(filename_disk),
backdrop(filename_disk),
episodes (
episode_number,
season_number,
@ -35,7 +37,6 @@ const fetchAllShows = async () => {
}
shows = shows.concat(data)
if (data.length < PAGE_SIZE) break
rangeStart += PAGE_SIZE
}
@ -43,38 +44,47 @@ const fetchAllShows = async () => {
return shows
}
const prepareShowData = (show) => {
return {
...show,
image: show.art?.filename_disk ? show.art.filename_disk : '',
backdrop: show.backdrop?.filename_disk ? show.backdrop.filename_disk : ''
}
}
const prepareEpisodeData = (show) => {
return show.episodes.map(episode => ({
...episode,
show_title: show.title,
show_tmdb_id: show.tmdb_id,
collected: show.collected,
favorite: show.favorite,
image: show.image,
backdrop: show.backdrop
}))
}
export default async function () {
const shows = await fetchAllShows()
const rawShows = await fetchAllShows()
const shows = rawShows.map(prepareShowData)
let episodes = []
shows.forEach(show => {
show.episodes.forEach(episode => {
episodes.push({
...episode,
show_title: show['title'],
show_tmdb_id: show['tmdb_id'],
collected: show['collected'],
favorite: show['favorite'],
year: show['year']
})
})
})
const episodes = shows.flatMap(prepareEpisodeData)
episodes.sort((a, b) => new Date(b.last_watched_at) - new Date(a.last_watched_at))
episodes.sort((a, b) => new Date(b['last_watched_at']) - new Date(a['last_watched_at']))
const allEpisodes = episodes
const recentlyWatchedEpisodes = episodes.slice(0, 225)
const formatEpisodeData = (episodes) => {
const episodeData = []
const showEpisodesMap = {}
episodes.forEach((episode) => {
const showTitle = episode['show_title']
const showTmdbId = episode['show_tmdb_id']
const episodeNumber = episode['episode_number']
const seasonNumber = episode['season_number']
const lastWatchedAt = episode['last_watched_at']
const collected = episode['collected']
const favorite = episode['favorite']
episodes.forEach(episode => {
const showTitle = episode.show_title
const showTmdbId = episode.show_tmdb_id
const episodeNumber = episode.episode_number
const seasonNumber = episode.season_number
const lastWatchedAt = episode.last_watched_at
const collected = episode.collected
const favorite = episode.favorite
const image = episode.image
const backdrop = episode.backdrop
if (!showEpisodesMap[showTmdbId]) {
showEpisodesMap[showTmdbId] = {
@ -84,7 +94,9 @@ export default async function () {
favorite,
dateAdded: lastWatchedAt,
lastWatchedAt,
episodes: []
episodes: [],
image,
backdrop
}
}
@ -96,41 +108,44 @@ export default async function () {
season: seasonNumber,
tmdbId: showTmdbId,
type: 'tv',
image: `/shows/poster-${showTmdbId}.jpg`,
backdrop: `/shows/backdrops/backdrop-${showTmdbId}.jpg`,
dateAdded: lastWatchedAt,
lastWatchedAt
lastWatchedAt,
image,
backdrop
})
})
const sortedShows = Object.values(showEpisodesMap).sort((a, b) => new Date(b['episodes'][0]['lastWatchedAt']) - new Date(a['episodes'][0]['lastWatchedAt']))
const sortedShows = Object.values(showEpisodesMap).sort((a, b) => new Date(b.episodes[0].lastWatchedAt) - new Date(a.episodes[0].lastWatchedAt))
sortedShows.forEach((show) => {
const startingEpisode = show['episodes'][show['episodes'].length - 1]['episode']
const startingSeason = show['episodes'][show['episodes'].length - 1]['season']
const endingEpisode = show['episodes'][0]['episode']
const endingSeason = show['episodes'][0]['season']
const episodeData = []
sortedShows.forEach(show => {
const startingEpisode = show.episodes[show.episodes.length - 1].episode
const startingSeason = show.episodes[show.episodes.length - 1].season
const endingEpisode = show.episodes[0].episode
const endingSeason = show.episodes[0].season
if (show.episodes.length > 1) {
episodeData.push({
name: show['title'],
url: `/watching/shows/${show['tmdbId']}`,
name: show.title,
url: `/watching/shows/${show.tmdbId}`,
subtext: `S${startingSeason}E${startingEpisode} - S${endingSeason}E${endingEpisode}`,
startingEpisode,
startingSeason,
episode: endingEpisode,
season: endingSeason,
tmdbId: show['tmdbId'],
collected: show['collected'],
favorite: show['favorite'],
tmdbId: show.tmdbId,
collected: show.collected,
favorite: show.favorite,
type: 'tv-range',
image: `/shows/poster-${show['tmdbId']}.jpg`,
backdrop: `/shows/backdrops/backdrop-${show['tmdbId']}.jpg`,
image: show.image,
backdrop: show.backdrop
})
} else {
const singleEpisode = show['episodes'][0]
singleEpisode['collected'] = show['collected']
singleEpisode['favorite'] = show['favorite']
const singleEpisode = show.episodes[0]
singleEpisode.collected = show.collected
singleEpisode.favorite = show.favorite
singleEpisode.image = show.image
singleEpisode.backdrop = show.backdrop
episodeData.push(singleEpisode)
}
})
@ -138,20 +153,12 @@ export default async function () {
return episodeData
}
const favoriteShows = shows.filter(show => show['favorite'])
const collectedShows = shows.filter(show => show['collected'])
const toWatch = shows.map(show => ({...show, url: `/watching/shows/${show['tmdb_id']}`})).filter(show => !show.episodes.some(episode => episode.last_watched_at)).sort((a, b) => a['title'].localeCompare(b['title']))
const favoriteShows = shows.filter(show => show.favorite)
return {
shows,
watchHistory: formatEpisodeData(allEpisodes),
recentlyWatched: formatEpisodeData(recentlyWatchedEpisodes),
favorites: formatEpisodeData(favoriteShows.flatMap(show => show['episodes'].map(episode => ({
...episode,
show_title: show['title'],
show_tmdb_id: show['tmdb_id'],
collected: show['collected'],
favorite: show['favorite']
})))).sort((a, b) => a['name'].localeCompare(b['name']))
watchHistory: formatEpisodeData(episodes),
recentlyWatched: formatEpisodeData(episodes.slice(0, 225)),
favorites: formatEpisodeData(favoriteShows.flatMap(prepareEpisodeData)).sort((a, b) => a.name.localeCompare(b.name))
}
}