my pkgs monorepo
at main 48 lines 1.5 kB view raw
1/** 2 * Spotify JSON parsing — environment-agnostic. 3 * No Node.js deps; file I/O is the caller's responsibility. 4 */ 5 6import type { SpotifyRecord, PlayRecord } from './types.js'; 7import { RECORD_TYPE } from './config.js'; 8 9export type { SpotifyRecord }; 10 11/** 12 * Filter raw Spotify records, keeping only music tracks (not podcasts). 13 */ 14export function parseSpotifyJsonContent(records: SpotifyRecord[]): SpotifyRecord[] { 15 return records.filter( 16 (r) => r.master_metadata_track_name && r.master_metadata_album_artist_name 17 ); 18} 19 20/** 21 * Convert a Spotify record to an ATProto play record. 22 * 23 * @param clientAgent The `submissionClientAgent` string for this runtime. 24 */ 25export function convertSpotifyToPlayRecord(r: SpotifyRecord, clientAgent: string): PlayRecord { 26 const artists: PlayRecord['artists'] = []; 27 if (r.master_metadata_album_artist_name) { 28 artists.push({ artistName: r.master_metadata_album_artist_name }); 29 } 30 31 const record: PlayRecord = { 32 $type: RECORD_TYPE, 33 trackName: r.master_metadata_track_name ?? 'Unknown Track', 34 artists, 35 playedTime: r.ts, 36 submissionClientAgent: clientAgent, 37 musicServiceBaseDomain: 'spotify.com', 38 originUrl: '', 39 }; 40 41 if (r.master_metadata_album_album_name) record.releaseName = r.master_metadata_album_album_name; 42 if (r.spotify_track_uri) { 43 const id = r.spotify_track_uri.replace('spotify:track:', ''); 44 record.originUrl = `https://open.spotify.com/track/${id}`; 45 } 46 47 return record; 48}