2024-08-11 22:15:34 +02:00
|
|
|
const { Request, Response } = require('express')
|
2024-06-09 20:43:03 +02:00
|
|
|
const Logger = require('../Logger')
|
2023-09-04 23:33:55 +02:00
|
|
|
const BookFinder = require('../finders/BookFinder')
|
|
|
|
const PodcastFinder = require('../finders/PodcastFinder')
|
|
|
|
const AuthorFinder = require('../finders/AuthorFinder')
|
|
|
|
const MusicFinder = require('../finders/MusicFinder')
|
2024-06-09 20:43:03 +02:00
|
|
|
const Database = require('../Database')
|
|
|
|
const { isValidASIN } = require('../utils')
|
2022-11-20 23:12:30 +01:00
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
2024-08-12 00:01:25 +02:00
|
|
|
* @typedef RequestUserObject
|
2024-08-11 23:07:29 +02:00
|
|
|
* @property {import('../models/User')} user
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @typedef {Request & RequestUserObject} RequestWithUser
|
2024-08-11 22:15:34 +02:00
|
|
|
*/
|
|
|
|
|
2022-11-19 20:28:06 +01:00
|
|
|
class SearchController {
|
2024-06-09 20:43:03 +02:00
|
|
|
constructor() {}
|
2022-11-19 20:28:06 +01:00
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/search/books
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-11-19 20:28:06 +01:00
|
|
|
async findBooks(req, res) {
|
2023-12-08 23:33:06 +01:00
|
|
|
const id = req.query.id
|
|
|
|
const libraryItem = await Database.libraryItemModel.getOldById(id)
|
2022-12-13 00:45:51 +01:00
|
|
|
const provider = req.query.provider || 'google'
|
|
|
|
const title = req.query.title || ''
|
|
|
|
const author = req.query.author || ''
|
2024-08-11 22:15:34 +02:00
|
|
|
|
|
|
|
if (typeof provider !== 'string' || typeof title !== 'string' || typeof author !== 'string') {
|
|
|
|
Logger.error(`[SearchController] findBooks: Invalid request query params`)
|
|
|
|
return res.status(400).send('Invalid request query params')
|
|
|
|
}
|
|
|
|
|
2023-12-08 23:33:06 +01:00
|
|
|
const results = await BookFinder.search(libraryItem, provider, title, author)
|
2022-12-13 00:45:51 +01:00
|
|
|
res.json(results)
|
2022-11-19 20:28:06 +01:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/search/covers
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-11-19 20:28:06 +01:00
|
|
|
async findCovers(req, res) {
|
2022-12-13 00:45:51 +01:00
|
|
|
const query = req.query
|
2022-11-20 23:12:30 +01:00
|
|
|
const podcast = query.podcast == 1
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
if (!query.title || typeof query.title !== 'string') {
|
|
|
|
Logger.error(`[SearchController] findCovers: Invalid title sent in query`)
|
2022-11-20 23:12:30 +01:00
|
|
|
return res.sendStatus(400)
|
|
|
|
}
|
2022-11-19 20:28:06 +01:00
|
|
|
|
2022-12-13 00:45:51 +01:00
|
|
|
let results = null
|
2023-09-04 23:33:55 +02:00
|
|
|
if (podcast) results = await PodcastFinder.findCovers(query.title)
|
2023-10-03 00:09:12 +02:00
|
|
|
else results = await BookFinder.findCovers(query.provider || 'google', query.title, query.author || '')
|
2022-11-29 19:23:02 +01:00
|
|
|
res.json({
|
2022-12-13 00:45:51 +01:00
|
|
|
results
|
2022-11-29 19:23:02 +01:00
|
|
|
})
|
2022-11-19 20:28:06 +01:00
|
|
|
}
|
|
|
|
|
2023-12-17 18:18:21 +01:00
|
|
|
/**
|
2024-08-11 22:15:34 +02:00
|
|
|
* GET: /api/search/podcasts
|
2023-12-17 18:18:21 +01:00
|
|
|
* Find podcast RSS feeds given a term
|
2024-06-09 20:43:03 +02:00
|
|
|
*
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-12-17 18:18:21 +01:00
|
|
|
*/
|
2022-11-19 20:28:06 +01:00
|
|
|
async findPodcasts(req, res) {
|
2022-12-13 00:45:51 +01:00
|
|
|
const term = req.query.term
|
2024-02-17 20:24:49 +01:00
|
|
|
const country = req.query.country || 'us'
|
2023-12-17 18:18:21 +01:00
|
|
|
if (!term) {
|
|
|
|
Logger.error('[SearchController] Invalid request query param "term" is required')
|
|
|
|
return res.status(400).send('Invalid request query param "term" is required')
|
|
|
|
}
|
|
|
|
|
2024-01-05 07:45:35 +01:00
|
|
|
const results = await PodcastFinder.search(term, {
|
2024-02-17 20:24:49 +01:00
|
|
|
country
|
2024-01-05 07:45:35 +01:00
|
|
|
})
|
2022-12-13 00:45:51 +01:00
|
|
|
res.json(results)
|
2022-11-19 20:28:06 +01:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/search/authors
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-11-19 20:28:06 +01:00
|
|
|
async findAuthor(req, res) {
|
2022-12-13 00:45:51 +01:00
|
|
|
const query = req.query.q
|
2024-08-11 22:15:34 +02:00
|
|
|
if (!query || typeof query !== 'string') {
|
|
|
|
Logger.error(`[SearchController] findAuthor: Invalid query param`)
|
|
|
|
return res.status(400).send('Invalid query param')
|
|
|
|
}
|
|
|
|
|
2023-09-04 23:33:55 +02:00
|
|
|
const author = await AuthorFinder.findAuthorByName(query)
|
2022-11-19 20:28:06 +01:00
|
|
|
res.json(author)
|
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/search/chapters
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-11-19 20:28:06 +01:00
|
|
|
async findChapters(req, res) {
|
2022-12-13 00:45:51 +01:00
|
|
|
const asin = req.query.asin
|
2024-06-09 20:43:03 +02:00
|
|
|
if (!isValidASIN(asin.toUpperCase())) {
|
|
|
|
return res.json({ error: 'Invalid ASIN' })
|
|
|
|
}
|
2022-12-13 00:45:51 +01:00
|
|
|
const region = (req.query.region || 'us').toLowerCase()
|
2023-09-04 23:33:55 +02:00
|
|
|
const chapterData = await BookFinder.findChapters(asin, region)
|
2022-11-19 20:28:06 +01:00
|
|
|
if (!chapterData) {
|
|
|
|
return res.json({ error: 'Chapters not found' })
|
|
|
|
}
|
|
|
|
res.json(chapterData)
|
|
|
|
}
|
|
|
|
}
|
2024-06-09 20:43:03 +02:00
|
|
|
module.exports = new SearchController()
|