-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate verify timestamps from missing music
- Loading branch information
Showing
6 changed files
with
164 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,52 @@ | ||
import getClient from './services/getClient.ts'; | ||
import getUserLikes from './services/getUserLikes.ts'; | ||
import getMissingMusic from './services/getMissingMusic.ts'; | ||
import verifyTimestamps from './services/verifyTimestamps.ts'; | ||
import logger from './helpers/logger.ts'; | ||
import { SoundCloudSyncOptions } from './types.ts'; | ||
|
||
export { getClient, getUserLikes, getMissingMusic }; | ||
export { getClient, getUserLikes, getMissingMusic, verifyTimestamps }; | ||
|
||
export default async function soundCloudSync({ | ||
username, | ||
folder = './music', | ||
limit = 50, | ||
verifyTimestamps = false, | ||
verifyTimestamps: shouldVerifyTimestamps = false, | ||
}: SoundCloudSyncOptions) { | ||
logger.info(`Getting latest likes for ${username}`); | ||
|
||
try { | ||
const client = await getClient(username); | ||
const userLikes = await getUserLikes(client, '0', limit); | ||
const results = await getMissingMusic( | ||
userLikes, | ||
folder, | ||
{ | ||
onDownloadStart: track => logger.info('Downloading track', track.title), | ||
onDownloadComplete: track => logger.info('Added track', track.title), | ||
onDownloadError: (track, error) => | ||
logger.error('Failed to download track', { | ||
title: track.title, | ||
error: error instanceof Error ? error.message : String(error), | ||
}), | ||
onTimestampUpdate: (track, oldDate, newDate) => | ||
logger.info( | ||
`Updated timestamp for ${track.title} from ${oldDate.toISOString()} to ${newDate.toISOString()}`, | ||
), | ||
}, | ||
verifyTimestamps, | ||
|
||
const callbacks = { | ||
onDownloadStart: track => logger.info(`Downloading "${track.title}"`), | ||
onDownloadComplete: track => logger.info(`Added "${track.title}"`), | ||
onDownloadError: (track, error) => | ||
logger.error( | ||
`Failed to download "${track.title}": ${ | ||
error instanceof Error ? error.message : String(error) | ||
}`, | ||
), | ||
onTimestampUpdate: (track, oldDate, newDate) => | ||
logger.info( | ||
`Updated timestamp for ${track.title}" from ${oldDate.toISOString()} to ${newDate.toISOString()}`, | ||
), | ||
}; | ||
|
||
let verifyResultsLength = 0; | ||
if (shouldVerifyTimestamps) { | ||
({ length: verifyResultsLength } = await verifyTimestamps(userLikes, folder, callbacks)); | ||
} | ||
|
||
const downloadResults = await getMissingMusic(userLikes, folder, callbacks); | ||
logger.info( | ||
`Completed successfully: ${downloadResults.length} tracks downloaded${ | ||
shouldVerifyTimestamps ? `, ${verifyResultsLength} tracks verified` : '' | ||
}`, | ||
); | ||
logger.info(`Completed successfully, ${results.length} tracks processed`); | ||
} catch (error) { | ||
logger.error('An error occurred', { | ||
error: error instanceof Error ? error.message : String(error), | ||
}); | ||
logger.error(`An error occurred: ${error instanceof Error ? error.message : String(error)}`); | ||
throw error; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
import { Track, Callbacks, UserLike, VerifyTimestampResult } from '../types.ts'; | ||
import logger from '../helpers/logger.ts'; | ||
import sanitiseFilename from '../helpers/sanitise.ts'; | ||
|
||
const verifyAndUpdateTimestamp = async ( | ||
filePath: string, | ||
created_at: string, | ||
track: Track, | ||
callbacks: Callbacks, | ||
): Promise<VerifyTimestampResult> => { | ||
try { | ||
const stats = await fs.stat(filePath); | ||
const likeDate = new Date(created_at); | ||
const fileDate = stats.mtime; | ||
|
||
if (Math.abs(likeDate.getTime() - fileDate.getTime()) > 1000) { | ||
// 1 second tolerance | ||
logger.debug( | ||
`Verifying timestamp for "${path.basename(filePath)}" from ${fileDate.toISOString()} to ${likeDate.toISOString()}`, | ||
); | ||
await fs.utimes(filePath, likeDate, likeDate); | ||
callbacks.onTimestampUpdate?.(track, fileDate, likeDate); | ||
return { | ||
track: track.title, | ||
status: { success: true, updated: true }, | ||
}; | ||
} | ||
return { | ||
track: track.title, | ||
status: { success: true, updated: false }, | ||
}; | ||
} catch (error) { | ||
logger.error( | ||
`Failed to verify/update timestamp for "${path.basename(filePath)}": ${ | ||
error instanceof Error ? error.message : String(error) | ||
}`, | ||
); | ||
return { | ||
track: track.title, | ||
status: { | ||
success: false, | ||
updated: false, | ||
error: error instanceof Error ? error.message : String(error), | ||
}, | ||
}; | ||
} | ||
}; | ||
|
||
export default async function verifyTimestamps( | ||
likes: UserLike[], | ||
folder: string, | ||
callbacks: Callbacks = {}, | ||
): Promise<VerifyTimestampResult[]> { | ||
const availableMusic = (await fs.readdir(folder)).map(filename => path.parse(filename).name); | ||
|
||
const existingTracks = likes.filter(({ track }) => | ||
availableMusic.includes(sanitiseFilename(track.title)), | ||
); | ||
|
||
return Promise.all( | ||
existingTracks.map(async ({ track, created_at }) => { | ||
const filePath = path.join(folder, `${sanitiseFilename(track.title)}.mp3`); | ||
return verifyAndUpdateTimestamp(filePath, created_at, track, callbacks); | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters