From 21270bc86aa89c7cdf020668f0c8fa560ee6158d Mon Sep 17 00:00:00 2001 From: Michael Herger Date: Sun, 20 Sep 2020 14:13:50 +0200 Subject: [PATCH] When importing online libraries, and there's more than one account per service, store the account used to import a track in the track object's comment. --- Slim/Plugin/OnlineLibraryBase.pm | 18 +++++++++++++++++- Slim/Plugin/WiMP/Importer.pm | 4 +++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Slim/Plugin/OnlineLibraryBase.pm b/Slim/Plugin/OnlineLibraryBase.pm index 7d110696a5f..1a5cf2f0293 100644 --- a/Slim/Plugin/OnlineLibraryBase.pm +++ b/Slim/Plugin/OnlineLibraryBase.pm @@ -46,6 +46,11 @@ sub initOnlineTracksTable { if (main::SCANNER && !$main::wipe) { $dbh->do(qq{ CREATE TEMPORARY TABLE online_tracks (url TEXT PRIMARY KEY); }); + + $dbh->do('DROP TABLE IF EXISTS online_mapping'); + $dbh->do(qq{ + CREATE TABLE online_mapping (id INTEGER PRIMARY KEY, account char(128)); + }); } } sub deleteRemovedTracks { if (main::SCANNER && !$main::wipe) { @@ -108,11 +113,13 @@ sub isImportEnabled { } sub storeTracks { - my ($class, $tracks, $libraryId) = @_; + my ($class, $tracks, $libraryId, $accountId) = @_; return unless $tracks && ref $tracks; my $dbh = Slim::Schema->dbh(); + my $checkComment_sth = $dbh->prepare_cached("SELECT id FROM comments WHERE track = ? AND value = ?") if $accountId; + my $insertAccountInComment_sth = $dbh->prepare_cached("INSERT OR IGNORE INTO comments (track, value) VALUES (?, ?)") if $accountId; my $insertTrackInLibrary_sth = $dbh->prepare_cached("INSERT OR IGNORE INTO library_track (library, track) VALUES (?, ?)") if $libraryId; my $insertTrackInTempTable_sth = $dbh->prepare_cached("INSERT OR IGNORE INTO online_tracks (url) VALUES (?)") if main::SCANNER && !$main::wipe; @@ -127,6 +134,15 @@ sub storeTracks { integrateRemote => 1, }); + if ($checkComment_sth) { + $checkComment_sth->execute($trackObj->id, $accountId); + my $data = $checkComment_sth->fetchall_arrayref([0]); + + if (!$data || !ref $data || !scalar @$data) { + $insertAccountInComment_sth->execute($trackObj->id, $accountId); + } + } + if ($insertTrackInLibrary_sth) { $insertTrackInLibrary_sth->execute($libraryId, $trackObj->id); } diff --git a/Slim/Plugin/WiMP/Importer.pm b/Slim/Plugin/WiMP/Importer.pm index 8e37d84de7d..b96d59ae459 100644 --- a/Slim/Plugin/WiMP/Importer.pm +++ b/Slim/Plugin/WiMP/Importer.pm @@ -89,6 +89,8 @@ sub scanAlbums { if (main::SCANNER) { main::INFOLOG && $log->is_info && $log->info("Reading albums for $account..."); $progress->update(string('PLUGIN_TIDAL_PROGRESS_READ_ALBUMS', $account)); + my $accountName = "tidal:$account" if scalar @$accounts > 1; + my $albumsResponse = $http->get(Slim::Networking::SqueezeNetwork::Sync->url(sprintf(ALBUMS_URL, $account))); my $albums = eval { from_json($albumsResponse->content) } || []; @@ -111,7 +113,7 @@ sub scanAlbums { if (main::SCANNER) { $class->storeTracks([ map { _prepareTrack($_, $album) } @$tracks - ]); + ], undef, $accountName); } Slim::Schema->forceCommit;