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
|
|
|
|
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
|
|
|
|
|
|
|
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 || ''
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if (!query.title) {
|
|
|
|
Logger.error(`[SearchController] findCovers: No title sent in query`)
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Find podcast RSS feeds given a term
|
2024-06-09 20:43:03 +02:00
|
|
|
*
|
|
|
|
* @param {import('express').Request} req
|
|
|
|
* @param {import('express').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
|
|
|
}
|
|
|
|
|
|
|
|
async findAuthor(req, res) {
|
2022-12-13 00:45:51 +01:00
|
|
|
const query = req.query.q
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2023-01-07 20:05:33 +01:00
|
|
|
|
|
|
|
async findMusicTrack(req, res) {
|
2023-09-04 23:33:55 +02:00
|
|
|
const tracks = await MusicFinder.searchTrack(req.query || {})
|
2023-01-07 20:05:33 +01:00
|
|
|
res.json({
|
|
|
|
tracks
|
|
|
|
})
|
|
|
|
}
|
2022-11-19 20:28:06 +01:00
|
|
|
}
|
2024-06-09 20:43:03 +02:00
|
|
|
module.exports = new SearchController()
|