[library/filescanner/spotify] Rework full-rescan logic

This commit is contained in:
chme 2017-01-06 11:08:47 +01:00
parent 4e60626749
commit 510d38c059
3 changed files with 87 additions and 30 deletions

View File

@ -1482,13 +1482,9 @@ filescanner_fullrescan()
{ {
DPRINTF(E_LOG, L_SCAN, "Full rescan triggered\n"); DPRINTF(E_LOG, L_SCAN, "Full rescan triggered\n");
player_playback_stop();
db_queue_clear();
inofd_event_unset(); // Clears all inotify watches 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); bulk_scan(F_SCAN_BULK);
inofd_event_set();
return 0; return 0;
} }

View File

@ -43,6 +43,7 @@
#include "db.h" #include "db.h"
#include "logger.h" #include "logger.h"
#include "misc.h" #include "misc.h"
#include "player.h"
static struct commands_base *cmdbase; static struct commands_base *cmdbase;
@ -564,12 +565,21 @@ rescan(void *arg, int *ret)
time_t endtime; time_t endtime;
int i; int i;
DPRINTF(E_LOG, L_LIB, "Library rescan triggered\n");
starttime = time(NULL); starttime = time(NULL);
for (i = 0; sources[i]; i++) for (i = 0; sources[i]; i++)
{ {
if (!sources[i]->disabled && sources[i]->rescan) 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); purge_cruft(starttime);
@ -585,14 +595,34 @@ rescan(void *arg, int *ret)
static enum command_state static enum command_state
fullrescan(void *arg, int *ret) fullrescan(void *arg, int *ret)
{ {
time_t starttime;
time_t endtime;
int i; 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++) for (i = 0; sources[i]; i++)
{ {
if (!sources[i]->disabled && sources[i]->fullrescan) 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; scanning = false;
*ret = 0; *ret = 0;
return COMMAND_END; return COMMAND_END;

View File

@ -2180,8 +2180,6 @@ scan_saved_albums()
int i; int i;
int ret; int ret;
db_transaction_begin();
memset(&request, 0, sizeof(struct spotify_request)); memset(&request, 0, sizeof(struct spotify_request));
while (0 == spotifywebapi_request_next(&request, SPOTIFY_WEBAPI_SAVED_ALBUMS)) 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", DPRINTF(E_DBG, L_SPOTIFY, "Got saved album: '%s' - '%s' (%s) - track-count: %d\n",
album.artist, album.name, album.uri, track_count); album.artist, album.name, album.uri, track_count);
db_transaction_begin();
dir_id = prepare_directories(album.artist, album.name); dir_id = prepare_directories(album.artist, album.name);
ret = 0; ret = 0;
for (i = 0; i < track_count && ret == 0; i++) 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_pl_add_item_bypath(spotify_saved_plid, track.uri);
} }
} }
db_transaction_end();
} }
} }
spotifywebapi_request_end(&request); spotifywebapi_request_end(&request);
db_transaction_end();
return 0; return 0;
} }
@ -2274,8 +2274,6 @@ scan_playlists()
char virtual_path[PATH_MAX]; char virtual_path[PATH_MAX];
int plid; int plid;
db_transaction_begin();
memset(&request, 0, sizeof(struct spotify_request)); memset(&request, 0, sizeof(struct spotify_request));
while (0 == spotifywebapi_request_next(&request, SPOTIFY_WEBAPI_SAVED_PLAYLISTS)) 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); DPRINTF(E_DBG, L_SPOTIFY, "Got playlist: '%s' (%s) \n", playlist.name, playlist.uri);
db_transaction_begin();
if (playlist.owner) if (playlist.owner)
{ {
snprintf(virtual_path, PATH_MAX, "/spotify:/%s (%s)", playlist.name, playlist.owner); snprintf(virtual_path, PATH_MAX, "/spotify:/%s (%s)", playlist.name, playlist.owner);
@ -2299,11 +2299,12 @@ scan_playlists()
scan_playlisttracks(&playlist, plid); scan_playlisttracks(&playlist, plid);
else else
DPRINTF(E_LOG, L_SPOTIFY, "Error adding playlist: '%s' (%s) \n", playlist.name, playlist.uri); DPRINTF(E_LOG, L_SPOTIFY, "Error adding playlist: '%s' (%s) \n", playlist.name, playlist.uri);
db_transaction_end();
} }
} }
spotifywebapi_request_end(&request); spotifywebapi_request_end(&request);
db_transaction_end();
return 0; return 0;
} }
@ -2317,14 +2318,14 @@ scan_playlist(const char *uri)
char virtual_path[PATH_MAX]; char virtual_path[PATH_MAX];
int plid; int plid;
db_transaction_begin();
memset(&request, 0, sizeof(struct spotify_request)); memset(&request, 0, sizeof(struct spotify_request));
if (0 == spotifywebapi_playlist_start(&request, uri, &playlist)) if (0 == spotifywebapi_playlist_start(&request, uri, &playlist))
{ {
DPRINTF(E_DBG, L_SPOTIFY, "Got playlist: '%s' (%s) \n", playlist.name, playlist.uri); DPRINTF(E_DBG, L_SPOTIFY, "Got playlist: '%s' (%s) \n", playlist.name, playlist.uri);
db_transaction_begin();
if (playlist.owner) if (playlist.owner)
{ {
snprintf(virtual_path, PATH_MAX, "/spotify:/%s (%s)", playlist.name, 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); scan_playlisttracks(&playlist, plid);
else else
DPRINTF(E_LOG, L_SPOTIFY, "Error adding playlist: '%s' (%s) \n", playlist.name, playlist.uri); DPRINTF(E_LOG, L_SPOTIFY, "Error adding playlist: '%s' (%s) \n", playlist.name, playlist.uri);
db_transaction_end();
} }
spotifywebapi_request_end(&request); spotifywebapi_request_end(&request);
db_transaction_end();
return 0; return 0;
} }
@ -2360,13 +2362,28 @@ create_saved_tracks_playlist()
} }
} }
/* Thread: library */ static void
static int create_base_playlist()
initscan()
{ {
cfg_t *spotify_cfg; cfg_t *spotify_cfg;
int ret; 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; scanning = true;
/* Refresh access token for the spotify webapi */ /* Refresh access token for the spotify webapi */
@ -2383,17 +2400,9 @@ initscan()
/* /*
* Add playlist folder for all spotify playlists * Add playlist folder for all spotify playlists
*/ */
spotify_base_plid = 0; create_base_playlist();
spotify_saved_plid = 0; 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. * Login to spotify needs to be done before scanning tracks from the web api.
@ -2422,6 +2431,8 @@ rescan()
{ {
scanning = true; scanning = true;
create_base_playlist();
/* /*
* Scan saved tracks from the web api * Scan saved tracks from the web api
*/ */
@ -2449,6 +2460,26 @@ rescan()
static int static int
fullrescan() 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; return 0;
} }