/*
* @title プレイリスト取得(Tune My Music互換)
* @description Amazon MusicのプレイリストをTune My 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: 'music_title', name: 'Track name',},
{key: 'artists', name: 'Artist name',},
{key: 'album_title', name: 'Album',},
{key: 'playlist_name', name: 'Playlist name',},
{key: 'list_type', name: 'Type',},
{key: 'isrc', name: 'ISRC',},
{key: 'asin', name: 'Amazon - id',},
],
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_name = 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'),
list_type = 'Playlist',
isrc = '',
asin = (music_url.match(/trackAsin=([A-Z0-9]+)/) ?? [0,''])[1];
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 : '',
list_type,
playlist_name,
isrc,
asin,
};
}),
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_name}.csv`, csv_lines);
})();