From 341221c78c7991158c036622ad12683a9d835b85 Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Fri, 21 Jul 2023 14:44:39 -0700 Subject: [PATCH] feat: only download in prod; fix bug if dealing with sub-1 hour of tracks --- src/_data/json/mocks/music.json | 12 ++++++++++++ src/_data/music.js | 22 ++++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 src/_data/json/mocks/music.json diff --git a/src/_data/json/mocks/music.json b/src/_data/json/mocks/music.json new file mode 100644 index 00000000..0ccb0334 --- /dev/null +++ b/src/_data/json/mocks/music.json @@ -0,0 +1,12 @@ +{ + "i.rXXXdmUa6Nme-1689970612847": { + "name": "Sacrificial Blood Oath In The Temple Of K'zadu", + "artist": "Gateway", + "album": "Galgendood", + "art": "https://store-033.blobstore.apple.com/sq-mq-us-033-000002/18/f1/a3/18f1a37a-8c9a-169a-5458-464aea20ce05/image?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Date=20230721T202228Z&X-Amz-SignedHeaders=host&X-Amz-Expires=86400&X-Amz-Credential=MKIAU0HKO2RBEAT0UMZS%2F20230721%2Fstore-033%2Fs3%2Faws4_request&X-Amz-Signature=85790600221880597074559ed3674564f17ca3df6634d6fa15496baf7aca5d56", + "url": "https://rateyourmusic.com/search?searchtype=l&searchterm=Galgendood%20Gateway", + "id": "i.rXXXdmUa6Nme", + "playTime": 1689970612847, + "duration": 338808 + } +} diff --git a/src/_data/music.js b/src/_data/music.js index fa0c27ec..feb0704d 100644 --- a/src/_data/music.js +++ b/src/_data/music.js @@ -2,6 +2,7 @@ const { S3Client, GetObjectCommand, PutObjectCommand } = require('@aws-sdk/clien const _ = require('lodash') const artistAliases = require('./json/artist-aliases.json') const artistGenres = require('./json/artist-genres.json') +const mockedMusic = require('./json/mocks/music.json') const titleCaseExceptions = require('./json/title-case-exceptions.json') const { getReadableData } = require('../utils/aws') const { getKeyByValue } = require('../utils/arrays') @@ -62,6 +63,7 @@ const getTracksOneHour = (tracks) => { let trackTimer = 0 while (trackTimer < TIMER_CEILING) { + if (!tracks[trackIndex]) return tracksOneHour trackTimer = trackTimer + parseInt(tracks[trackIndex].duration) tracksOneHour.push(tracks[trackIndex]) trackIndex++ @@ -186,6 +188,7 @@ module.exports = async function () { let CURRENT_PAGE = 0 let trackTimer = 0 let res = [] + let cachedTracks = mockedMusic while (trackTimer < TIMER_CEILING) { const URL = `https://api.music.apple.com/v1/me/recent/played/tracks?limit=${PAGE_SIZE}&offset=${ @@ -209,14 +212,17 @@ module.exports = async function () { CURRENT_PAGE++ } - const cachedTracksOutput = await client.send( - new GetObjectCommand({ - Bucket: WASABI_BUCKET, - Key: 'music.json', - }) - ) - const cachedTracksData = getReadableData(cachedTracksOutput.Body) - const cachedTracks = await cachedTracksData.then((tracks) => JSON.parse(tracks)).catch() + if (process.env.ELEVENTY_PRODUCTION === 'true') { + const cachedTracksOutput = await client.send( + new GetObjectCommand({ + Bucket: WASABI_BUCKET, + Key: 'music.json', + }) + ) + const cachedTracksData = getReadableData(cachedTracksOutput.Body) + cachedTracks = await cachedTracksData.then((tracks) => JSON.parse(tracks)).catch() + } + const diffedTracks = diffTracks(cachedTracks, formatTracks(res)) const updatedCache = { ...cachedTracks,