2022-11-20 23:12:30 +01: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')
|
2023-12-08 23:33:06 +01:00
|
|
|
const Database = require("../Database")
|
2022-11-20 23:12:30 +01:00
|
|
|
|
2022-11-19 20:28:06 +01:00
|
|
|
class SearchController {
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
async findPodcasts(req, res) {
|
2022-12-13 00:45:51 +01:00
|
|
|
const term = req.query.term
|
2023-09-04 23:33:55 +02:00
|
|
|
const results = await PodcastFinder.search(term)
|
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
|
|
|
|
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
|
|
|
}
|
|
|
|
module.exports = new SearchController()
|