Skip to content

Instantly share code, notes, and snippets.

@munierujp
Last active May 4, 2022 01:19
Show Gist options
  • Save munierujp/2d773f29186e0d8ea66f6d66c152d232 to your computer and use it in GitHub Desktop.
Save munierujp/2d773f29186e0d8ea66f6d66c152d232 to your computer and use it in GitHub Desktop.
(function (copy) {
main()
async function main () {
const rowElements = getRowElements()
console.log(`${rowElements.length}件のアイテムを取得開始します。`)
const items = await Promise.all(rowElements.map(async (rowElement) => {
const item = await toItem(rowElement)
console.log(`Done: ${item.title}`)
return item
}))
console.log(`${rowElements.length}件中${items.length}件のアイテムを取得しました。`)
const lines = items.map(item => {
const { title, author, purchaseDate, asin, originalASIN} = item
const line = `${title}\t${author}\t${purchaseDate}\t${asin}`
return originalASIN ? `${line}\t${originalASIN}` : line
})
const tsv = lines.join('\n')
copy(tsv)
const first = items[0]
const last = items[lines.length - 1]
console.log(`${lines.length}行のTSVをコピーしました(『${first.title}』から『${last.title}』まで)。`)
}
function getRowElements () {
return Array.from(document.querySelectorAll('.contentTableListRow_myx li'))
}
async function toItem (rowElement) {
const getCellValue = key => rowElement.querySelector(`[bo-text="tab.${key}"]`).textContent.trim()
const title = getCellValue('title')
const author = getCellValue('author')
const purchaseDate = getCellValue('purchaseDate')
const asin = rowElement.querySelector('[id^="contentTabList_"]').getAttribute('name').replace(/^contentTabList_/, '')
const item = { title, author, purchaseDate, asin}
const originalASIN = await fetchOriginalASIN(asin)
return originalASIN ? { ...item, originalASIN} : item
}
async function fetchOriginalASIN (asin) {
const originalASINFromAmazon = await fetchOriginalASINFromAmazon(asin)
if (originalASINFromAmazon) {
return originalASINFromAmazon
}
const originalASINFromBooklog = await fetchOriginalASINFromBooklog(asin)
return originalASINFromBooklog
}
async function fetchOriginalASINFromAmazon (asin) {
try {
const url = `https://www.amazon.co.jp/dp/${asin}`
const doc = await fetchDOM(url)
const formatsElement = doc.getElementById('formats')
if (!formatsElement) {
return
}
const formatElements = Array.from(formatsElement.querySelectorAll('.format'))
const formatElement = formatElements.find(formatElement => {
const text = formatElement.textContent
return !text.includes('Kindle') && !text.includes('Audible')
})
if (!formatElement) {
return
}
const path = formatElement.querySelector('.a-button-text').getAttribute('href')
const originalASIN = path.match(/^(.+)\/dp\/(.+)\/.+/)[2]
return originalASIN
} catch (ignored) {
return
}
}
async function fetchOriginalASINFromBooklog (asin) {
try {
const url = `https://booklog.jp/item/1/${asin}`
const doc = await fetchDOM(url)
const altItemElements = doc.querySelectorAll('.alt-item')
if (!altItemElements.length) {
return
}
const altItemElement = altItemElements[0]
const path = altItemElement.querySelector('a').getAttribute('href')
const originalASIN = path.match(/^\/item\/1\/(.+)/)[1]
return originalASIN
} catch (ignored) {
return
}
}
async function fetchDOM (url) {
const resp = await fetch(url)
const body = await resp.text()
const doc = await new DOMParser().parseFromString(body, 'text/html')
return doc
}
})(copy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment