diff --git a/src/backend/scrobblers/LastfmScrobbler.ts b/src/backend/scrobblers/LastfmScrobbler.ts index 5e5b6307..1da49ade 100644 --- a/src/backend/scrobblers/LastfmScrobbler.ts +++ b/src/backend/scrobblers/LastfmScrobbler.ts @@ -47,16 +47,17 @@ export default class LastfmScrobbler extends AbstractScrobbleClient { } getScrobblesForRefresh = async (limit: number) => { - const resp = await this.api.callApi((client: any) => client.userGetRecentTracks({ - user: this.api.user, - sk: this.api.client.sessionKey, - limit: limit, - extended: true - })); + const resp = await this.api.callApi((client: any) => client.userGetRecentTracks({ + user: this.api.user, + sk: this.api.client.sessionKey, + limit: limit, + extended: true + })); + try { const { recenttracks: { track: list = [], - } + } = {} } = resp; return list.reduce((acc: any, x: any) => { try { @@ -71,24 +72,28 @@ export default class LastfmScrobbler extends AbstractScrobbleClient { nowPlaying, } } = formatted; - if(nowPlaying === true) { + if (nowPlaying === true) { // if the track is "now playing" it doesn't get a timestamp so we can't determine when it started playing // and don't want to accidentally count the same track at different timestamps by artificially assigning it 'now' as a timestamp // so we'll just ignore it in the context of recent tracks since really we only want "tracks that have already finished being played" anyway - this.logger.debug("Ignoring 'now playing' track returned from Last.fm client", {track, mbid}); + this.logger.debug("Ignoring 'now playing' track returned from Last.fm client", { track, mbid }); return acc; - } else if(playDate === undefined) { - this.logger.warn(`Last.fm recently scrobbled track did not contain a timestamp, omitting from time frame check`, {track, mbid}); + } else if (playDate === undefined) { + this.logger.warn(`Last.fm recently scrobbled track did not contain a timestamp, omitting from time frame check`, { track, mbid }); return acc; } return acc.concat(formatted); } catch (e) { - this.logger.warn('Failed to format Last.fm recently scrobbled track, omitting from time frame check', {error: e.message}); + this.logger.warn('Failed to format Last.fm recently scrobbled track, omitting from time frame check', { error: e.message }); this.logger.debug('Full api response object:'); this.logger.debug(x); return acc; } }, []); + } catch (e) { + this.logger.debug(resp); + throw e; + } } cleanSourceSearchTitle = (playObj: PlayObject) => { diff --git a/src/backend/sources/LastfmSource.ts b/src/backend/sources/LastfmSource.ts index b3fc46d0..4bc7aadf 100644 --- a/src/backend/sources/LastfmSource.ts +++ b/src/backend/sources/LastfmSource.ts @@ -80,47 +80,52 @@ export default class LastfmSource extends MemorySource { limit, extended: true })); - const { - recenttracks: { - track: list = [], - } - } = resp; + try { + const { + recenttracks: { + track: list = [], + } = {} + } = resp; - const plays = list.reduce((acc: PlayObject[], x: TrackObject) => { - try { - const formatted = LastfmApiClient.formatPlayObj(x); - const { - data: { - track, - playDate, - }, - meta: { - mbid, - nowPlaying, - } - } = formatted; - if(playDate === undefined) { - if(nowPlaying === true) { - formatted.data.playDate = dayjs(); - return acc.concat(formatted); + const plays = list.reduce((acc: PlayObject[], x: TrackObject) => { + try { + const formatted = LastfmApiClient.formatPlayObj(x); + const { + data: { + track, + playDate, + }, + meta: { + mbid, + nowPlaying, + } + } = formatted; + if (playDate === undefined) { + if (nowPlaying === true) { + formatted.data.playDate = dayjs(); + return acc.concat(formatted); + } + this.logger.warn(`Last.fm recently scrobbled track did not contain a timestamp, omitting from time frame check`, { track, mbid }); + return acc; } - this.logger.warn(`Last.fm recently scrobbled track did not contain a timestamp, omitting from time frame check`, {track, mbid}); + return acc.concat(formatted); + } catch (e) { + this.logger.warn('Failed to format Last.fm recently scrobbled track, omitting from time frame check', { error: e.message }); + this.logger.debug('Full api response object:'); + this.logger.debug(x); return acc; } - return acc.concat(formatted); - } catch (e) { - this.logger.warn('Failed to format Last.fm recently scrobbled track, omitting from time frame check', {error: e.message}); - this.logger.debug('Full api response object:'); - this.logger.debug(x); - return acc; - } - }, []).sort(sortByOldestPlayDate); - // if the track is "now playing" it doesn't get a timestamp so we can't determine when it started playing - // and don't want to accidentally count the same track at different timestamps by artificially assigning it 'now' as a timestamp - // so we'll just ignore it in the context of recent tracks since really we only want "tracks that have already finished being played" anyway - const history = plays.filter(x => x.meta.nowPlaying !== true); - const now = plays.filter(x => x.meta.nowPlaying === true); - return [history, now]; + }, []).sort(sortByOldestPlayDate); + // if the track is "now playing" it doesn't get a timestamp so we can't determine when it started playing + // and don't want to accidentally count the same track at different timestamps by artificially assigning it 'now' as a timestamp + // so we'll just ignore it in the context of recent tracks since really we only want "tracks that have already finished being played" anyway + const history = plays.filter(x => x.meta.nowPlaying !== true); + const now = plays.filter(x => x.meta.nowPlaying === true); + return [history, now]; + } catch (e) { + this.logger.debug(resp); + throw e; + } } getRecentlyPlayed = async(options: RecentlyPlayedOptions = {}): Promise => {