Skip to content

Commit bdc5dca

Browse files
Merge pull request #13 from kyle-seongwoo-jun/fix/mac-mini
fix: update for renewed website of mac mini
2 parents 6f0e245 + 000c1c0 commit bdc5dca

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

scripts/scrape-from-apple.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,27 @@ function parseOldPage(document: HTMLDocument): Device[] {
6565
function parseNewPage(document: HTMLDocument): Device[] {
6666
const MODEL_IDENTIFIER = 'Model Identifier: '
6767

68-
const names = [...document.querySelectorAll('p.gb-paragraph b')].map(b => (b as Element).innerText)
68+
const names = [...document.querySelectorAll('p.gb-paragraph b')].map(b => (b as Element).innerText.trim())
6969
const ids = [...document.querySelectorAll('p.gb-paragraph')].filter(p => (p as Element).innerText.startsWith(MODEL_IDENTIFIER)).map(p => (p as Element).innerText.replace(MODEL_IDENTIFIER, ''))
7070

7171
if (names.length !== ids.length) {
7272
throw new Error('names and ids are not matched')
7373
}
7474

75-
const devices: Device[] = []
76-
names.forEach((name, i) => {
77-
const id = ids[i]
78-
devices.push({ id, name })
79-
})
75+
const devices = names.map((name, i) => {
76+
let id = ids[i]
77+
78+
// Apple might have temporarily miswritten the document, as currently, on https://support.apple.com/en-us/102852,
79+
// there is no line break between Model Identifier and Part Numbers in the description of the Mac mini (2023) model.
80+
// Therefore, a separate handling for this model has been added
81+
// (planned to be removed when the document is updated).
82+
if (id.includes('Part Numbers:')) {
83+
id = id.split('Part Numbers:')[0].trim()
84+
}
85+
86+
// some devices have multiple identifiers
87+
return id.split('; ').map(id => ({ id, name }))
88+
}).flat()
8089

8190
return devices
8291
}

0 commit comments

Comments
 (0)