Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse book language from NFO metadata source #3170

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions server/scanner/NfoFileScanner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ const { parseNfoMetadata } = require('../utils/parsers/parseNfoMetadata')
const { readTextFile } = require('../utils/fileUtils')

class NfoFileScanner {
constructor() { }
constructor() {}

/**
* Parse metadata from .nfo file found in library scan and update bookMetadata
*
* @param {import('../models/LibraryItem').LibraryFileObject} nfoLibraryFileObj
* @param {Object} bookMetadata
*
* @param {import('../models/LibraryItem').LibraryFileObject} nfoLibraryFileObj
* @param {Object} bookMetadata
*/
async scanBookNfoFile(nfoLibraryFileObj, bookMetadata) {
const nfoText = await readTextFile(nfoLibraryFileObj.metadata.path)
const nfoMetadata = nfoText ? await parseNfoMetadata(nfoText) : null
if (nfoMetadata) {
for (const key in nfoMetadata) {
if (key === 'tags') { // Add tags only if tags are empty
if (key === 'tags') {
// Add tags only if tags are empty
if (nfoMetadata.tags.length) {
bookMetadata.tags = nfoMetadata.tags
}
} else if (key === 'genres') { // Add genres only if genres are empty
} else if (key === 'genres') {
// Add genres only if genres are empty
if (nfoMetadata.genres.length) {
bookMetadata.genres = nfoMetadata.genres
}
Expand All @@ -33,10 +35,12 @@ class NfoFileScanner {
}
} else if (key === 'series') {
if (nfoMetadata.series) {
bookMetadata.series = [{
name: nfoMetadata.series,
sequence: nfoMetadata.sequence || null
}]
bookMetadata.series = [
{
name: nfoMetadata.series,
sequence: nfoMetadata.sequence || null
}
]
}
} else if (nfoMetadata[key] && key !== 'sequence') {
bookMetadata[key] = nfoMetadata[key]
Expand All @@ -45,4 +49,4 @@ class NfoFileScanner {
}
}
}
module.exports = new NfoFileScanner()
module.exports = new NfoFileScanner()
4 changes: 4 additions & 0 deletions server/utils/parsers/parseNfoMetadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ function parseNfoMetadata(nfoText) {
case 'isbn-13':
metadata.isbn = value
break
case 'language':
case 'lang':
metadata.language = value
break
}
}
})
Expand Down
10 changes: 10 additions & 0 deletions test/server/utils/parsers/parseNfoMetadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ describe('parseNfoMetadata', () => {
expect(result.asin).to.equal('B08X5JZJLH')
})

it('parses language', () => {
const nfoText = 'Language: eng'
const result = parseNfoMetadata(nfoText)
expect(result.language).to.equal('eng')

const nfoText2 = 'lang: deu'
const result2 = parseNfoMetadata(nfoText2)
expect(result2.language).to.equal('deu')
})

it('parses description', () => {
const nfoText = 'Book Description\n=========\nThis is a book.\n It\'s good'
const result = parseNfoMetadata(nfoText)
Expand Down
Loading