Beispiel #1
0
        private async Task GetPlaylistItems(Action <PlaylistItemData, InsertStatus> callback, PlaylistData playlist)
        {
            var playlistItems = new List <PlaylistItemData>();

            await foreach (var playlistItem in _youTubeApi.GetPlaylistItems(playlist.Id, RemovePlaylist))
            {
                playlistItems.Add(playlistItem);
                var result = await _youTubeCleanupToolDbContextFactory.Create().UpsertPlaylistItem(playlistItem);

                callback(playlistItem, result);
            }

            // Now we have to remove playlist items we didn't get back from the API - Otherwise if we delete + add items then we end up with duplicates
            var originalPlaylistItems = await _youTubeCleanupToolDbContextFactory.Create().GetPlaylistItems(playlist.Id);

            var playlistItemsHashSet = new HashSet <string>(playlistItems.Select(x => x.Id).ToList());

            foreach (var playlistItem in originalPlaylistItems)
            {
                if (!playlistItemsHashSet.Contains(playlistItem.Id))
                {
                    _youTubeCleanupToolDbContextFactory.Create().RemovePlaylistItem(playlistItem);
                    callback(playlistItem, InsertStatus.Deleted);
                }
            }
        }
Beispiel #2
0
        private async Task GetPlaylistItems(IYouTubeCleanupToolDbContext context, Func <IData, InsertStatus, CancellationToken, Task> callback, PlaylistData playlist, CancellationToken cancellationToken)
        {
            var playlistItems = new List <PlaylistItemData>();

            await foreach (var playlistItem in _youTubeApi.GetPlaylistItems(playlist.Id, RemovePlaylist).WithCancellation(cancellationToken))
            {
                playlistItems.Add(playlistItem);
                var result = await context.UpsertPlaylistItem(playlistItem);
                await callback(playlistItem, result, cancellationToken);

                cancellationToken.ThrowIfCancellationRequested();
            }

            // Now we have to remove playlist items we didn't get back from the API - Otherwise if we delete + add items then we end up with duplicates
            var originalPlaylistItems = await context.GetPlaylistItems(playlist.Id);

            var playlistItemsHashSet = new HashSet <string>(playlistItems.Select(x => x.Id).ToList());

            foreach (var playlistItem in originalPlaylistItems)
            {
                if (!playlistItemsHashSet.Contains(playlistItem.Id))
                {
                    context.RemovePlaylistItem(playlistItem);
                    await callback(playlistItem, InsertStatus.Deleted, cancellationToken);

                    cancellationToken.ThrowIfCancellationRequested();
                }
            }
        }
Beispiel #3
0
        public async Task GetPlaylistItemsForPlaylist(Action <PlaylistItemData, InsertStatus> callback, PlaylistData playlist)
        {
            await GetPlaylistItems(callback, playlist);

            await _youTubeCleanupToolDbContextFactory.Create().SaveChangesAsync();
        }
Beispiel #4
0
        public async Task GetPlaylistItemsForPlaylist(Func <IData, InsertStatus, CancellationToken, Task> callback, PlaylistData playlist, CancellationToken cancellationToken)
        {
            var context = _youTubeCleanupToolDbContextFactory.Create();

            await GetPlaylistItems(context, callback, playlist, cancellationToken);

            await context.SaveChangesAsync(cancellationToken);
        }