{"size": 942, "ext": "js", "max_stars_repo_path": "lib/v2/spotify/saved.js", "max_stars_repo_name": "magicLaLa/RSSHub", "max_stars_repo_stars_event_min_datetime": "2018-04-02T14:57:10.000Z", "max_stars_repo_stars_event_max_datetime": "2018-04-06T13:02:50.000Z", "max_issues_repo_path": "lib/v2/spotify/saved.js", "max_issues_repo_name": "magicLaLa/RSSHub", "max_issues_repo_issues_event_min_datetime": "2021-11-26T08:27:02.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T21:34:27.000Z", "max_forks_repo_path": "lib/v2/spotify/saved.js", "max_forks_repo_name": "magicLaLa/RSSHub", "max_forks_repo_forks_event_min_datetime": "2020-05-10T04:04:21.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-10T04:04:21.000Z"}
{"max_stars_count": 16, "max_issues_count": 274, "max_forks_count": 1, "avg_line_length": 30.3870967742, "max_line_length": 72, "alphanum_fraction": 0.5626326964}
const utils = require('./utils');
const got = require('@/utils/got');
const { parseDate } = require('@/utils/parse-date');

module.exports = async (ctx) => {
    const token = await utils.getPrivateToken();

    const { limit } = ctx.params;
    const pageSize = !isNaN(parseInt(limit)) ? parseInt(limit) : 50;

    const itemsResponse = await got
        .get(`https://api.spotify.com/v1/me/tracks?limit=${pageSize}`, {
            headers: {
                Authorization: `Bearer ${token}`,
            },
        })
        .json();
    const tracks = itemsResponse.items;

    ctx.state.data = {
        title: 'Spotify: My Saved Tracks',
        link: 'https://open.spotify.com/collection/tracks',
        description: `Latest ${pageSize} saved tracks on Spotify.`,
        allowEmpty: true,
        item: tracks.map((x) => ({
            ...utils.parseTrack(x.track),
            pubDate: parseDate(x.added_at),
        })),
    };
};
