-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: search internal and external in log media
- Loading branch information
1 parent
7d48f27
commit e594dc4
Showing
13 changed files
with
404 additions
and
144 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
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,45 @@ | ||
import { validateSessionToken } from '@/server/auth/validateSession'; | ||
import prisma from '@/server/db'; | ||
import { Category } from '@prisma/client'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
export const GET = async (request: NextRequest) => { | ||
const user = await validateSessionToken(); | ||
|
||
if (!user) { | ||
return Response.json({ error: 'You are not logged in' }, { status: 400 }); | ||
} | ||
|
||
const query = request.nextUrl.searchParams.get('q'); | ||
const take = request.nextUrl.searchParams.get('take') | ||
? Number(request.nextUrl.searchParams.get('take')) | ||
: undefined; | ||
const categories = request.nextUrl.searchParams.get('categories') | ||
? (request.nextUrl.searchParams.get('categories')!.split(',') as Category[]) | ||
: undefined; | ||
|
||
return Response.json( | ||
await prisma.entry.findMany({ | ||
where: { | ||
category: categories | ||
? { | ||
in: categories.filter(e => !!e), | ||
} | ||
: undefined, | ||
alternativeTitles: { | ||
some: { | ||
title: { | ||
contains: query ?? '', | ||
}, | ||
}, | ||
}, | ||
}, | ||
take, | ||
orderBy: { | ||
userEntries: { | ||
_count: 'desc', | ||
}, | ||
}, | ||
}) | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,112 @@ | ||
import prisma from '@/server/db'; | ||
import { Category } from '@prisma/client'; | ||
import { NextRequest } from 'next/server'; | ||
|
||
export const dynamic = 'force-dynamic'; | ||
|
||
export const GET = async (request: NextRequest) => { | ||
const search = request.nextUrl.searchParams.get('q'); | ||
const excludeExisting = !!Boolean( | ||
request.nextUrl.searchParams.get('excludeExisting') | ||
); // Exclude media already in the local database | ||
const take = request.nextUrl.searchParams.get('take') | ||
? Number(request.nextUrl.searchParams.get('take')) | ||
: 10; | ||
const categories = request.nextUrl.searchParams.get('categories') | ||
? (request.nextUrl.searchParams.get('categories')!.split(',') as Category[]) | ||
: ['Book', 'Series', 'Movie']; | ||
|
||
if (!search) { | ||
return Response.json([]); | ||
} | ||
|
||
const openLibrary = ( | ||
await ( | ||
await fetch( | ||
`https://openlibrary.org/search.json?title=${search.replace(' ', '+')}` | ||
) | ||
).json() | ||
).docs | ||
.map((ol: any) => { | ||
let lowestReleaseDate = new Date(); | ||
let releaseDateFallback: string | undefined = undefined; | ||
|
||
if (ol.publish_date) { | ||
for (const d of ol.publish_date) { | ||
const date = new Date(d); | ||
if (date.getTime() < lowestReleaseDate.getTime()) { | ||
lowestReleaseDate = date; | ||
} | ||
} | ||
} else { | ||
releaseDateFallback = 'No release date'; | ||
} | ||
|
||
return { | ||
title: ol.title, | ||
category: 'Book', | ||
releaseDate: releaseDateFallback ?? lowestReleaseDate.toDateString(), | ||
author: ol.author_name ? ol.author_name[0] : '', | ||
foreignId: ol.key.split('/')[ol.key.split('/').length - 1], | ||
posterPath: `https://covers.openlibrary.org/b/olid/${ol.edition_key[0]}-L.jpg`, | ||
}; | ||
}) | ||
.filter((e: any) => !!new Date(e.releaseDate)) | ||
.filter(() => categories.includes('Book')); | ||
|
||
const options = { | ||
method: 'GET', | ||
headers: { | ||
accept: 'application/json', | ||
Authorization: `Bearer ${process.env.TMDB_ACCESS_TOKEN}`, | ||
}, | ||
}; | ||
|
||
console.log('b'); | ||
|
||
const tmdb = ( | ||
await ( | ||
await fetch( | ||
`https://api.themoviedb.org/3/search/multi?query=${search}&include_adult=false&language=en-US&page=1`, | ||
options | ||
) | ||
).json() | ||
).results | ||
.map((tmdb: any) => ({ | ||
title: tmdb.original_title ?? tmdb.name, | ||
category: tmdb.media_type === 'tv' ? 'Series' : 'Movie', | ||
releaseDate: | ||
tmdb.type === 'tv' | ||
? new Date(tmdb.first_air_date).toDateString() | ||
: new Date(tmdb.release_date).toDateString(), | ||
author: '', | ||
foreignId: tmdb.id, | ||
posterPath: 'https://image.tmdb.org/t/p/original/' + tmdb.poster_path, | ||
})) | ||
.filter((tmdb: any) => categories?.includes(tmdb.category)) | ||
.filter((e: any) => e.releaseDate !== 'Invalid Date'); | ||
|
||
if (excludeExisting) { | ||
let finalMedia = []; | ||
for (const media of [...openLibrary, ...tmdb]) { | ||
const existing = await prisma.entry.findFirst({ | ||
where: { | ||
foreignId: media.foreignId.toString(), | ||
category: media.category, | ||
}, | ||
}); | ||
|
||
if (!existing) { | ||
finalMedia.push(media); | ||
} | ||
} | ||
|
||
console.log('c'); | ||
|
||
return Response.json(finalMedia.slice(0, take)); | ||
} | ||
|
||
console.log('d'); | ||
|
||
return Response.json([...openLibrary, ...tmdb].slice(0, take)); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.