feat: only download in prod; fix bug if dealing with sub-1 hour of tracks

This commit is contained in:
Cory Dransfeldt 2023-07-21 14:44:39 -07:00
parent ade92f7ec1
commit 341221c78c
No known key found for this signature in database
2 changed files with 26 additions and 8 deletions

View file

@ -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
}
}

View file

@ -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,