Skip to content

Instantly share code, notes, and snippets.

@pastak
Created August 2, 2021 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pastak/7a4dbc035216984a23d1f5f6fd1a186a to your computer and use it in GitHub Desktop.
Save pastak/7a4dbc035216984a23d1f5f6fd1a186a to your computer and use it in GitHub Desktop.
/**
* https://www.amazon.co.jp/gp/css/order-history で実行
* TSVの中身は順に
* 注文ID, 購入日, タイトル, URL, ASIN, (空白), 価格
*/
// https://stackoverflow.com/questions/25735677/remove-duplicates-values-in-array-google-apps-script/25740182#25740182
function removeDups(array) {
const outArray = [];
array.sort(lowerCase);
function lowerCase(a,b){
return a[0].toLowerCase()>b[0].toLowerCase() ? 1 : -1;// sort function that does not "see" letter case
}
outArray.push(array[0]);
for(let n in array){
if(outArray[outArray.length-1][0].toLowerCase()!=array[n][0].toLowerCase()){
outArray.push(array[n]);
}
}
return outArray;
}
function parseURLQuery (url) {
return (new URL(url)).searchParams;
}
const AMAZON_URL_REGEXP = /^https?:\/\/www\.amazon\.co\.jp\/dp\/([\W\w\d]+)\//;
let result = [];
for(const element of document.querySelectorAll('.js-order-card')) {
const date = element.querySelector('.order-info > .a-box-inner .a-col-left > .a-row > .a-column:nth-child(1) > .a-row.a-size-base').textContent.replace(/\s|\n/g,'').replace(/(\d+)年(\d+)月(\d+)日/, '$2/$3/$1 0:0:0')
const id = element.querySelector('.order-info > .a-box-inner .a-col-right > .a-row bdi').textContent
const isDigital = /^D01-/.test(id);
if (!isDigital) continue;
const isKindle = [...element.querySelectorAll('.a-text-bold')].some((elm) => /Kindle/.test(elm.textContent));
if (!isKindle) continue;
const res = await fetch(`/gp/digital/your-account/order-summary.html?ie=UTF8&orderID=${id}`)
const text = await res.text();
const doc = (new DOMParser).parseFromString(text, 'text/html');
const booksInfo = removeDups(
[...doc.querySelectorAll('a')]
.map(function (a) { return [a.href, a] })
.filter(function (a) { return AMAZON_URL_REGEXP.test(a[0]) })
.map(function (a) { return [a[0].replace(/ref=.+$/, ''),a[1]] })
.map(function (a) { return [a[0],a[1], a[0].replace(AMAZON_URL_REGEXP, '$1')] })
.map(function (a) { return [a[0], a[1].textContent, id, date, a[2], '',
// 価格を取得
a[1].parentElement.parentElement.parentElement.querySelector('.a-text-right').textContent.replace(/\s|\n/g,'').replace(/^¥\s*/,'')
] })
);
booksInfo.reverse().forEach(info => {
result.push([
info[2],
info[3],
info[1],
info[0],
info[4],
info[5],
info[6]
].join(`\t`))
})
}
console.log(result.join(`\n`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment