From 510d38c059086bde35bc1a2afa68603ee4d8c12f Mon Sep 17 00:00:00 2001 From: chme Date: Fri, 6 Jan 2017 11:08:47 +0100 Subject: [PATCH] [library/filescanner/spotify] Rework full-rescan logic --- src/filescanner.c | 6 +--- src/library.c | 34 +++++++++++++++++++-- src/spotify.c | 77 +++++++++++++++++++++++++++++++++-------------- 3 files changed, 87 insertions(+), 30 deletions(-) diff --git a/src/filescanner.c b/src/filescanner.c index 34cd9169..8e7be450 100644 --- a/src/filescanner.c +++ b/src/filescanner.c @@ -1482,13 +1482,9 @@ filescanner_fullrescan() { DPRINTF(E_LOG, L_SCAN, "Full rescan triggered\n"); - player_playback_stop(); - db_queue_clear(); inofd_event_unset(); // Clears all inotify watches - db_purge_all(); // Clears files, playlists, playlistitems, inotify and groups - - inofd_event_set(); bulk_scan(F_SCAN_BULK); + inofd_event_set(); return 0; } diff --git a/src/library.c b/src/library.c index e60776eb..1cbb6176 100644 --- a/src/library.c +++ b/src/library.c @@ -43,6 +43,7 @@ #include "db.h" #include "logger.h" #include "misc.h" +#include "player.h" static struct commands_base *cmdbase; @@ -564,12 +565,21 @@ rescan(void *arg, int *ret) time_t endtime; int i; + DPRINTF(E_LOG, L_LIB, "Library rescan triggered\n"); + starttime = time(NULL); for (i = 0; sources[i]; i++) { if (!sources[i]->disabled && sources[i]->rescan) - sources[i]->rescan(); + { + DPRINTF(E_INFO, L_LIB, "Rescan library source '%s'\n", sources[i]->name); + sources[i]->rescan(); + } + else + { + DPRINTF(E_INFO, L_LIB, "Library source '%s' is disabled\n", sources[i]->name); + } } purge_cruft(starttime); @@ -585,14 +595,34 @@ rescan(void *arg, int *ret) static enum command_state fullrescan(void *arg, int *ret) { + time_t starttime; + time_t endtime; int i; + DPRINTF(E_LOG, L_LIB, "Library full-rescan triggered\n"); + + starttime = time(NULL); + + player_playback_stop(); + db_queue_clear(); + db_purge_all(); // Clears files, playlists, playlistitems, inotify and groups + for (i = 0; sources[i]; i++) { if (!sources[i]->disabled && sources[i]->fullrescan) - sources[i]->fullrescan(); + { + DPRINTF(E_INFO, L_LIB, "Full-rescan library source '%s'\n", sources[i]->name); + sources[i]->fullrescan(); + } + else + { + DPRINTF(E_INFO, L_LIB, "Library source '%s' is disabled\n", sources[i]->name); + } } + endtime = time(NULL); + DPRINTF(E_LOG, L_LIB, "Library full-rescan completed in %.f sec\n", difftime(endtime, starttime)); + scanning = false; *ret = 0; return COMMAND_END; diff --git a/src/spotify.c b/src/spotify.c index 0781b534..d8cea6e0 100644 --- a/src/spotify.c +++ b/src/spotify.c @@ -2180,8 +2180,6 @@ scan_saved_albums() int i; int ret; - db_transaction_begin(); - memset(&request, 0, sizeof(struct spotify_request)); while (0 == spotifywebapi_request_next(&request, SPOTIFY_WEBAPI_SAVED_ALBUMS)) @@ -2191,6 +2189,8 @@ scan_saved_albums() DPRINTF(E_DBG, L_SPOTIFY, "Got saved album: '%s' - '%s' (%s) - track-count: %d\n", album.artist, album.name, album.uri, track_count); + db_transaction_begin(); + dir_id = prepare_directories(album.artist, album.name); ret = 0; for (i = 0; i < track_count && ret == 0; i++) @@ -2213,13 +2213,13 @@ scan_saved_albums() db_pl_add_item_bypath(spotify_saved_plid, track.uri); } } + + db_transaction_end(); } } spotifywebapi_request_end(&request); - db_transaction_end(); - return 0; } @@ -2274,8 +2274,6 @@ scan_playlists() char virtual_path[PATH_MAX]; int plid; - db_transaction_begin(); - memset(&request, 0, sizeof(struct spotify_request)); while (0 == spotifywebapi_request_next(&request, SPOTIFY_WEBAPI_SAVED_PLAYLISTS)) @@ -2284,6 +2282,8 @@ scan_playlists() { DPRINTF(E_DBG, L_SPOTIFY, "Got playlist: '%s' (%s) \n", playlist.name, playlist.uri); + db_transaction_begin(); + if (playlist.owner) { snprintf(virtual_path, PATH_MAX, "/spotify:/%s (%s)", playlist.name, playlist.owner); @@ -2299,11 +2299,12 @@ scan_playlists() scan_playlisttracks(&playlist, plid); else DPRINTF(E_LOG, L_SPOTIFY, "Error adding playlist: '%s' (%s) \n", playlist.name, playlist.uri); + + db_transaction_end(); } } spotifywebapi_request_end(&request); - db_transaction_end(); return 0; } @@ -2317,14 +2318,14 @@ scan_playlist(const char *uri) char virtual_path[PATH_MAX]; int plid; - db_transaction_begin(); - memset(&request, 0, sizeof(struct spotify_request)); if (0 == spotifywebapi_playlist_start(&request, uri, &playlist)) { DPRINTF(E_DBG, L_SPOTIFY, "Got playlist: '%s' (%s) \n", playlist.name, playlist.uri); + db_transaction_begin(); + if (playlist.owner) { snprintf(virtual_path, PATH_MAX, "/spotify:/%s (%s)", playlist.name, playlist.owner); @@ -2340,10 +2341,11 @@ scan_playlist(const char *uri) scan_playlisttracks(&playlist, plid); else DPRINTF(E_LOG, L_SPOTIFY, "Error adding playlist: '%s' (%s) \n", playlist.name, playlist.uri); + + db_transaction_end(); } spotifywebapi_request_end(&request); - db_transaction_end(); return 0; } @@ -2360,13 +2362,28 @@ create_saved_tracks_playlist() } } -/* Thread: library */ -static int -initscan() +static void +create_base_playlist() { cfg_t *spotify_cfg; int ret; + spotify_base_plid = 0; + spotify_cfg = cfg_getsec(cfg, "spotify"); + if (!cfg_getbool(spotify_cfg, "base_playlist_disable")) + { + ret = library_add_playlist_info("spotify:playlistfolder", "Spotify", NULL, PL_FOLDER, 0, 0); + if (ret < 0) + DPRINTF(E_LOG, L_SPOTIFY, "Error adding base playlist\n"); + else + spotify_base_plid = ret; + } +} + +/* Thread: library */ +static int +initscan() +{ scanning = true; /* Refresh access token for the spotify webapi */ @@ -2383,17 +2400,9 @@ initscan() /* * Add playlist folder for all spotify playlists */ - spotify_base_plid = 0; + create_base_playlist(); + spotify_saved_plid = 0; - spotify_cfg = cfg_getsec(cfg, "spotify"); - if (! cfg_getbool(spotify_cfg, "base_playlist_disable")) - { - ret = library_add_playlist_info("spotify:playlistfolder", "Spotify", NULL, PL_FOLDER, 0, 0); - if (ret < 0) - DPRINTF(E_LOG, L_SPOTIFY, "Error adding base playlist\n"); - else - spotify_base_plid = ret; - } /* * Login to spotify needs to be done before scanning tracks from the web api. @@ -2422,6 +2431,8 @@ rescan() { scanning = true; + create_base_playlist(); + /* * Scan saved tracks from the web api */ @@ -2449,6 +2460,26 @@ rescan() static int fullrescan() { + scanning = true; + + create_base_playlist(); + + /* + * Scan saved tracks from the web api + */ + if (spotify_access_token_valid) + { + create_saved_tracks_playlist(); + scan_saved_albums(); + scan_playlists(); + } + else + { + spotify_login(NULL); + } + + scanning = false; + return 0; }