|
| 1 | +import { getImageScore, logMarian, delay } from "../shared/utils.js"; |
| 2 | + |
| 3 | +async function getLibroDetails() { |
| 4 | + logMarian("Extracting Libro details"); |
| 5 | + const bookDetails = {}; |
| 6 | + |
| 7 | + const imggrab = document.querySelector('.audiobook-cover .book-cover-wrap img.book-cover'); |
| 8 | + bookDetails["img"] = imggrab?.src; |
| 9 | + bookDetails["imgScore"] = imggrab?.src ? await getImageScore(imggrab.src) : 0; |
| 10 | + |
| 11 | + // Title |
| 12 | + getLibroBookTitle(bookDetails); |
| 13 | + |
| 14 | + // Series name and number |
| 15 | + getLibroSeries(bookDetails); |
| 16 | + |
| 17 | + // Contributors |
| 18 | + extractLibroContributors(bookDetails); |
| 19 | + |
| 20 | + //get format and length |
| 21 | + getLibroFormatInfo(bookDetails, window.location.href) |
| 22 | + |
| 23 | + // get extra block of info - isbn, language, etc. |
| 24 | + extraLibroInfo(bookDetails); |
| 25 | + |
| 26 | + // Description |
| 27 | + extractLibroDescription(bookDetails); |
| 28 | + |
| 29 | + logMarian("Libro extraction complete:", bookDetails); |
| 30 | + return { |
| 31 | + ...bookDetails, |
| 32 | + }; |
| 33 | + |
| 34 | +} |
| 35 | + |
| 36 | +function extractLibroContributors(bookDetails) { |
| 37 | + const contributions = {} |
| 38 | + |
| 39 | + const section = extractSection('audiobook details') |
| 40 | + const authors = section.querySelectorAll('span[itemprop="author"] a') |
| 41 | + authors.forEach(author => { |
| 42 | + const name = author.textContent.trim() |
| 43 | + if (!(name in contributions)) { |
| 44 | + contributions[name] = [] |
| 45 | + } |
| 46 | + contributions[name].push("Author") |
| 47 | + }) |
| 48 | + |
| 49 | + const narrators = section.querySelectorAll('a[href$="searchby=narrators"]') |
| 50 | + narrators.forEach(narrator => { |
| 51 | + const name = narrator.textContent.trim() |
| 52 | + if (!(name in contributions)) { |
| 53 | + contributions[name] = [] |
| 54 | + } |
| 55 | + contributions[name].push("Narrator") |
| 56 | + }) |
| 57 | + |
| 58 | + let contributors = [] |
| 59 | + for (let [name, roles] of Object.entries(contributions)) { |
| 60 | + contributors.push({ name, roles }) |
| 61 | + } |
| 62 | + if (contributors.length) { |
| 63 | + bookDetails["Contributors"] = contributors; |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +function getLibroSeries(bookDetails) { |
| 68 | + const seriesName = document.querySelector('.audiobook-title__series a'); |
| 69 | + if (seriesName) { |
| 70 | + let name = seriesName.textContent.trim(); |
| 71 | + bookDetails['Series'] = name; |
| 72 | + let seriesPlace = extractTextNode(document.querySelector('.audiobook-title__series')); |
| 73 | + let number = seriesPlace.match(/\d+/); |
| 74 | + if (number) { |
| 75 | + bookDetails['Series Place'] = number[0]; |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +function getLibroBookTitle(bookDetails) { |
| 81 | + const h1 = document.querySelector('h1.audiobook-title'); |
| 82 | + const rawTitle = h1?.childNodes[0]?.textContent.trim(); |
| 83 | + rawTitle ? bookDetails["Title"] = rawTitle : null; |
| 84 | +} |
| 85 | + |
| 86 | +function joinContent(elements) { |
| 87 | + return Array.from(elements) |
| 88 | + // libro.fm uses <br> tags instead of <p> tags for paragraphs, so have to use innerText |
| 89 | + .map(item => item.innerText.trim()) |
| 90 | + // split by newlines so that everything isn't on one line |
| 91 | + .flatMap(item => item.split('\n')) |
| 92 | + // strip out empty lines (there are some random empty <p> tags) |
| 93 | + .filter(item => item.length > 0) |
| 94 | + .join("\n"); |
| 95 | +} |
| 96 | + |
| 97 | +function extractTextNode(element) { |
| 98 | + return Array.from(element?.childNodes || []) |
| 99 | + .filter(n => n.nodeType == Node.TEXT_NODE) |
| 100 | + .map(n => n.textContent.trim()) |
| 101 | + .join("\n") |
| 102 | + .trim(); |
| 103 | +} |
| 104 | + |
| 105 | +function extractSection(title) { |
| 106 | + const sections = document.querySelectorAll('section') |
| 107 | + return Array.from(sections) |
| 108 | + .find(section => section.querySelector('h2')?.textContent.trim().toLowerCase() == title) |
| 109 | +} |
| 110 | + |
| 111 | +function getLibroFormatInfo(bookDetails) { |
| 112 | + bookDetails['Reading Format'] = 'Audiobook'; |
| 113 | + const informationSections = document.querySelectorAll(".audiobook-information .audiobook-information__section"); |
| 114 | + |
| 115 | + const audioLength = extractTextNode( |
| 116 | + Array.from(informationSections) |
| 117 | + .find(section => section.querySelector("strong")?.textContent.trim().toLowerCase() == 'length') |
| 118 | + ); |
| 119 | + |
| 120 | + // split the length by number boundary |
| 121 | + const lengthParts = audioLength.split(/ (?=\d+)/); |
| 122 | + |
| 123 | + bookDetails['Listening Length'] = lengthParts; |
| 124 | + |
| 125 | +} |
| 126 | + |
| 127 | +function extractLibroDescription(bookDetails) { |
| 128 | + const summaryEl = extractSection('summary'); |
| 129 | + // if there is a tab for more information about the authors, it's a different element |
| 130 | + const summaryTabEl = document.querySelector('#panel_summary') |
| 131 | + const element = summaryEl || summaryTabEl; |
| 132 | + if (element) { |
| 133 | + const summary = joinContent(element.querySelectorAll('p')) |
| 134 | + bookDetails["Description"] = summary; |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | + |
| 139 | +function extraLibroInfo(bookDetails) { |
| 140 | + const section = extractSection('audiobook details') |
| 141 | + const publisher = section.querySelector('span[itemprop="publisher"]') |
| 142 | + if (publisher) { |
| 143 | + bookDetails['Publisher'] = publisher.textContent.trim(); |
| 144 | + } |
| 145 | + const releaseDate = section.querySelector('span[itemprop="datePublished"]') |
| 146 | + if (releaseDate) { |
| 147 | + bookDetails['Publication date'] = releaseDate.textContent.trim(); |
| 148 | + } |
| 149 | + const language = section.querySelector('span[itemprop="inLanguage"]') |
| 150 | + if (language) { |
| 151 | + bookDetails['Language'] = language.textContent.trim(); |
| 152 | + } |
| 153 | + const isbn = section.querySelector('span[itemprop="isbn"]') |
| 154 | + if (isbn) { |
| 155 | + const isbnText = isbn.textContent.trim() |
| 156 | + if (isbnText.length == 13) { |
| 157 | + bookDetails['ISBN-13'] = isbnText; |
| 158 | + } else if (isbn.length == 10) { |
| 159 | + bookDetails['ISBN-10'] = isbnText; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + // no nice itemprop attribute for edition type :( |
| 164 | + const cells = section.querySelectorAll('.cell') |
| 165 | + // try to find the relevant cell with the 'Edition' header |
| 166 | + const editionCell = Array.from(cells) |
| 167 | + .find(cell => cell.querySelector('strong')?.textContent.trim().toLowerCase() == 'edition'); |
| 168 | + if (editionCell) { |
| 169 | + let editionFormat = editionCell.querySelector('span')?.textContent.trim() |
| 170 | + bookDetails['Edition Format'] = editionFormat; |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +export { getLibroDetails }; |
0 commit comments