Skip to content

Commit

Permalink
feat: datetime format added as option
Browse files Browse the repository at this point in the history
  • Loading branch information
rombat committed Apr 6, 2024
1 parent 5dfce3d commit 92761ff
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Syncs playcounts, track ratings, loved tracks and last played date from MusicBee

* `-f, --first` : runs sync for the first time: **add** MusicBee playcount to Navidrome playcount. If not used, playcount will be updated only if greater than Navidrome's one (see [Notes](#-notes)).
* `--csv <path>` : MusicBee CSV source file path. By default if not passed, will look for a file named `MusicBee_Export.csv` in the same folder as `musicbee-navidrome-sync.exe`
* `--datetime-format <format>` : MusicBee CSV datetime format. Default: `"DD/MM/YYYY HH:mm"`. Use available formats from https://day.js.org/docs/en/display/format`


### albumsSync

Expand Down
24 changes: 17 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,17 @@ const commandLinesOptions = {
flags: '-u, --user <user_name>',
description: 'choose Navidrome username (by default if not used, the first user will be used)'
},
datetimeFormat: {
flags: '--datetime-format <format>',
description: 'MusicBee CSV datetime format. Default: "DD/MM/YYYY HH:mm"',
defaultValue: 'DD/MM/YYYY HH:mm'
},
verbose: {
flags: '--verbose',
description: 'verbose debugging'
}
};

program
.name('musicbee-navidrome-sync')
.description(
'MusicBee to Navidrome Sync (MBNDS) : Tools to sync MusicBee DB to Navidrome DB\nhttps://github.com/rombat/musicbee-navidrome-sync'
)
.version(packageJson.version, '-v, --version', 'output the current version');

program
.command('fullSync')
.description('sync playcounts, track ratings, loved tracks and last played from MusicBee DB to Navidrome DB')
Expand All @@ -43,8 +41,20 @@ program
.option(commandLinesOptions.verbose.flags, commandLinesOptions.verbose.description)
.option(commandLinesOptions.csv.flags, commandLinesOptions.description, commandLinesOptions.defaultValue)
.option(commandLinesOptions.db.flags, commandLinesOptions.db.description, commandLinesOptions.db.defaultValue)
.option(
commandLinesOptions.datetimeFormat.flags,
commandLinesOptions.datetimeFormat.description,
commandLinesOptions.datetimeFormat.defaultValue
)
.action(runAction);

program
.name('musicbee-navidrome-sync')
.description(
'MusicBee to Navidrome Sync (MBNDS) : Tools to sync MusicBee DB to Navidrome DB\nhttps://github.com/rombat/musicbee-navidrome-sync'
)
.version(packageJson.version, '-v, --version', 'output the current version');

program
.command('albumsSync')
.description('update all albums playcounts and ratings based on existing Navidrome DB')
Expand Down
12 changes: 9 additions & 3 deletions lib/handlers/MBNDSynchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ class MBNDSynchronizer {
throw new Error('DB file not found');
}

if (options.datetimeFormat && !dayjs(dayjs().format(options.datetimeFormat), options.datetimeFormat).isValid()) {
throw new Error(
`Invalid datetime format : ${options.datetimeFormat}. Please use available formats from https://day.js.org/docs/en/display/format`
);
}

this.backupDbFile();

this.sequelize = await dbManager.init(paths.dbFilePath);
Expand Down Expand Up @@ -163,9 +169,9 @@ class MBNDSynchronizer {
}
return rating;
},
dateAdded: item => (!!item?.trim() && dayjs(item).isValid() ? dayjs(item).utc() : null),
lastPlayed: item => (!!item?.trim() && dayjs(item).isValid() ? dayjs(item).utc() : null),
dateModified: item => (!!item?.trim() && dayjs(item).isValid() ? dayjs(item).utc() : null),
dateAdded: item => (dayjs(item, options.datetimeFormat).isValid() ? dayjs(item, options.datetimeFormat).utc() : null),
lastPlayed: item => (dayjs(item, options.datetimeFormat).isValid() ? dayjs(item, options.datetimeFormat).utc() : null),
dateModified: item => (dayjs(item, options.datetimeFormat).isValid() ? dayjs(item, options.datetimeFormat).utc() : null),
love: item => (!!item?.trim() ? 1 : 0)
}
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "musicbee-navidrome-sync",
"version": "1.0.7",
"version": "1.1.0",
"description": "sync ratings and playcount from musicbee db to navidrome db",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 92761ff

Please sign in to comment.