-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
341 lines (289 loc) · 14.2 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using NewsAPI;
using NewsAPI.Models;
using NewsAPI.Constants;
using Newtonsoft.Json;
using SpotifyAPI.Web;
using SpotifyAPI.Web.Enums;
using SpotifyAPI.Web.Models;
using SpotifyAPI.Web.Auth;
using System.Text.RegularExpressions;
namespace SoftwareEngineering
{
class Program
{
//Class structure of the recived Json string
public class News
{
public class Rootobject
{
public string status { get; set; }
public int totalResults { get; set; }
public Article[] articles { get; set; }
}
public class Article
{
public Source source { get; set; }
public string author { get; set; }
public string title { get; set; }
public string description { get; set; }
public string url { get; set; }
public string urlToImage { get; set; }
public DateTime publishedAt { get; set; }
public string content { get; set; }
}
public class Source
{
public string id { get; set; }
public string name { get; set; }
}
}
private static SpotifyWebAPI _spotify;
public static async void getSpotify()
{
//ClientID and SecretID
CredentialsAuth auth = new CredentialsAuth("088f576b5164473c99d0f31d261d1501", "d42f8386acbe4f6b8894ef9ccae9ef0a");
Token token = await auth.GetToken();
_spotify = new SpotifyWebAPI()
{
AccessToken = token.AccessToken,
TokenType = token.TokenType
};
}
public static void Main(string[] args)
{
bool incorrectInput = false;
int userSelection = -1;
do
{
Console.Clear();
Console.WriteLine("==***** SPOTIFY PLAYLIST FROM NEWS CREATOR THING *****==\n\n\n");
Console.WriteLine(" * Please select an option from belows\n");
Console.WriteLine(" 1 > Create playlist from news category\n 2 > Create playlist from your countries news\n 3 > Random news selection");
Console.Write("\n > ");
try
{
userSelection = Convert.ToInt16(Console.ReadLine());
if (userSelection < 1 || userSelection > 3)
{
Console.WriteLine("\n * Incorrect Input - Press Enter to Continue.");
incorrectInput = true;
Console.ReadLine();
}
else
{
incorrectInput = false;
}
}
catch
{
userSelection = -1;
Console.WriteLine("\n * Incorrect Input Format - Press Enter to Continue.");
incorrectInput = true;
Console.ReadLine();
}
} while (incorrectInput == true);
Console.Clear();
switch (userSelection)
{
case 1: //News category specific
ConnectToNews("category=" + getNewsCategory() + "&country=gb");
break;
case 2: //Country specific case
ConnectToNews("country=" + GetCountryCode());
break;
case 3: //Random news selection?
break;
default:
break;
}
Console.ReadLine();
}
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);
}
//Asks the user how long they want their playlist to be with exception handling
static int getListLength()
{
bool incorrectInput = false;
int userSelection;
do
{
Console.Clear();
Console.WriteLine(" * How many songs would you like in the playlist? (5-100)");
Console.Write(" > ");
try
{
userSelection = Convert.ToInt16(Console.ReadLine());
if (userSelection < 5 || userSelection > 100) //Playlist must be longer than 5 (need to use top 5 keywords) and less than 100 items
{
Console.WriteLine("\n * Incorrect Input - Press Enter to Continue.");
incorrectInput = true;
Console.ReadLine();
}
else
{
incorrectInput = false;
}
}
catch
{
userSelection = -1;
Console.WriteLine("\n * Incorrect Input Format - Press Enter to Continue.");
incorrectInput = true;
Console.ReadLine();
}
} while (incorrectInput == true);
return userSelection;
}
//User selects the category they want their news from with exception handling
public static string getNewsCategory()
{
bool incorrectInput = false;
int userSelection = -1;
string[] possibleCategories = { "Business", "Entertainment", "General", "Health", "Science", "Sports", "Technology" };
do
{
Console.Clear();
Console.WriteLine(" * Please select a news category from the options below\n");
for (int i = 0; i < possibleCategories.Length; i++)
{
Console.WriteLine(i + 1 + " > " + possibleCategories[i]);
}
Console.Write("\n > ");
try
{
userSelection = Convert.ToInt16(Console.ReadLine());
if (userSelection < 1 || userSelection > possibleCategories.Length)
{
Console.WriteLine("\n * Incorrect Input - Press Enter to Continue.");
incorrectInput = true;
Console.ReadLine();
}
else
{
incorrectInput = false;
}
}
catch
{
userSelection = -1;
Console.WriteLine("\n * Incorrect Input Format - Press Enter to Continue.");
incorrectInput = true;
Console.ReadLine();
}
} while (incorrectInput == true);
Console.Clear();
return possibleCategories[userSelection-1].ToLower(); //Returns the users selection from possibleCategories
}
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));
}
}
public class Song
{
public string SongName { get; set; }
public string Artist { get; set; }
}
public static string GetCountryCode()
{
string country = System.Globalization.RegionInfo.CurrentRegion.EnglishName;
switch (country)
{
case "United Kingdom":
return "gb";
}
return null;
}
}
}