/// <summary> /// Parses and returns Song object for specifi radio station or NULL! /// </summary> /// <param name="radio"></param> public static Song Parse(RadioStationItem radio) { Song song = new Song(); song.PlayedAt = DateTime.Now; try { //Log4cs.Log(Importance.Debug, "Radio [{1}]{0}{2}", Environment.NewLine, radio.RadioName, radio.RawStationResponse); if(!string.IsNullOrEmpty(radio.RawStationResponse)) { int offset = 0; if( !string.IsNullOrEmpty(radio.beforeTitle) ) { // Use to place song title and artist position offset = radio.RawStationResponse.IndexOf(radio.beforeTitle, StringComparison.CurrentCultureIgnoreCase); } // Get song title Match m = radio.TitleRegex.Match(radio.RawStationResponse, offset); if(m.Success) { song.Title = m.Groups[radio.TitlePositon].ToString(); song.Title = StripTags(song.Title).Trim(); // TODO: clean HTML entities method song.Title = song.Title.Replace("#039;", "'"); } // Get song artist m = radio.ArtistRegex.Match(radio.RawStationResponse, offset); if(m.Success) { song.Artist = m.Groups[radio.ArtistPositon].ToString(); song.Artist = StripTags(song.Artist).Trim(); song.Artist = song.Artist.Replace("#039;", "'"); } } } catch(Exception ex) { Log4cs.Log(Importance.Error, "Error parsing response from radio station!"); Log4cs.Log(Importance.Debug, ex.ToString()); song = null; } return song; }
/// <summary> /// Adds song to playlist, returns whether song is new /// </summary> /// <param name="song"></param> public bool Add(Song song) { // Skip if both artist and title are empty (some kind of parsing error) if( song == null || (string.IsNullOrEmpty(song.Artist) && string.IsNullOrEmpty(song.Title)) ) { return false; } Song lastSong = GetLast(song.Radio.RadioName); if((lastSong == null) || !lastSong.Artist.Equals(song.Artist) || !lastSong.Title.Equals(song.Title)) { Log4cs.Log(Importance.Debug, "Adding new song on {0}: {1} - {2}", song.Radio.RadioName, song.Artist, song.Title); _arPlayedSongs.Add(song); return true; } else { Log4cs.Log(Importance.Debug, "Song {0} - {1} is currently playing on {2}", song.Artist, song.Title, song.Radio.RadioName); } return false; }
void OnSongParsed(RadioStationItem radio, Song song) { // Playlist returns true if need to display song if(_playlist.Add(song)) { Debug("New song on radio {0}: {1}-{2}", radio.RadioName, song.Artist, song.Title); } }