/*
* @title プレイリスト取得
* @description Amazon MusicのプレイリストをCSV形式でダウンロード
* @include https://music.amazon.co.jp/playlists/*
* @include https://music.amazon.co.jp/my/playlists/*
* @license MIT License
* @javascript_url
*/
(() => {
const
headers = [
{key: 'no', name: 'No.',},
{key: 'music_title', name: '曲',},
{key: 'music_url', name: '曲URL',},
{key: 'artists', name: 'アーティスト',},
{key: 'artists_url', name: 'アーティストURL',},
{key: 'album_title', name: 'アルバム',},
{key: 'album_url', name: 'アルバムURL',},
],
create_csv_line = values => values.map(value => `"${('' + value).replace(/"/g, '""')}"` ).join(','),
download_csv = (csv_filename, csv_lines) => {
const
csv_text = csv_lines.join('\r\n'),
bom = new Uint8Array([0xEF, 0xBB, 0xBF]),
blob = new Blob([bom, csv_text], {'type': 'text/csv'}),
blob_url = URL.createObjectURL(blob),
download_link = document.createElement('a');
download_link.href = blob_url;
download_link.download = csv_filename;
document.documentElement.appendChild(download_link);
download_link.click();
download_link.remove();
};
const
header_csv_line = create_csv_line(headers.map(head => head.name)),
playlist_title = document.querySelector('music-detail-header.hydrated[label]').getAttribute('headline'),
music_info_list = [...document.querySelectorAll('music-image-row.hydrated[data-key]')].map((row, index) => {
const
music_title = row.getAttribute('primary-text') ?? '',
music_url = row.getAttribute('primary-href'),
artists = row.getAttribute('secondary-text-1') ?? '',
artists_url = row.getAttribute('secondary-href-1'),
album_title = row.getAttribute('secondary-text-2') ?? '',
album_url = row.getAttribute('secondary-href-2');
return {
no: index+1,
music_title,
music_url: music_url ? new URL(music_url,location.href).href : '',
artists,
artists_url: artists_url ? new URL(artists_url,location.href).href : '',
album_title,
album_url: album_url ? new URL(album_url,location.href).href : '',
};
}),
csv_lines = [header_csv_line].concat(music_info_list.map(music_info => create_csv_line(headers.map(header => music_info[header.key]))));
download_csv(`${playlist_title}.csv`, csv_lines);
})();