/// <summary> /// Using user provided parsing information, attempt to map retrieved data to mp3 file /// </summary> /// <param name="itemToProcess">The mp3 file wrapper</param> /// <param name="website">An exposed websites node</param> public void Process(Mp3File itemToProcess, Website website) { // Only use first valid source for parsing to avoid overwriting data and potentially unnecessary parsing foreach (var uri in website.uriElements) { var completedUri = CompleteUri(uri.uri, itemToProcess.DirtyTags); if (completedUri.Equals(this.completedUri)) { ProcessRegex(website.regexElements, itemToProcess); break; } if (Uri.IsWellFormedUriString(completedUri, UriKind.Absolute)) { try { // Is this were to be asynchronous, we'd have to introduce a mechanism // by which processing is done by a grouping of albums instead of per // mp3 file. In such a case, you could process them in parallel as generally // they would be using the same source. var completedPage = webClient.DownloadString(completedUri); this.completedUri = completedUri; this.completedPage = completedPage; ProcessRegex(website.regexElements, itemToProcess); break; } catch { continue; } } } }
private int addedTags = 0; // Counter to know when to unregister listener #endregion #region Startup and Shutdown /// <summary> /// Set defaults and bind grid /// </summary> /// <param name="mp3File">mp3file is used only for tags</param> public Mp3FileDialogue(Mp3File mp3File) { InitializeComponent(); this.mp3File = mp3File; Text = Path.GetFileName(mp3File.AbsolutePath); exposedTags = ExposedTagsConfiguration.ExposedTagsConfigurationInstance.ExposedTags; tags = new BindingList <TagProcessing>(mp3File.DirtyTags); TagsGrid.AutoGenerateColumns = false; TagsGrid.DataSource = tags; }
/// <summary> /// Process every user defined regex, if it's invalid, remove the regex /// </summary> /// <param name="regexElements">Regex elements define control</param> /// <param name="dirtyTags">Tags to where to map</param> private void ProcessRegex(List <Website.RegularExpression> regexElements, Mp3File itemToProcess) { List <Website.RegularExpression> invalidRegexElements = null; // Process all regex foreach (var regex in regexElements) { if (string.IsNullOrEmpty(regex.Regex)) { continue; } try { if (regex.SelectMultiple) { var matches = Regex.Matches(completedPage, regex.Regex); foreach (Match match in matches) { itemToProcess.AddDirtyTag(regex.TargetTag, match.Groups[1].Value, regex.Append); } } else { var match = Regex.Match(completedPage, regex.Regex); if (match.Success) { itemToProcess.AddDirtyTag(regex.TargetTag, match.Groups[1].Value, regex.Append); } } } catch // invalid regex { if (invalidRegexElements == null) { invalidRegexElements = new List <Website.RegularExpression>(); } invalidRegexElements.Add(regex); continue; } } // Remove invalid regex if (invalidRegexElements != null) { invalidRegexElements.ForEach(x => regexElements.Remove(x)); } }
/// <summary> /// Process a single item /// </summary> /// <param name="itemsToProcess"></param> /// <returns></returns> public bool TryProcess(Mp3File itemToProcess, List <Website> targetWebsites) { var success = false; log.Log("Processing item " + Path.GetFileName(itemToProcess.AbsolutePath) + "\n"); if (audioFingerprintLookup != null) { try { log.Log("Attempting audio fingerprint lookup.\n"); audioFingerprintLookup.Lookup(itemToProcess); log.Log("Audio fingerprint lookup successful.\n"); success = true; } catch { log.Log("Audio fingerprint lookup failed.\n"); } } if (targetWebsites != null && targetWebsites.Count > 0) { try { log.Log("Attempting screen scraping.\n"); foreach (var website in targetWebsites) { httpScreenScrapping.Process(itemToProcess, website); } log.Log("Screen scraping successful.\n"); success = true; } catch { log.Log("Screen scraping failed.\n"); } } var mp3FileDialogue = new Mp3FileDialogue(itemToProcess); mp3FileDialogue.Show(); return(success); }
/// <summary> /// Does a known mapping from the foreign JSON object to dirty tags /// </summary> /// <param name="item">The mp3file reference to add dirty tags to</param> /// <param name="musicbrainzJson">The raw musicbrainz api response</param> private void MapMusicBrainzResponseToMp3(Mp3File item, JObject musicbrainzJson) { var artistInfoJson = musicbrainzJson["artist-credit"].FirstOrDefault(); var albumInfoJson = musicbrainzJson["releases"].FirstOrDefault(); var discInfoJson = albumInfoJson["media"].FirstOrDefault(); item.AddDirtyTag("Artists", artistInfoJson.Value <string>("name"), false); item.AddDirtyTag("Title", musicbrainzJson.Value <string>("title"), false); item.AddDirtyTag("Album", albumInfoJson.Value <string>("title"), false); item.AddDirtyTag("Year", albumInfoJson.Value <string>("date"), false); item.AddDirtyTag("Genres", "", false); item.AddDirtyTag("Composers", artistInfoJson.Value <string>("name"), false); item.AddDirtyTag("PERFORMER", artistInfoJson.Value <string>("name"), false); item.AddDirtyTag("AlbumArtists", artistInfoJson.Value <string>("name"), false); item.AddDirtyTag("Track", discInfoJson.Value <string>("position"), false); item.AddDirtyTag("TrackCount", discInfoJson.Value <string>("track-count"), false); item.AddDirtyTag("Disc", "", false); item.AddDirtyTag("DiscCount", discInfoJson["discs"].Count().ToString(), false); item.AddDirtyTag("Comment", string.Empty, false); }
/// <summary> /// Attempt to invoke the application, create a Json object /// </summary> /// <param name="item">Mp3 file to do a lookup on</param> /// <exception>Throws an exception if unable to create the Json object, or http request did not succeed</exception> public void Lookup(Mp3File item) { var fingerprint = GetFingerprint(item.AbsolutePath); var fingerprintJson = JsonConvert.DeserializeObject <JObject>(fingerprint); fingerprintJson.Add("client", apiKey); // Remote api does not like decimal values var duration = ( JProperty )fingerprintJson.Children().Where( x => (( JProperty )x).Name == "duration").FirstOrDefault(); if (duration != null) { duration.Value = Math.Round((( double )duration.Value)); } var acoustidJson = LookupByFingerprint(fingerprintJson); var musicbrainzJson = LookupByAcoustid(acoustidJson); MapMusicBrainzResponseToMp3(item, musicbrainzJson); }