feat: only download in prod; fix bug if dealing with sub-1 hour of tracks
This commit is contained in:
parent
ade92f7ec1
commit
341221c78c
2 changed files with 26 additions and 8 deletions
12
src/_data/json/mocks/music.json
Normal file
12
src/_data/json/mocks/music.json
Normal 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
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,6 +2,7 @@ const { S3Client, GetObjectCommand, PutObjectCommand } = require('@aws-sdk/clien
|
||||||
const _ = require('lodash')
|
const _ = require('lodash')
|
||||||
const artistAliases = require('./json/artist-aliases.json')
|
const artistAliases = require('./json/artist-aliases.json')
|
||||||
const artistGenres = require('./json/artist-genres.json')
|
const artistGenres = require('./json/artist-genres.json')
|
||||||
|
const mockedMusic = require('./json/mocks/music.json')
|
||||||
const titleCaseExceptions = require('./json/title-case-exceptions.json')
|
const titleCaseExceptions = require('./json/title-case-exceptions.json')
|
||||||
const { getReadableData } = require('../utils/aws')
|
const { getReadableData } = require('../utils/aws')
|
||||||
const { getKeyByValue } = require('../utils/arrays')
|
const { getKeyByValue } = require('../utils/arrays')
|
||||||
|
@ -62,6 +63,7 @@ const getTracksOneHour = (tracks) => {
|
||||||
let trackTimer = 0
|
let trackTimer = 0
|
||||||
|
|
||||||
while (trackTimer < TIMER_CEILING) {
|
while (trackTimer < TIMER_CEILING) {
|
||||||
|
if (!tracks[trackIndex]) return tracksOneHour
|
||||||
trackTimer = trackTimer + parseInt(tracks[trackIndex].duration)
|
trackTimer = trackTimer + parseInt(tracks[trackIndex].duration)
|
||||||
tracksOneHour.push(tracks[trackIndex])
|
tracksOneHour.push(tracks[trackIndex])
|
||||||
trackIndex++
|
trackIndex++
|
||||||
|
@ -186,6 +188,7 @@ module.exports = async function () {
|
||||||
let CURRENT_PAGE = 0
|
let CURRENT_PAGE = 0
|
||||||
let trackTimer = 0
|
let trackTimer = 0
|
||||||
let res = []
|
let res = []
|
||||||
|
let cachedTracks = mockedMusic
|
||||||
|
|
||||||
while (trackTimer < TIMER_CEILING) {
|
while (trackTimer < TIMER_CEILING) {
|
||||||
const URL = `https://api.music.apple.com/v1/me/recent/played/tracks?limit=${PAGE_SIZE}&offset=${
|
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++
|
CURRENT_PAGE++
|
||||||
}
|
}
|
||||||
|
|
||||||
const cachedTracksOutput = await client.send(
|
if (process.env.ELEVENTY_PRODUCTION === 'true') {
|
||||||
new GetObjectCommand({
|
const cachedTracksOutput = await client.send(
|
||||||
Bucket: WASABI_BUCKET,
|
new GetObjectCommand({
|
||||||
Key: 'music.json',
|
Bucket: WASABI_BUCKET,
|
||||||
})
|
Key: 'music.json',
|
||||||
)
|
})
|
||||||
const cachedTracksData = getReadableData(cachedTracksOutput.Body)
|
)
|
||||||
const cachedTracks = await cachedTracksData.then((tracks) => JSON.parse(tracks)).catch()
|
const cachedTracksData = getReadableData(cachedTracksOutput.Body)
|
||||||
|
cachedTracks = await cachedTracksData.then((tracks) => JSON.parse(tracks)).catch()
|
||||||
|
}
|
||||||
|
|
||||||
const diffedTracks = diffTracks(cachedTracks, formatTracks(res))
|
const diffedTracks = diffTracks(cachedTracks, formatTracks(res))
|
||||||
const updatedCache = {
|
const updatedCache = {
|
||||||
...cachedTracks,
|
...cachedTracks,
|
||||||
|
|
Reference in a new issue