コード例 #1
0
        static void ConnectToNews(string searchPerameters)
        {
            //Get the news, change the url to get different news
            var url  = "https://newsapi.org/v2/top-headlines?" + searchPerameters + "&" + "apiKey=2cdba516b7024c7eb765e9f0b186c0eb";
            var json = new WebClient().DownloadString(url);

            //Using Newtonsoft.Json to deserialise the Json as a News.Rootobject Object
            News.Rootobject deserializedNews = JsonConvert.DeserializeObject <News.Rootobject>(json);

            //The string can now be used for stuff like this (the titles of all the headline articles)
            for (int i = 0; i < deserializedNews.articles.Length; i++)
            {
                Console.WriteLine(deserializedNews.articles[i].title);
            }
            SearchSpotify(deserializedNews);
        }
コード例 #2
0
        static void SearchSpotify(News.Rootobject news)
        {
            getSpotify();
            Console.WriteLine("Enter to start search");
            Console.ReadLine();

            Dictionary <string, int> wordFreq = new Dictionary <string, int>();

            string[] desiredWords   = { "fury", "rage", "outrage", "sex", "sexy", "fraud", "row", "attack", "football", "sport", "rugby", "pool", "roar", "cash", "kick", "stab", "punch", "hit", "suspect", "gunman", "extremist", "genocide", "death", "killed", "award", "medal", "funny", "royal", "queen", "prince", "king", "climate", "fined", "surprise", "labour", "conservative", "green", "brexit", "animal", "politics", "premier", "league", "celebrity", "firefighter", "policeman", "drama", "outbreak", "angrily", "netflix", "facebook", "google", "snapchat", "twitter", "dad", "mum", "father", "grandfather", "grandmother", "mother", "healthy", "genetics", "fundraising", "plastic" };
            string[] undesiredWords = { "expresscouk", "a", "and", "or", "if", "i", "in", "claims", "expert", "by", "of", "it", "to", "news", "mail", "telegraph", "the", "also", "up", "down", "left", "right", "yes", "no", "from", "on", "off", "under", "with", "till", "than", "any", "every", "other", "some", "such", "come", "get", "give", "go", "keep", "let", "make", "put", "seem", "take", "do", "have", "say", "but", "though", "when", "where", "how", "why", "who", "far", "forward", "near", "now", "uk", "against", ".com", ".co.uk", "pro", "before", ",", ".", " ", "want", "me", "gone", "will", "only", "leave", "my", "you", "took", "your", "that", "he", "be", "new", "deal", "at", "had", "she", "today", "its", "may", "is", "out", "general", "are", "both", "an", "what", "into", "has", "his", "for", "told", "was", "her", "after", "not", "says", "said", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "as", "could", "been", "else", "someone", "partner", "watch", "one", "two", "three", "four", "five", "weeks", "spent", "-", "", "once", "twice", "mother", "first", "second", "while", "chars", "image", "whats", "us", "caption", "citys", "unveiled", "guardian", "bbc", "live" };

            for (int i = 0; i < news.articles.Length; i++)
            {
                Regex rgx = new Regex("[^a-zA-Z -]"); //Gets rid of all special characters
                if (news.articles[i].content == null)
                {
                    news.articles[i].content = rgx.Replace(news.articles[i].title ?? "", "");
                }
                else
                {
                    news.articles[i].content = rgx.Replace(news.articles[i].content ?? "", "");
                }
                foreach (string word in (news.articles[i].content).Split(' '))
                {
                    string wordL = (word).ToLower();                                    //puts everything in lower case
                    if (!undesiredWords.Contains(wordL) && wordFreq.ContainsKey(wordL)) //checks to see if word is in 'undesired words' list
                    {
                        wordFreq[wordL]++;                                              //adds one to freq if already in list
                    }
                    else if (!undesiredWords.Contains(wordL))
                    {
                        wordFreq.Add(wordL, 1); //if not in the list, add it
                    }
                }
            }

            var sortedDict = from entry in wordFreq orderby entry.Value descending select entry; //sorts dictionary list
            Dictionary <string, int> top5words = new Dictionary <string, int>();                 //creates new dictionary

            int wordCount = 0;

            foreach (KeyValuePair <string, int> kvp in sortedDict)
            {
                if (wordCount < 5) //gets top 5 frequent words
                {
                    top5words.Add(kvp.Key, kvp.Value);
                    wordCount++;
                }

                Console.WriteLine(string.Format("Word = {0} | Freq = {1}", kvp.Key, kvp.Value));
            }

            Console.ReadLine();

            int playlistLength = getListLength();



            //string[,] createdPlaylist = new string[userSelection,2];
            //Dictionary<string, string> createdPlaylist = new Dictionary<string, string>();
            var createdPlaylist = new List <Song>(); //Creates new list of class song

            int playlistRemainder = playlistLength % 5;
            int wordNumber        = 0;

            foreach (KeyValuePair <string, int> kvp in top5words)
            {
                int extraSong = 0;
                wordNumber++;
                if (wordNumber <= playlistRemainder)
                {
                    extraSong = 1;
                }
                Console.WriteLine(string.Format("Word = {0} | Freq = {1}", kvp.Key, kvp.Value));
                SearchItem item = _spotify.SearchItems(kvp.Key, SearchType.Track);
                for (int i = 0; i < (playlistLength / 5) + extraSong; i++)                      //goes through each top word and gets the userselection/5 (equally distributed songs/word)
                {
                    Song tempSong = new Song();                                                 //creates new instance of the song class
                    tempSong.SongName = item.Tracks.Items[i].Name;                              //sets name
                    tempSong.Artist   = Convert.ToString(item.Tracks.Items[i].Artists[0].Name); //sets artist
                    createdPlaylist.Add(tempSong);                                              //adds song to playlist
                }
            }


            //freaquency analysis on all headlines retrieved
            //Loop spotify search for the top 5(?) words
            //Total ammount of songs/ 5 rounded up is the ammount of songs needed for each word
            //Might need to remove some random songs

            Console.WriteLine("\n");
            foreach (Song song in createdPlaylist) //outputs all songs
            {
                Console.WriteLine(string.Format("Song = {0}\nArtist = {1}\n\n", song.SongName, song.Artist));
            }
        }