Shows some quick stats about your teal.fm records. Kind of like Spotify Wrapped

Shows records load as it searches them all

Changed files
+77 -72
src
components
+77 -72
src/components/LookUp.vue
··· 54 54 service: doc.service[0].serviceEndpoint as string, 55 55 }); 56 56 let cursor = ""; 57 - let plays = []; 57 + let totalCount = 0; 58 + let inner_tracks: { name: string; artist: string; plays: number }[] = []; 59 + let inner_artists: { name: string; plays: number }[] = []; 60 + 58 61 let response = await agent.com.atproto.repo.listRecords({ 59 62 repo: did, 60 63 collection: "fm.teal.alpha.feed.play", ··· 62 65 cursor: cursor, 63 66 }); 64 67 65 - do { 66 - plays.push(...response.data.records); 67 - cursor = response.data.cursor; 68 + // Process pages incrementally while fetching them 69 + while (true) { 70 + const records = response.data.records ?? []; 71 + totalCount += records.length; 68 72 69 - if (cursor) { 70 - response = await agent.com.atproto.repo.listRecords({ 71 - repo: did, 72 - collection: "fm.teal.alpha.feed.play", 73 - limit: 100, 74 - cursor: cursor, 75 - }); 76 - } 77 - } while (cursor); 73 + for (const play of records) { 74 + // spot-check if play is valid 75 + if ( 76 + play.success == false || 77 + play.value == undefined || 78 + play.value.artistName || 79 + play.value.trackName == undefined 80 + ) { 81 + continue; 82 + } 83 + // new version 78 84 79 - let inner_tracks = []; 80 - let inner_artists = []; 81 - for (const play of plays) { 82 - // spot-check if play is valid 83 - if ( 84 - play.success == false || 85 - play.value == undefined || 86 - play.value.artistName || 87 - play.value.trackName == undefined 88 - ) { 89 - continue; 90 - } 91 - // new version 92 - console.log(play); 93 - if (play.value?.artists) { 94 - for (const artist of play.value?.artists) { 95 - let alreadyPlayed = inner_artists.find( 96 - (a) => a.name === artist, 97 - ); 98 - if (!alreadyPlayed) { 99 - inner_artists.push({ 100 - name: artist.artistName, 101 - plays: 1, 102 - }); 103 - } else { 104 - alreadyPlayed.plays++; 85 + if (play.value?.artists) { 86 + for (const artist of play.value?.artists) { 87 + let alreadyPlayed = inner_artists.find( 88 + (a) => a.name === artist.artistName, 89 + ); 90 + if (!alreadyPlayed) { 91 + inner_artists.push({ 92 + name: artist.artistName, 93 + plays: 1, 94 + }); 95 + } else { 96 + console.log(`Artist already played: ${alreadyPlayed}`); 97 + alreadyPlayed.plays++; 98 + } 99 + } 100 + } else if (play.value?.artistNames) { 101 + // old version 102 + for (const arist of play.value?.artistNames) { 103 + let alreadyPlayed = inner_artists.find( 104 + (a) => a.name === arist, 105 + ); 106 + if (!alreadyPlayed) { 107 + inner_artists.push({ name: arist, plays: 1 }); 108 + } else { 109 + alreadyPlayed.plays++; 110 + } 105 111 } 106 112 } 107 - } else if (play.value?.artistNames) { 108 - // old version 109 - for (const arist of play.value?.artistNames) { 110 - let alreadyPlayed = inner_artists.find( 111 - (a) => a.name === arist, 112 - ); 113 - if (!alreadyPlayed) { 114 - inner_artists.push({ name: arist, plays: 1 }); 115 - } else { 116 - alreadyPlayed.plays++; 117 - } 113 + 114 + let alreadyPlayed = inner_tracks.find( 115 + (a) => a.name === play.value.trackName, 116 + ); 117 + if (!alreadyPlayed && play?.value) { 118 + inner_tracks.push({ 119 + name: play.value.trackName, 120 + artist: play.value?.artists 121 + ? play.value.artists[0].artistName 122 + : play.value.artistNames[0], 123 + plays: 1, 124 + }); 125 + } else if (alreadyPlayed) { 126 + alreadyPlayed.plays++; 118 127 } 119 128 } 120 129 121 - let alreadyPlayed = inner_tracks.find( 122 - (a) => a.name === play.value.trackName, 123 - ); 124 - if (!alreadyPlayed && play?.value) { 125 - inner_tracks.push({ 126 - name: play.value.trackName, 127 - artist: play.value?.artists 128 - ? play.value.artists[0].artistName 129 - : play.value.artistNames[0], 130 - plays: 1, 131 - }); 132 - } else { 133 - alreadyPlayed.plays++; 134 - } 130 + // update reactive values incrementally (top 10) 131 + artists.value = inner_artists 132 + .sort((a, b) => b.plays - a.plays) 133 + .slice(0, 10); 134 + tracks.value = inner_tracks 135 + .sort((a, b) => b.plays - a.plays) 136 + .slice(0, 10); 137 + totalSongs.value = totalCount; 138 + 139 + cursor = response.data.cursor; 140 + if (!cursor) break; 141 + 142 + response = await agent.com.atproto.repo.listRecords({ 143 + repo: did, 144 + collection: "fm.teal.alpha.feed.play", 145 + limit: 100, 146 + cursor: cursor, 147 + }); 135 148 } 136 - 137 - artists.value = inner_artists 138 - .sort((a, b) => b.plays - a.plays) 139 - .slice(0, 10); 140 - tracks.value = inner_tracks 141 - .sort((a, b) => b.plays - a.plays) 142 - .slice(0, 10); 143 - totalSongs.value = plays.length; 144 149 } finally { 145 150 loading.value = false; 146 151 }