fix: debug caching

This commit is contained in:
Cory Dransfeldt 2023-07-20 13:55:14 -07:00
parent f6b52826a8
commit 7d80a441d7
No known key found for this signature in database

View file

@ -1,9 +1,9 @@
const { S3Client, GetObjectCommand, PutObjectCommand } = require('@aws-sdk/client-s3') const { S3Client, GetObjectCommand, PutObjectCommand } = require('@aws-sdk/client-s3')
const _ = require('lodash') const _ = require('lodash')
const { AssetCache } = require('@11ty/eleventy-fetch')
const artistAliases = require('./json/artist-aliases.json') const artistAliases = require('./json/artist-aliases.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 fs = require('fs')
/** /**
* Accepts a string representing an artist name, checks to see if said artist name * Accepts a string representing an artist name, checks to see if said artist name
@ -55,7 +55,7 @@ const titleCase = (string) => {
} }
const diffTracks = (cache, tracks) => { const diffTracks = (cache, tracks) => {
const trackCompareSet = Object.values(tracks).sort((a, b) => a.time - b.time) const trackCompareSet = Object.values(tracks)
const cacheCompareSet = Object.values(cache).sort((a, b) => a.time - b.time) const cacheCompareSet = Object.values(cache).sort((a, b) => a.time - b.time)
const diffedTracks = {} const diffedTracks = {}
@ -87,8 +87,7 @@ const formatTracks = (tracks, time) => {
const artistFormatted = titleCase(aliasArtist(track.attributes['artistName'])) const artistFormatted = titleCase(aliasArtist(track.attributes['artistName']))
const albumFormatted = titleCase(sanitizeMedia(track.attributes['albumName'])) const albumFormatted = titleCase(sanitizeMedia(track.attributes['albumName']))
const trackFormatted = sanitizeMedia(track.attributes['name']) const trackFormatted = sanitizeMedia(track.attributes['name'])
if (!formattedTracks[track.attributes.name]) { formattedTracks[`${track.id}-${time}`] = {
formattedTracks[track.attributes.name] = {
name: trackFormatted, name: trackFormatted,
artist: artistFormatted, artist: artistFormatted,
album: albumFormatted, album: albumFormatted,
@ -103,9 +102,6 @@ const formatTracks = (tracks, time) => {
time, time,
duration: track.attributes['durationInMillis'], duration: track.attributes['durationInMillis'],
} }
} else {
formattedTracks[track.attributes.name].plays++
}
}) })
return formattedTracks return formattedTracks
} }
@ -177,7 +173,6 @@ module.exports = async function () {
.then((data) => data.json()) .then((data) => data.json())
.catch() .catch()
const APPLE_TOKEN = APPLE_TOKEN_RESPONSE['music-token'] const APPLE_TOKEN = APPLE_TOKEN_RESPONSE['music-token']
const asset = new AssetCache('recent_tracks_data')
const PAGE_SIZE = 30 const PAGE_SIZE = 30
const PAGES = 10 const PAGES = 10
const time = Number(new Date()) const time = Number(new Date())
@ -189,8 +184,6 @@ module.exports = async function () {
let res = [] let res = []
let hasNextPage = true let hasNextPage = true
if (asset.isCacheValid('1h')) return await asset.getCachedValue()
while (CURRENT_PAGE < PAGES && hasNextPage) { while (CURRENT_PAGE < PAGES && hasNextPage) {
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=${
PAGE_SIZE * CURRENT_PAGE PAGE_SIZE * CURRENT_PAGE
@ -240,6 +233,5 @@ module.exports = async function () {
) )
} }
await asset.save(charts, 'json')
return charts return charts
} }