Example #1
0
        private async Task <(IEnumerable <Playlist>, bool, int)> GetUserPlaylists(int index, string ownerId)
        {
            var requestCount       = 0;
            var previousTotalCount = index * MaxItemsPerRequest;
            var isFinished         = false;

            var playlists = new List <Playlist>();

            do
            {
                var request = new PlaylistCurrentUsersRequest
                {
                    Limit  = MaxItemsPerRequest,
                    Offset = index * MaxItemsPerRequest
                };

                var requestedPlaylists = await spotify.Playlists.CurrentUsers(request);

                var totalCount = requestedPlaylists.Total.GetValueOrDefault();
                playlists.AddRange(requestedPlaylists.Items.Select(x => mapper.Map <Playlist>(x)));

                index++;
                requestCount++;

                if (playlists.Count + previousTotalCount == totalCount)
                {
                    isFinished = true;
                }
            }while (requestCount < MaxRequests && isFinished == false);

            playlists = playlists.Where(x => ownerId == null || x.OwnerId == ownerId).ToList();

            return(playlists, isFinished, index);
        }
Example #2
0
        public static async Task <IEnumerable <SimplePlaylist> > GetUserPlaylistsAsync(
            SpotifyAPICredentials spotifyAPICredentials,
            Guid userId,
            int page,
            int pageSize,
            IRepositoryManager repositoryManager)
        {
            try
            {
                var user = await repositoryManager.UserRepository.GetByIdAsync(userId);

                if (user.SpotifyRefreshToken != null)
                {
                    var spotifyAuthRefreshRequest = new AuthorizationCodeRefreshRequest
                                                    (
                        spotifyAPICredentials.ClientId,
                        spotifyAPICredentials.ClientSecret,
                        DecryptionService.DecryptString(user.SpotifyRefreshToken)
                                                    );

                    var spotifyAuthResponse = await new OAuthClient().RequestToken(spotifyAuthRefreshRequest);
                    var spotifyToken        = spotifyAuthResponse.AccessToken;

                    var spotifyClient = new SpotifyClient(spotifyToken);

                    var playlistsRequest = new PlaylistCurrentUsersRequest
                    {
                        Limit  = pageSize,
                        Offset = page * pageSize
                    };

                    var playlists = await spotifyClient.Playlists.CurrentUsers(playlistsRequest);

                    return(playlists.Items);
                }
                else
                {
                    throw new SpotifyNotLinkedException("Spotify Account not Linked", "Spotify Account not Linked");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
        public async Task OnGet()
        {
            var spotify = await _spotifyClientBuilder.BuildClient();

            int offset          = int.TryParse(Request.Query["Offset"], out offset) ? offset : 0;
            var playlistRequest = new PlaylistCurrentUsersRequest
            {
                Limit  = LIMIT,
                Offset = offset
            };

            Playlists = await spotify.Playlists.CurrentUsers(playlistRequest);

            if (Playlists.Next != null)
            {
                Next = Url.Page("Index", new { Offset = offset + LIMIT });
            }
            if (Playlists.Previous != null)
            {
                Previous = Url.Page("Index", values: new { Offset = offset - LIMIT });
            }
        }
Example #4
0
        /// <summary>
        /// Gets a list of the current users playlists, including following and own playlists.
        /// </summary>
        /// <returns>
        /// The list of playlists.
        /// </returns>
        public async Task <List <Playlist> > GetPlaylists()
        {
            var results = new List <Playlist>();

            PlaylistCurrentUsersRequest request = new PlaylistCurrentUsersRequest
            {
                Limit  = limit,
                Offset = startIndex
            };
            var spotify = await Authentication.GetSpotifyClientAsync();

            if (spotify != null)
            {
                var result = await spotify.Playlists.CurrentUsers(request);

                if (startIndex == 0)
                {
                    page = result;
                }

                if (result != null && result.Items != null)
                {
                    BitmapImage          image        = null;
                    User                 owner        = null;
                    int                  itemsCount   = 0;
                    PlaylistCategoryType categoryType = PlaylistCategoryType.All;
                    foreach (var item in result.Items)
                    {
                        //if (_playlist.Find(c => c.Id == item.Id) == null)
                        //    _playlist.Add(item);

                        if (Profile != null && item.Owner != null)
                        {
                            if (item.Owner.Id == Profile.Id)
                            {
                                categoryType = PlaylistCategoryType.MyPlaylist;
                            }
                            else
                            {
                                if (Models.Category._personalizedPlaylistNames.Find(c => c.ToLower().Equals(item.Name.ToLower())) != null)
                                {
                                    categoryType = PlaylistCategoryType.MadeForYou;
                                }
                                else
                                {
                                    categoryType = PlaylistCategoryType.Following;
                                }
                            }
                        }

                        if (item.Images != null && item.Images.Count > 0)
                        {
                            image = new BitmapImage(new Uri(item.Images.FirstOrDefault().Url));
                        }

                        if (item.Tracks != null)
                        {
                            itemsCount = item.Tracks.Total;
                        }

                        if (item.Owner != null)
                        {
                            BitmapImage ownerImg = null;
                            if (item.Owner.Images != null && item.Owner.Images.Count > 0)
                            {
                                ownerImg = new BitmapImage(new Uri(item.Owner.Images.FirstOrDefault().Url));
                            }
                            owner = new User(item.Owner.Id, item.Owner.DisplayName, ownerImg, item.Owner.Uri, item.Owner.ExternalUrls);
                        }

                        results.Add(new Playlist(item.Id,
                                                 item.Name,
                                                 image,
                                                 item.Uri,
                                                 item.ExternalUrls,
                                                 owner,
                                                 null,
                                                 item.Description,
                                                 categoryType,
                                                 itemsCount,
                                                 await QuickAccessItem.IsQuickAccessItem(item.Id)));

                        //reset
                        image        = null;
                        itemsCount   = 0;
                        owner        = null;
                        categoryType = PlaylistCategoryType.All;
                    }
                }

                startIndex += results.Count;
                return(results);
            }
            else
            {
                ViewModels.Helpers.DisplayDialog("Unable to load playlists", "An error occured, could not load items. Please give it another shot and make sure your internet connection is working");;
                return(null);
            }
        }