Example #1
0
        private async Task DoWork(CancellationToken cancellationToken)
        {
            await _youtubeService.Initialize(cancellationToken);

            await _spotifyService.Initialize(cancellationToken);

            if (cancellationToken.IsCancellationRequested)
            {
                return;
            }

            var playlistId = await _spotifyService.CreatePlaylist(Constants.ApplicationName, cancellationToken);

            await foreach (var title in _youtubeService.QueryLikedMusicVideos(cancellationToken))
            {
                var finalTitle = FilterTitle(title);

                _logger.LogInformation("Searching for {title}!", finalTitle);
                var tracks = await _spotifyService.SearchTracks(finalTitle, cancellationToken);

                if (tracks.Count == 0)
                {
                    _logger.LogWarning("{title} not found!", finalTitle);
                    continue;
                }

                // grab the first track for now
                var track = tracks.First();
                _logger.LogInformation("Found {track.Name} by {track.Artists}", track.Name, JsonConvert.SerializeObject(track.Artists));

                await _spotifyService.AddToPlaylist(playlistId, track.Id, cancellationToken);
            }
        }
Example #2
0
        public ActionResult Callback(string code, string state, string error)
        {
            TokenResponse token = null;

            var host = HttpContext.Request.Host;

            var client = new HttpClient();

            var body = $"client_id={_config.Value.ClientId}&" +
                       $"client_secret={_config.Value.ClientSecret}&" +
                       $"grant_type=authorization_code&" +
                       $"code={code}&" +
                       $"redirect_uri=http%3A%2F%2F{host}%2Fhome%2Fcallback";

            var response = client.PostAsync("https://accounts.spotify.com/api/token", new StringContent(body, Encoding.UTF8, "application/x-www-form-urlencoded")).Result;

            using (var content = response.Content)
            {
                var result = content.ReadAsStringAsync().Result;

                token = JsonConvert.DeserializeObject <TokenResponse>(result);

                if (!string.IsNullOrWhiteSpace(token.AccessToken))
                {
                    HttpContext.Session.SetObjectAsJson("tokenResponse", token);
                }
            }

            var playlist = HttpContext.Session.GetObjectFromJson <PlaylistResult>("playlist");

            playlist.Url = _spotifyService.CreatePlaylist(token, playlist);

            return(View("Index", playlist.ToHomeModel()));
        }
Example #3
0
        public async Task <IActionResult> Playlist(List <long> artistIds, int numberOfTracks)
        {
            var user = await _userManager.GetUserAsync(User);

            var artists = await _artistService.GetArtistsById(artistIds);

            var topTracks = await _spotifyService.GetMostPopularTracks(artists, user.SpotifyUserId, numberOfTracks);

            var model = new PlaylistViewModel
            {
                Playlist = await _spotifyService.CreatePlaylist(topTracks, user.SpotifyUserId)
            };

            return(View(model));
        }
        public async Task <ActionResult <Playlist> > CreatePlaylist(string userId,
                                                                    [FromBody] CreatePlaylistCommand command)
        {
            var user = await _userService.GetUserById(userId);

            var artists = await _artistService.GetArtistsById(command.ArtistIds);

            var topTracks =
                await _spotifyService.GetMostPopularTracks(artists, user.SpotifyUserId, command.NumberOfTracks);

            var playlist = await _spotifyService.CreatePlaylist(topTracks, user.SpotifyUserId);

            playlist.OwnedBy = null;
            return(Created(playlist.SpotifyId, playlist));
        }