Ejemplo n.º 1
0
        public async Task <string?> GetArtworkUrlFor(Track track)
        {
            if (track.ArtworkUrl != null)
            {
                return(track.ArtworkUrl);
            }

            var sw      = StartStopwatch();
            var spotify = await _spotifyApiProvider.GetAPI();

            var query  = $"{track.ArtistName} ${track.Title}";
            var search = await spotify.SearchItemsEscapedAsync(query, SearchType.Track, 1);

            if (CheckForErrorsAndLog(search))
            {
                return(null);
            }

            var searchedTrack  = search.Tracks.Items.First();
            var albumImages    = searchedTrack.Album.Images;
            var desiredArtwork = albumImages.FirstOrDefault();

            if (desiredArtwork == null)
            {
                return(null);
            }

            track.ArtworkUrl = desiredArtwork.Url;
            await _context.SaveChangesAsync();

            StopStopwatchAndLogTime(sw);
            return(track.ArtworkUrl);
        }
Ejemplo n.º 2
0
        public async Task <Result> UpdateUserPreferences(Models.User user, UserPreferencesDto newPreferences)
        {
            TimeZoneInfo info;
            var          validTimeZone = TZConvert.TryGetTimeZoneInfo(newPreferences.IanaTimezone, out info);

            if (!validTimeZone)
            {
                return(Results.Fail("Invalid time zone"));
            }

            user.IanaTimezone = newPreferences.IanaTimezone;
            await _context.SaveChangesAsync();

            return(Results.Ok());
        }
Ejemplo n.º 3
0
        private async Task <SpotifyApiToken> GetToken(int retryCount = 0, Exception?exception = null)
        {
            if (retryCount > 1)
            {
                throw exception ?? new InvalidOperationException();
            }

            var existingToken = await _context.SpotifyApiTokens.FirstOrDefaultAsync();

            if (existingToken != null && existingToken.ExpiresAt >= DateTime.UtcNow)
            {
                return(existingToken);
            }

            var credentials     = new CredentialsAuth(_clientId, _clientSecret);
            var newSpotifyToken = await credentials.GetToken();

            var newExpirationDate = DateTime.UtcNow.AddSeconds(newSpotifyToken.ExpiresIn - 5);

            var newToken = new SpotifyApiToken()
            {
                ExpiresAt = newExpirationDate,
                Token     = newSpotifyToken.AccessToken,
                Type      = newSpotifyToken.TokenType,
            };

            try
            {
                if (existingToken != null)
                {
                    _context.SpotifyApiTokens.Remove(existingToken);
                }
                await _context.AddAsync(newToken);

                await _context.SaveChangesAsync();
            }
            catch (Exception newException)
            {
                return(await GetToken(retryCount + 1, newException));
            }

            _logger.LogInformation("Created new Spotify access token");
            return(newToken);
        }
Ejemplo n.º 4
0
        public async Task AddWeeksAndSaveChanges(IEnumerable <ChartWeek> weeks)
        {
            await _context.ChartWeeks.AddRangeAsync(weeks);

            await _context.SaveChangesAsync();
        }