public FreeDBCDInfoDetail parse(string content) { m_content = (string)content.Clone(); if (m_content.IndexOf("# xmcd") != 0) { return(null); } m_offsets = parseOffsets(); m_length = parseLength(); m_trackDurations = calculateDurations(m_offsets, m_length); m_discid = parseDiscIDs()[0]; m_artist = parseArtist(); m_playorder = parsePlayOrder(); m_title = parseTitle(); m_year = parseYear(); m_genre = parseGenre(); m_extd = parseExtension(); m_cdTrackDetail = new FreeDBCDTrackDetail[m_offsets.Length]; for (int i = 0; i < m_offsets.Length; i++) { m_cdTrackDetail[i] = new FreeDBCDTrackDetail(); string trackArtist = parseTrackArtist(i); m_cdTrackDetail[i].Artist = trackArtist.Equals(m_artist) ? null : trackArtist; m_cdTrackDetail[i].Title = parseTrackTitle(i); m_cdTrackDetail[i].Offset = m_offsets[i]; m_cdTrackDetail[i].Duration = m_trackDurations[i]; m_cdTrackDetail[i].EXTT = parseTrackExtension(i); m_cdTrackDetail[i].TrackNumber = i + 1; } return(new FreeDBCDInfoDetail(m_discid, m_artist, m_title, m_genre, m_year, m_length, m_cdTrackDetail, m_extd, m_playorder)); }
public override Task <bool> UpdateFromOnlineMusicTrackAsync(TrackInfo track, string language, bool cacheOnly) { try { if (string.IsNullOrEmpty(track.AlbumCdDdId) || track.TrackNum == 0) { return(Task.FromResult(false)); } //TODO: Split CDDB ID into disc id and genre? string discId = null; FreeDBCDInfoDetail discInfo; if (GetCachedDisc(discId, out discInfo)) { FreeDBCDTrackDetail foundTrack = null; foreach (FreeDBCDTrackDetail trackDetail in discInfo.Tracks) { if (trackDetail.TrackNumber == track.TrackNum) { foundTrack = trackDetail; break; } } if (foundTrack == null) { return(Task.FromResult(false)); } track.Album = discInfo.Title; track.AlbumArtists = ConvertToPersons(discInfo.Artist, PersonAspect.OCCUPATION_ARTIST); track.TotalTracks = discInfo.Tracks.Count(); track.ReleaseDate = discInfo.Year > 0 ? new DateTime(discInfo.Year, 1, 1) : default(DateTime?); track.TrackNum = foundTrack.TrackNumber; track.TrackName = foundTrack.Title; track.Artists = ConvertToPersons(foundTrack.Artist, PersonAspect.OCCUPATION_ARTIST); track.Duration = foundTrack.Duration; if (!track.DataProviders.Contains(_name)) { track.DataProviders.Add(_name); } return(Task.FromResult(true)); } return(Task.FromResult(false)); } catch (Exception ex) { ServiceRegistration.Get <ILogger>().Debug("FreeDbWrapper: Exception while processing track {0}", ex, track.ToString()); return(Task.FromResult(false)); } }
public override async Task <List <TrackInfo> > SearchTrackAsync(TrackInfo trackSearch, string language) { if (string.IsNullOrEmpty(trackSearch.AlbumCdDdId) || trackSearch.TrackNum == 0) { return(null); } //TODO: Split CDDB ID into disc id and genre? string genre = null; string discId = null; List <TrackInfo> tracks = null; try { if (_freeDbHandler.Connect()) { string[] xmcds = await _freeDbHandler.GetDiscDetailsXMCDAsync(genre, discId).ConfigureAwait(false); if (xmcds != null) { string fileName = GetCacheFilePath(discId, genre); if (File.Exists(fileName) == false) { File.WriteAllLines(fileName, xmcds, Encoding.UTF8); } FreeDBCDInfoDetail discInfo = _freeDbHandler.GetDiscDetailsFromXMCD(xmcds); if (discInfo != null) { FreeDBCDTrackDetail foundTrack = null; foreach (FreeDBCDTrackDetail trackDetail in discInfo.Tracks) { if (trackDetail.TrackNumber == trackSearch.TrackNum) { foundTrack = trackDetail; break; } } if (foundTrack == null) { return(null); } if (tracks == null) { tracks = new List <TrackInfo>(); } TrackInfo info = new TrackInfo() { AlbumCdDdId = trackSearch.AlbumCdDdId, Album = discInfo.Title, AlbumArtists = ConvertToPersons(discInfo.Artist, PersonAspect.OCCUPATION_ARTIST), TotalTracks = discInfo.Tracks.Count(), ReleaseDate = discInfo.Year > 0 ? new DateTime(discInfo.Year, 1, 1) : default(DateTime?), TrackNum = foundTrack.TrackNumber, TrackName = foundTrack.Title, Artists = ConvertToPersons(foundTrack.Artist, PersonAspect.OCCUPATION_ARTIST), Duration = foundTrack.Duration, DataProviders = new List <string>() { _name } }; tracks.Add(info); } } } } finally { _freeDbHandler.Disconnect(); } return(tracks); }
private void InitVariables(ArrayList offsets, Hashtable comments, Hashtable fields) { // all the song offsets m_offsets = (int[])offsets.ToArray(typeof(int)); m_cdTrackDetail = new FreeDBCDTrackDetail[m_offsets.Length]; foreach (DictionaryEntry dict in comments) { string key = (string)dict.Key; string val = (string)dict.Value; switch (key) { case "Disc length": m_length = Convert.ToInt32(val.Split(new char[] { ' ' })[0].Trim()); m_trackDurations = calculateDurations(m_offsets, m_length); break; case "Revision": break; case "Submitted via": break; case "Processed by": break; default: break; } } foreach (DictionaryEntry dict in fields) { string key = (string)dict.Key; string val = (string)dict.Value; if (key.StartsWith("TTITLE") || key.StartsWith("EXTT")) // a track { continue; } else { switch (key) { case "DISCID": m_discid = val; break; case "DTITLE": m_title = val; break; case "DYEAR": try { m_year = Convert.ToInt32(val); } catch { string year = val; int k = 0; int l = 0; char[] yearChar = year.ToCharArray(); for (l = 0; l < yearChar.Length; l++) { if (Char.IsDigit(yearChar[l])) { break; } } for (k = l; k < yearChar.Length; k++) { if (!Char.IsDigit(yearChar[k])) { break; } } if (l < k) { if (k == year.Length) { m_year = Convert.ToInt32(year.Substring(l)); } else { m_year = Convert.ToInt32(year.Substring(l, k - l)); } } } break; case "DGENRE": m_genre = val; break; case "EXTD": m_extd = val; break; case "PLAYORDER": { // restricting scope... int[] ai; char[] seps = { ',', '\n', '\r', '\t' }; string[] tokens = val.Split(seps); if (tokens.Length == 1 && tokens[0].Trim().Length == 0) { ai = new int[0]; } else { ai = new int[tokens.Length]; } try { for (int i = 0; i < ai.Length; i++) { ai[i] = Convert.ToInt32(tokens[i]); } } catch { } m_playorder = ai; } break; default: break; } } } // find the artist from the title... int slash = m_title.IndexOf(" / "); //Some FreeDB annotators use a " - " instead of the conventional " / " //Try to find such a delimiter if the standard one is not found beforehand if (slash < 0) { slash = m_title.IndexOf(" - "); } if (slash < 0) { m_artist = null; } else { m_artist = m_title.Substring(0, slash); m_title = m_title.Substring(slash + 3); } /*Tracks from compilation-like albums have their own nomenclature in FreeDB (cf. http://www.freedb.org/modules.php?name=Sections&sop=viewarticle&artid=26#2-2), track tags have to be pre-processed * * The only difficulty here is whether or not some Artist or Title names may contain legitimate " / ", regarding FreeDB nomenclature it is illegal * We split the string even if we're not sure this CD is a real compilation: it is usually no use to try to figure out if the CD is an actual compilation by looking at its Artist tag (album Artist tag = "((Various)|(Assorted))(Artists)?") because most annotators don't tag it that way * * Extended to the detection of " - " as well, a lot of FreeDB annotators do not follow the above rule and split the Artist from the Title name this way; this workaround is a hell more tricky, a few legitimate tags may be badly cut * We only split the string if we're sure this CD is a real compilation * */ //isALegitimateCompilation if the CD Artist is either not set or equals "Various", "Various Artists", etc... Regex artistTagIsSetToVarious = new Regex(@"^(([Vv]arious)|([Aa]ssorted))( [Aa]rtist[s]?)?$"); bool isALegitimateCompilation = (m_artist == string.Empty || artistTagIsSetToVarious.Match(m_artist).Success) ? true : false; for (int i = 0; i < offsets.Count; i++) { string title = (string)fields["TTITLE" + i]; string extt = (string)fields["EXTT" + i]; m_cdTrackDetail[i] = new FreeDBCDTrackDetail(); string trackArtist = extendedParseTrackArtist(title, isALegitimateCompilation); string trackTitle = extendedParseTrackTitle(title, isALegitimateCompilation); m_cdTrackDetail[i].Artist = trackArtist.Equals(m_artist) ? null : trackArtist; m_cdTrackDetail[i].Title = trackTitle; m_cdTrackDetail[i].Offset = m_offsets[i]; m_cdTrackDetail[i].Duration = m_trackDurations[i]; m_cdTrackDetail[i].EXTT = extt; m_cdTrackDetail[i].TrackNumber = i + 1; } }