Esempio n. 1
0
        public async Task <List <Song> > SearchPlaylistAsync(NewsAPI.Constants.Countries country, NewsAPI.Constants.Categories catagory, int size = 25)
        {
            int trackAmount = 0;
            var results     = await news.GetHeadlinesAsync(country, catagory, 50);

            List <string> headlineText = new List <string>();

            foreach (var item in results)
            {
                headlineText.Add(item.Title);
            }

            List <Song> playlist = new List <Song>();

            for (int x = 0; x < headlineText.Count; x++)
            {
                var result      = headlineText[x];
                var splitResult = result.Split(' ');
                result = result.Split(' ').FirstOrDefault().Trim();
                for (int y = 0; y < splitResult.Length; y++)
                {
                    if (splitResult[y].Length > 3)
                    {
                        result = splitResult[y];
                        break;
                    }
                }

                var songOutput = await spotify.GetSongsAsync(result, 1);

                try
                {
                    playlist.Add(songOutput[0]);
                    trackAmount++;
                }
                catch (System.ArgumentOutOfRangeException e)
                {
                    Console.WriteLine(e.Message);
                }

                if (trackAmount == size)
                {
                    break;
                }
            }

            TooFewTracks = (trackAmount < size);
            TrackAmount  = trackAmount;

            return(playlist);
        }
        public async Task <List <Song> > SearchPlaylist(NewsAPI.Constants.Countries country, NewsAPI.Constants.Categories catagory, int size = 25)
        {
            int trackAmount = 0;
            var results     = await news.GetHeadlines(country, catagory, 50);



            List <string> headlineText = new List <string>();

            foreach (var item in results)
            {
                headlineText.Add(item.Title);
            }

            List <Song> playlist = new List <Song>();


            for (int x = 0; x < headlineText.Count; x++)
            {
                var result = headlineText[x];
                result = result.Split(' ').FirstOrDefault().Trim();

                var songOutput = await spotify.GetSongs(result, 1);

                try
                {
                    playlist.Add(songOutput[0]);
                    trackAmount++;
                }
                catch (System.ArgumentOutOfRangeException e)
                {
                    Console.WriteLine(e.Message);
                }

                if (trackAmount == size)
                {
                    break;
                }
            }

            if (trackAmount < size)
            {
                MessageBox.Show("Unfortunately we could only find " + trackAmount + " songs related to this news!", "Title", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }



            return(playlist);
        }
        public async Task <List <Article> > GetHeadlines(NewsAPI.Constants.Countries country, int size = 25)
        {
            var output = new List <Article>();

            var tmp = new NewsAPI.Models.TopHeadlinesRequest {
                Country = country, Language = NewsAPI.Constants.Languages.EN, PageSize = size
            };

            var response = await webAPI.GetTopHeadlinesAsync(tmp);

            foreach (var article in response.Articles)
            {
                output.Add(new Article(article.Title, article.Description, article.Url));
            }

            return(output);
        }