예제 #1
0
        public async Task ProcessNewSpotifyAlbums(ILogger logger)
        {
            logger.LogInformation("ProcessNewSpotifyAlbums start");

            //Get all saved Artists
            var allArtistsOutput = await _artistAppService.GetAll(new GetAllArtistsInput { IncludeAlbums = true });

            if (allArtistsOutput.HasError)
            {
                logger.LogError(allArtistsOutput.ErrorMessage);
                return;
            }

            logger.LogInformation("Found {0} saved artists", allArtistsOutput.Artists.Count);

            //An album is only considered a new release if its ReleaseDate was in the last 14 days
            DateTime newReleaseCutoff = DateTime.UtcNow.AddDays(-14);

            foreach (var artist in allArtistsOutput.Artists)
            {
                var artistAlbumsOutput = await _spotifyAppService.GetAlbumsForArtist(new GetAlbumsForArtistInput
                {
                    SpotifyArtistId = artist.SpotifyId
                });

                if (artistAlbumsOutput.HasError)
                {
                    logger.LogError(artistAlbumsOutput.ErrorMessage);
                    //Don't let one artist error stop the entire thing from running, skip and move on to the next
                    continue;
                }

                //If the album came out recently and we don't know about it (ie it wasn't under the Artist.Albums collection),
                //then we want to notify about it
                var newReleaseAlbums = artistAlbumsOutput.Albums
                                       .Where(a => a.ReleaseDateNormalised >= newReleaseCutoff &&
                                              !artist.Albums.Any(al => al.Album.SpotifyId == a.SpotifyId))
                                       // Handle the most recent first
                                       .Reverse()
                                       .ToList();

                if (newReleaseAlbums.Any())
                {
                    logger.LogInformation("Found {0} new release albums for SpotifyArtistId: {1}", newReleaseAlbums.Count, artist.SpotifyId);

                    var subscriptionsOutput = await _subscriptionAppService.GetSubscriptionsForArtist(new GetSubscriptionsForArtistInput
                    {
                        ArtistId = artist.Id
                    });

                    if (subscriptionsOutput.HasError)
                    {
                        logger.LogError(subscriptionsOutput.ErrorMessage);
                        //Don't let one error stop the entire thing from running, skip and move on to the next
                        continue;
                    }

                    if (subscriptionsOutput.Subscriptions.Any())
                    {
                        // Generally there will just be one newReleaseAlbum. Doesn't matter if we send multiple emails per artist
                        // in the rare case that more than one unique new release came out for them.

                        // Nov 2021: Spotify adds multiple versions of the same album at once, with different variations on the
                        // capitalisation of the name or other details. This is very annoying. Attempt to only notify about one version by keeping track
                        // of the names.
                        // We have called Reverse on the albums above to handle the most recent first, hopefully the most recent copy is the most "correct" one.
                        var notifiedAlbumNames = new List <string>();

                        foreach (var newReleaseAlbum in newReleaseAlbums)
                        {
                            string normalisedAlbumName = newReleaseAlbum.Name.ToLowerInvariant().Trim();

                            if (!notifiedAlbumNames.Contains(normalisedAlbumName))
                            {
                                // Send notification for all subscriptions
                                var notifyOutput = await _subscriberAppService.NotifySubscribers(new NotifySubscribersInput
                                {
                                    Artist        = artist,
                                    Album         = newReleaseAlbum,
                                    Subscriptions = subscriptionsOutput.Subscriptions
                                });

                                if (notifyOutput.HasError)
                                {
                                    logger.LogError(notifyOutput.ErrorMessage);
                                    // Don't let one error stop the entire thing from running, skip and move on to the next
                                    continue;
                                }

                                notifiedAlbumNames.Add(normalisedAlbumName);
                            }
                        }
                    }

                    //Save new albums to database
                    var albumsOutput = await _albumAppService.CreateAlbums(new CreateAlbumsInput
                    {
                        Artist = artist,
                        Albums = newReleaseAlbums
                    });

                    if (albumsOutput.HasError)
                    {
                        logger.LogError(albumsOutput.ErrorMessage);
                        //Don't let one error stop the entire thing from running, skip and move on to the next
                        continue;
                    }
                }
            }

            logger.LogInformation("ProcessNewSpotifyAlbums end");
        }