// Radio Bot // Returns radio stations of your favorite type of music. Advanced bot for students that have finished the tutorial. using System; using System.Text.RegularExpressions; namespace ChatBot { class RadioBot : BasicBot { public override string HandleMessage(string message, string user) { MatchCollection matches = getAllRows(); string reply = "I found these stations: "; foreach(Match match in matches) { string row = match.ToString(); if(User.RegexMatch("dc",message)) Console.WriteLine(row); string stationName, city, state, link, genre; ExtractDetails(row, out stationName, out city, out state, out link, out genre); if(state == "Washington") reply += stationName + "(" + genre + ", " + link + "); "; } return reply; } public static MatchCollection getAllRows() { string usLiveRadio = User.WebpageToString("http://www.usliveradio.com/"); usLiveRadio = usLiveRadio.Trim().Replace('\n',' ').Replace('\t',' ').Replace('\r',' '); Regex regex = new Regex(" ])*", RegexOptions.IgnoreCase); return regex.Matches(usLiveRadio); } public static void ExtractDetails(string row, out string stationName, out string city, out string state, out string link, out string genre) { Regex rowRegex = new Regex(@" (?.*) (?.*) (?.*) .* (?.*) (?.*) ", RegexOptions.IgnoreCase); Match match = rowRegex.Match(row); if(!match.Success) Console.WriteLine("Barbarian does not match: " + row); stationName = User.StripHTML(match.Groups["call"].Value); city = User.StripHTML(match.Groups["city"].Value); state = User.StripHTML(match.Groups["state"].Value); genre = User.StripHTML(match.Groups["genre"].Value); link = match.Groups["link"].Value; Regex linkRegex = new Regex("[^\"]*)\"", RegexOptions.IgnoreCase); Match match2 = linkRegex.Match(link); if(!match2.Success) Console.WriteLine("Assassin does not match: " + link); link = match2.Groups["link"].Value; } } }