mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-01-21 05:19:16 +01:00
Separated individual element parsing functions out of function getBookDataFromDir
This commit is contained in:
parent
6ff66370fe
commit
dd664da871
@ -212,17 +212,38 @@ function getBookDataFromDir(folderPath, relPath, parseSubtitle = false) {
|
|||||||
relPath = relPath.replace(/\\/g, '/')
|
relPath = relPath.replace(/\\/g, '/')
|
||||||
var splitDir = relPath.split('/')
|
var splitDir = relPath.split('/')
|
||||||
|
|
||||||
// Audio files will always be in the directory named for the title
|
var title = splitDir.pop() // Audio files will always be in the directory named for the title
|
||||||
var [title, narrators] = getTitleAndNarrator(splitDir.pop())
|
series = (splitDir.length > 1) ? splitDir.pop() : null // If there are at least 2 more directories, next furthest will be the series
|
||||||
|
author = (splitDir.length > 0) ? splitDir.pop() : null // There could be many more directories, but only the top 3 are used for naming /author/series/title/
|
||||||
|
|
||||||
var series = null
|
// The title directory may contain various other pieces of metadata, these functions extract it.
|
||||||
var author = null
|
var [title, narrators] = getNarrator(title)
|
||||||
// If there are at least 2 more directories, next furthest will be the series
|
if (series) { var [series, title, sequence] = getSeries(series, title) }
|
||||||
if (splitDir.length > 1) series = splitDir.pop()
|
var [title, publishedYear] = getPublishedYear(title)
|
||||||
if (splitDir.length > 0) author = splitDir.pop()
|
if (parseSubtitle) { var [title, subtitle] = getSubtitle(title) }
|
||||||
// There could be many more directories, but only the top 3 are used for naming /author/series/title/
|
|
||||||
|
|
||||||
|
return {
|
||||||
|
mediaMetadata: {
|
||||||
|
author,
|
||||||
|
title,
|
||||||
|
subtitle,
|
||||||
|
series,
|
||||||
|
sequence,
|
||||||
|
publishedYear,
|
||||||
|
narrators,
|
||||||
|
},
|
||||||
|
relPath: relPath, // relative audiobook path i.e. /Author Name/Book Name/..
|
||||||
|
path: Path.posix.join(folderPath, relPath) // i.e. /audiobook/Author Name/Book Name/..
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getNarrator(folder) {
|
||||||
|
let pattern = /^(?<title>.*)\{(?<narrators>.*)\} *$/
|
||||||
|
let match = folder.match(pattern)
|
||||||
|
return match ? [match.groups.title.trimEnd(), match.groups.narrators] : [folder, null]
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSeries(series, title) {
|
||||||
// If in a series directory check for volume number match
|
// If in a series directory check for volume number match
|
||||||
/* ACCEPTS
|
/* ACCEPTS
|
||||||
Book 2 - Title Here - Subtitle Here
|
Book 2 - Title Here - Subtitle Here
|
||||||
@ -236,7 +257,7 @@ function getBookDataFromDir(folderPath, relPath, parseSubtitle = false) {
|
|||||||
0.5 - Book Title
|
0.5 - Book Title
|
||||||
*/
|
*/
|
||||||
var volumeNumber = null
|
var volumeNumber = null
|
||||||
if (series) {
|
|
||||||
// Added 1.7.1: If title starts with a # that is 3 digits or less (or w/ 2 decimal), then use as volume number
|
// Added 1.7.1: If title starts with a # that is 3 digits or less (or w/ 2 decimal), then use as volume number
|
||||||
var volumeMatch = title.match(/^(\d{1,3}(?:\.\d{1,2})?) - ./)
|
var volumeMatch = title.match(/^(\d{1,3}(?:\.\d{1,2})?) - ./)
|
||||||
if (volumeMatch && volumeMatch.length > 1) {
|
if (volumeMatch && volumeMatch.length > 1) {
|
||||||
@ -261,8 +282,22 @@ function getBookDataFromDir(folderPath, relPath, parseSubtitle = false) {
|
|||||||
title = title.replace(replaceChunk, '').trim()
|
title = title.replace(replaceChunk, '').trim()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [series, title, volumeNumber]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSubtitle(title) {
|
||||||
|
// Subtitle can be parsed from the title if user enabled
|
||||||
|
// Subtitle is everything after " - "
|
||||||
|
var subtitle = null
|
||||||
|
if (title.includes(' - ')) {
|
||||||
|
var splitOnSubtitle = title.split(' - ')
|
||||||
|
title = splitOnSubtitle.shift()
|
||||||
|
subtitle = splitOnSubtitle.join(' - ')
|
||||||
|
}
|
||||||
|
return [title, subtitle]
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPublishedYear(title) {
|
||||||
var publishedYear = null
|
var publishedYear = null
|
||||||
// If Title is of format 1999 OR (1999) - Title, then use 1999 as publish year
|
// If Title is of format 1999 OR (1999) - Title, then use 1999 as publish year
|
||||||
var publishYearMatch = title.match(/^(\(?[0-9]{4}\)?) - (.+)/)
|
var publishYearMatch = title.match(/^(\(?[0-9]{4}\)?) - (.+)/)
|
||||||
@ -276,35 +311,7 @@ function getBookDataFromDir(folderPath, relPath, parseSubtitle = false) {
|
|||||||
title = publishYearMatch[2]
|
title = publishYearMatch[2]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return [title, publishedYear]
|
||||||
// Subtitle can be parsed from the title if user enabled
|
|
||||||
// Subtitle is everything after " - "
|
|
||||||
var subtitle = null
|
|
||||||
if (parseSubtitle && title.includes(' - ')) {
|
|
||||||
var splitOnSubtitle = title.split(' - ')
|
|
||||||
title = splitOnSubtitle.shift()
|
|
||||||
subtitle = splitOnSubtitle.join(' - ')
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
mediaMetadata: {
|
|
||||||
author,
|
|
||||||
title,
|
|
||||||
subtitle,
|
|
||||||
series,
|
|
||||||
sequence: volumeNumber,
|
|
||||||
publishedYear,
|
|
||||||
narrators,
|
|
||||||
},
|
|
||||||
relPath: relPath, // relative audiobook path i.e. /Author Name/Book Name/..
|
|
||||||
path: Path.posix.join(folderPath, relPath) // i.e. /audiobook/Author Name/Book Name/..
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getTitleAndNarrator(folder) {
|
|
||||||
let pattern = /^(?<title>.*)\{(?<narrators>.*)\} *$/
|
|
||||||
let match = folder.match(pattern)
|
|
||||||
return match ? [match.groups.title.trimEnd(), match.groups.narrators] : [folder, null]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPodcastDataFromDir(folderPath, relPath) {
|
function getPodcastDataFromDir(folderPath, relPath) {
|
||||||
|
Loading…
Reference in New Issue
Block a user