feat: time based fetching
This commit is contained in:
parent
d7ed49e2b6
commit
92e308e541
1 changed files with 33 additions and 29 deletions
|
@ -53,29 +53,26 @@ const titleCase = (string) => {
|
||||||
.join(' ')
|
.join(' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const getTracksOneHour = (tracks) => {
|
||||||
|
const TIMER_CEILING = 3600000 // 1 hour
|
||||||
|
const tracksOneHour = []
|
||||||
|
let trackIndex = 0
|
||||||
|
let trackTimer = 0
|
||||||
|
while (trackTimer < TIMER_CEILING) {
|
||||||
|
trackTimer = trackTimer + parseInt(tracks[trackIndex].duration)
|
||||||
|
tracksOneHour.push(tracks[trackIndex])
|
||||||
|
trackIndex++
|
||||||
|
}
|
||||||
|
|
||||||
|
return tracksOneHour
|
||||||
|
}
|
||||||
|
|
||||||
const diffTracks = (cache, tracks) => {
|
const diffTracks = (cache, tracks) => {
|
||||||
const trackCompareSet = Object.values(tracks)
|
const trackCompareSet = Object.values(tracks)
|
||||||
const cacheCompareSet = _.orderBy(Object.values(cache), ['time'], ['desc'])
|
const cacheCompareSet = _.orderBy(Object.values(cache), ['time'], ['desc'])
|
||||||
const diffedTracks = {}
|
const diffedTracks = {}
|
||||||
|
const cacheCompareOneHour = getTracksOneHour(cacheCompareSet)
|
||||||
const getTracksOneHour = (tracks) => {
|
const comparedTracks = _.differenceWith(trackCompareSet, cacheCompareOneHour, (a, b) =>
|
||||||
const TIMER_CEILING = 3600000 // 1 hour
|
|
||||||
const tracksOneHour = []
|
|
||||||
let trackIndex = 0
|
|
||||||
let trackTimer = 0
|
|
||||||
|
|
||||||
while (trackTimer < TIMER_CEILING) {
|
|
||||||
trackTimer = trackTimer + parseInt(tracks[trackIndex].duration)
|
|
||||||
tracksOneHour.push(tracks[trackIndex])
|
|
||||||
trackIndex++
|
|
||||||
}
|
|
||||||
|
|
||||||
return tracksOneHour
|
|
||||||
}
|
|
||||||
|
|
||||||
const tracksOneHour = getTracksOneHour(trackCompareSet)
|
|
||||||
const cachedTracksOneHour = getTracksOneHour(cacheCompareSet)
|
|
||||||
const comparedTracks = _.differenceWith(tracksOneHour, cachedTracksOneHour, (a, b) =>
|
|
||||||
_.isEqual(a.id, b.id)
|
_.isEqual(a.id, b.id)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -85,8 +82,11 @@ const diffTracks = (cache, tracks) => {
|
||||||
return diffedTracks
|
return diffedTracks
|
||||||
}
|
}
|
||||||
|
|
||||||
const formatTracks = (tracks, time) => {
|
const formatTracks = (tracks) => {
|
||||||
|
const now = new Date().getTime()
|
||||||
let formattedTracks = {}
|
let formattedTracks = {}
|
||||||
|
let time = now
|
||||||
|
|
||||||
Object.values(tracks).forEach((track) => {
|
Object.values(tracks).forEach((track) => {
|
||||||
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']))
|
||||||
|
@ -103,8 +103,9 @@ const formatTracks = (tracks, time) => {
|
||||||
albumFormatted
|
albumFormatted
|
||||||
)}%20${encodeURI(artistFormatted)}`,
|
)}%20${encodeURI(artistFormatted)}`,
|
||||||
id: track.id,
|
id: track.id,
|
||||||
time,
|
time: now,
|
||||||
duration: track.attributes['durationInMillis'],
|
playTime: time - parseInt(track.attributes['durationInMillis']),
|
||||||
|
duration: parseInt(track.attributes['durationInMillis']),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return formattedTracks
|
return formattedTracks
|
||||||
|
@ -178,14 +179,13 @@ module.exports = async function () {
|
||||||
.catch()
|
.catch()
|
||||||
const APPLE_TOKEN = APPLE_TOKEN_RESPONSE['music-token']
|
const APPLE_TOKEN = APPLE_TOKEN_RESPONSE['music-token']
|
||||||
const PAGE_SIZE = 30
|
const PAGE_SIZE = 30
|
||||||
const PAGES = 10
|
const TIMER_CEILING = 3600000 // 1 hour
|
||||||
const time = Number(new Date())
|
|
||||||
let charts
|
let charts
|
||||||
let CURRENT_PAGE = 0
|
let CURRENT_PAGE = 0
|
||||||
|
let trackTimer = 0
|
||||||
let res = []
|
let res = []
|
||||||
let hasNextPage = true
|
|
||||||
|
|
||||||
while (CURRENT_PAGE < PAGES && hasNextPage) {
|
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=${
|
||||||
PAGE_SIZE * CURRENT_PAGE
|
PAGE_SIZE * CURRENT_PAGE
|
||||||
}&include[songs]=albums&extend=artistUrl`
|
}&include[songs]=albums&extend=artistUrl`
|
||||||
|
@ -198,8 +198,12 @@ module.exports = async function () {
|
||||||
})
|
})
|
||||||
.then((data) => data.json())
|
.then((data) => data.json())
|
||||||
.catch()
|
.catch()
|
||||||
if (!tracks.next) hasNextPage = false
|
|
||||||
if (tracks.data.length) res = [...res, ...tracks.data]
|
tracks.data.forEach((track) => {
|
||||||
|
trackTimer = trackTimer + parseInt(track.attributes['durationInMillis'])
|
||||||
|
if (trackTimer >= TIMER_CEILING) return
|
||||||
|
res.push(track)
|
||||||
|
})
|
||||||
CURRENT_PAGE++
|
CURRENT_PAGE++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -211,7 +215,7 @@ module.exports = async function () {
|
||||||
)
|
)
|
||||||
const cachedTracksData = getReadableData(cachedTracksOutput.Body)
|
const cachedTracksData = getReadableData(cachedTracksOutput.Body)
|
||||||
const cachedTracks = await cachedTracksData.then((tracks) => JSON.parse(tracks)).catch()
|
const cachedTracks = await cachedTracksData.then((tracks) => JSON.parse(tracks)).catch()
|
||||||
const diffedTracks = diffTracks(cachedTracks, formatTracks(res, time))
|
const diffedTracks = diffTracks(cachedTracks, formatTracks(res))
|
||||||
const updatedCache = {
|
const updatedCache = {
|
||||||
...cachedTracks,
|
...cachedTracks,
|
||||||
...diffedTracks,
|
...diffedTracks,
|
||||||
|
|
Reference in a new issue