public static bool TryMatchImdbId(string folderOrFileName, out string imdbId) { string extensionLower = StringUtils.TrimToEmpty(Path.GetExtension(folderOrFileName)).ToLower(); if (!MatroskaConsts.MATROSKA_VIDEO_EXTENSIONS.Contains(extensionLower)) { imdbId = null; return(false); } MatroskaInfoReader mkvReader = new MatroskaInfoReader(folderOrFileName); // Add keys to be extracted to tags dictionary, matching results will returned as value Dictionary <string, IList <string> > tagsToExtract = MatroskaConsts.DefaultTags; mkvReader.ReadTags(tagsToExtract); if (tagsToExtract[MatroskaConsts.TAG_MOVIE_IMDB_ID] != null) { foreach (string candidate in tagsToExtract[MatroskaConsts.TAG_MOVIE_IMDB_ID]) { if (ImdbIdMatcher.TryMatchImdbId(candidate, out imdbId)) { return(true); } } } imdbId = null; return(false); }
public static async Task <string> TryMatchImdbIdAsync(ILocalFsResourceAccessor folderOrFileLfsra) { // Calling EnsureLocalFileSystemAccess not necessary; only string operation string extensionLower = StringUtils.TrimToEmpty(Path.GetExtension(folderOrFileLfsra.LocalFileSystemPath)).ToLower(); if (!MatroskaConsts.MATROSKA_VIDEO_EXTENSIONS.Contains(extensionLower)) { return(null); } MatroskaBinaryReader mkvReader = new MatroskaBinaryReader(folderOrFileLfsra); // Add keys to be extracted to tags dictionary, matching results will returned as value Dictionary <string, IList <string> > tagsToExtract = MatroskaConsts.DefaultVideoTags; await mkvReader.ReadTagsAsync(tagsToExtract).ConfigureAwait(false); if (tagsToExtract[MatroskaConsts.TAG_MOVIE_IMDB_ID] != null) { foreach (string candidate in tagsToExtract[MatroskaConsts.TAG_MOVIE_IMDB_ID]) { if (ImdbIdMatcher.TryMatchImdbId(candidate, out string imdbId)) { return(imdbId); } } } return(null); }
protected SeriesInfo GetSeriesFromTags(IDictionary <string, IList <string> > extractedTags) { SeriesInfo seriesInfo = new SeriesInfo(); if (extractedTags[MatroskaConsts.TAG_EPISODE_TITLE] != null) { seriesInfo.Episode = extractedTags[MatroskaConsts.TAG_EPISODE_TITLE].FirstOrDefault(); } if (extractedTags[MatroskaConsts.TAG_SERIES_TITLE] != null) { seriesInfo.Series = extractedTags[MatroskaConsts.TAG_SERIES_TITLE].FirstOrDefault(); } if (extractedTags[MatroskaConsts.TAG_SERIES_IMDB_ID] != null) { string imdbId; foreach (string candidate in extractedTags[MatroskaConsts.TAG_SERIES_IMDB_ID]) { if (ImdbIdMatcher.TryMatchImdbId(candidate, out imdbId)) { seriesInfo.ImdbId = imdbId; break; } } } int tmpInt; if (extractedTags[MatroskaConsts.TAG_SEASON_NUMBER] != null && int.TryParse(extractedTags[MatroskaConsts.TAG_SEASON_NUMBER].FirstOrDefault(), out tmpInt)) { seriesInfo.SeasonNumber = tmpInt; } if (extractedTags[MatroskaConsts.TAG_EPISODE_NUMBER] != null) { int episodeNum; foreach (string s in extractedTags[MatroskaConsts.TAG_EPISODE_NUMBER]) { if (int.TryParse(s, out episodeNum)) { if (!seriesInfo.EpisodeNumbers.Contains(episodeNum)) { seriesInfo.EpisodeNumbers.Add(episodeNum); } } } } return(seriesInfo); }
/// <summary> /// Tries to create a LocalResourceAccessor for the given <paramref name="metaFilePath"/> and to read the contents to match the IMDB id. /// </summary> /// <param name="metaFilePath">Path to file</param> /// <param name="imdbId">Returns a valid IMDB or <c>null</c></param> /// <returns>true if matched</returns> private static bool TryRead(string metaFilePath, out string imdbId) { imdbId = null; IResourceAccessor metaFileAccessor; if (!ResourcePath.Deserialize(metaFilePath).TryCreateLocalResourceAccessor(out metaFileAccessor)) { return(false); } using (metaFileAccessor) { ILocalFsResourceAccessor lfsra = metaFileAccessor as ILocalFsResourceAccessor; if (lfsra == null || !lfsra.Exists) { return(false); } string content = File.ReadAllText(lfsra.LocalFileSystemPath); return(ImdbIdMatcher.TryMatchImdbId(content, out imdbId)); } }
/// <summary> /// Tries to match series by reading matroska tags from <paramref name="folderOrFileLfsra"/>. /// </summary> /// <param name="folderOrFileLfsra"><see cref="ILocalFsResourceAccessor"/> to file or folder</param> /// <param name="episodeInfo">Returns the parsed EpisodeInfo</param> /// <param name="extractedAspectData">Dictionary containing a mapping of media item aspect ids to /// already present media item aspects, this metadata extractor should edit. If a media item aspect is not present /// in this dictionary but found by this metadata extractor, it will add it to the dictionary.</param> /// <returns><c>true</c> if successful.</returns> public bool MatchSeries(ILocalFsResourceAccessor folderOrFileLfsra, EpisodeInfo episodeInfo) { // Calling EnsureLocalFileSystemAccess not necessary; only string operation string extensionLower = StringUtils.TrimToEmpty(Path.GetExtension(folderOrFileLfsra.LocalFileSystemPath)).ToLower(); if (!MatroskaConsts.MATROSKA_VIDEO_EXTENSIONS.Contains(extensionLower)) { return(false); } MatroskaInfoReader mkvReader = new MatroskaInfoReader(folderOrFileLfsra); // Add keys to be extracted to tags dictionary, matching results will returned as value Dictionary <string, IList <string> > tagsToExtract = MatroskaConsts.DefaultTags; mkvReader.ReadTags(tagsToExtract); IList <string> tags = tagsToExtract[MatroskaConsts.TAG_EPISODE_SUMMARY]; string plot = tags != null?tags.FirstOrDefault() : string.Empty; if (!string.IsNullOrEmpty(plot)) { episodeInfo.HasChanged |= MetadataUpdater.SetOrUpdateString(ref episodeInfo.Summary, plot, true); } // Series and episode handling. Prefer information from tags. if (tagsToExtract[MatroskaConsts.TAG_EPISODE_TITLE] != null) { string title = tagsToExtract[MatroskaConsts.TAG_EPISODE_TITLE].FirstOrDefault(); if (!string.IsNullOrEmpty(title)) { title = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(title); episodeInfo.HasChanged |= MetadataUpdater.SetOrUpdateString(ref episodeInfo.EpisodeName, title, true); } } if (tagsToExtract[MatroskaConsts.TAG_SERIES_TITLE] != null) { string title = tagsToExtract[MatroskaConsts.TAG_SERIES_TITLE].FirstOrDefault(); if (!string.IsNullOrEmpty(title)) { title = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(title); episodeInfo.HasChanged |= MetadataUpdater.SetOrUpdateString(ref episodeInfo.SeriesName, title, true); } } if (tagsToExtract[MatroskaConsts.TAG_SERIES_IMDB_ID] != null) { string imdbId; foreach (string candidate in tagsToExtract[MatroskaConsts.TAG_SERIES_IMDB_ID]) { if (ImdbIdMatcher.TryMatchImdbId(candidate, out imdbId)) { episodeInfo.HasChanged |= MetadataUpdater.SetOrUpdateId(ref episodeInfo.SeriesImdbId, imdbId); break; } } } if (tagsToExtract[MatroskaConsts.TAG_SERIES_ACTORS] != null) { episodeInfo.HasChanged |= MetadataUpdater.SetOrUpdateList(episodeInfo.Actors, tagsToExtract[MatroskaConsts.TAG_SERIES_ACTORS].Select(t => new PersonInfo() { Name = t, Occupation = PersonAspect.OCCUPATION_ACTOR, MediaName = episodeInfo.EpisodeName.Text, ParentMediaName = episodeInfo.SeriesName.Text }).ToList(), false); } // On Series, the counting tag is "TVDB" if (tagsToExtract[MatroskaConsts.TAG_SERIES_TVDB_ID] != null) { int tmp; foreach (string candidate in tagsToExtract[MatroskaConsts.TAG_SERIES_TVDB_ID]) { if (int.TryParse(candidate, out tmp) == true) { episodeInfo.HasChanged |= MetadataUpdater.SetOrUpdateId(ref episodeInfo.SeriesTvdbId, tmp); break; } } } int tmpInt; if (tagsToExtract[MatroskaConsts.TAG_SEASON_NUMBER] != null && int.TryParse(tagsToExtract[MatroskaConsts.TAG_SEASON_NUMBER].FirstOrDefault(), out tmpInt)) { episodeInfo.HasChanged |= MetadataUpdater.SetOrUpdateValue(ref episodeInfo.SeasonNumber, tmpInt); } if (tagsToExtract[MatroskaConsts.TAG_EPISODE_NUMBER] != null) { int episodeNum; foreach (string s in tagsToExtract[MatroskaConsts.TAG_EPISODE_NUMBER]) { if (int.TryParse(s, out episodeNum)) { if (!episodeInfo.EpisodeNumbers.Contains(episodeNum)) { episodeInfo.EpisodeNumbers.Add(episodeNum); } } } } return(true); }
public static async Task <bool> ExtractFromTagsAsync(ILocalFsResourceAccessor folderOrFileLfsra, MovieInfo movieInfo) { // Calling EnsureLocalFileSystemAccess not necessary; only string operation string extensionLower = StringUtils.TrimToEmpty(Path.GetExtension(folderOrFileLfsra.LocalFileSystemPath)).ToLower(); if (!MatroskaConsts.MATROSKA_VIDEO_EXTENSIONS.Contains(extensionLower)) { return(false); } // Try to get extended information out of matroska files) MatroskaBinaryReader mkvReader = new MatroskaBinaryReader(folderOrFileLfsra); // Add keys to be extracted to tags dictionary, matching results will returned as value Dictionary <string, IList <string> > tagsToExtract = MatroskaConsts.DefaultVideoTags; await mkvReader.ReadTagsAsync(tagsToExtract).ConfigureAwait(false); // Read plot IList <string> tags = tagsToExtract[MatroskaConsts.TAG_EPISODE_SUMMARY]; string plot = tags != null?tags.FirstOrDefault() : string.Empty; if (!string.IsNullOrEmpty(plot)) { movieInfo.HasChanged |= MetadataUpdater.SetOrUpdateString(ref movieInfo.Summary, plot, true); } // Read genre tags = tagsToExtract[MatroskaConsts.TAG_SERIES_GENRE]; if (tags != null) { List <GenreInfo> genreList = tags.Where(s => !string.IsNullOrEmpty(s?.Trim())).Select(s => new GenreInfo { Name = s.Trim() }).ToList(); movieInfo.HasChanged |= MetadataUpdater.SetOrUpdateList(movieInfo.Genres, genreList, movieInfo.Genres.Count == 0); } // Read actors tags = tagsToExtract[MatroskaConsts.TAG_ACTORS]; if (tags != null) { movieInfo.HasChanged |= MetadataUpdater.SetOrUpdateList(movieInfo.Actors, tags.Select(t => new PersonInfo() { Name = t, Occupation = PersonAspect.OCCUPATION_ACTOR, MediaName = movieInfo.MovieName.Text }).ToList(), false); } tags = tagsToExtract[MatroskaConsts.TAG_DIRECTORS]; if (tags != null) { movieInfo.HasChanged |= MetadataUpdater.SetOrUpdateList(movieInfo.Directors, tags.Select(t => new PersonInfo() { Name = t, Occupation = PersonAspect.OCCUPATION_DIRECTOR, MediaName = movieInfo.MovieName.Text }).ToList(), false); } tags = tagsToExtract[MatroskaConsts.TAG_WRITTEN_BY]; if (tags != null) { movieInfo.HasChanged |= MetadataUpdater.SetOrUpdateList(movieInfo.Writers, tags.Select(t => new PersonInfo() { Name = t, Occupation = PersonAspect.OCCUPATION_WRITER, MediaName = movieInfo.MovieName.Text }).ToList(), false); } if (tagsToExtract[MatroskaConsts.TAG_MOVIE_IMDB_ID] != null) { string imdbId; foreach (string candidate in tagsToExtract[MatroskaConsts.TAG_MOVIE_IMDB_ID]) { if (ImdbIdMatcher.TryMatchImdbId(candidate, out imdbId)) { movieInfo.HasChanged |= MetadataUpdater.SetOrUpdateId(ref movieInfo.ImdbId, imdbId); break; } } } if (tagsToExtract[MatroskaConsts.TAG_MOVIE_TMDB_ID] != null) { int tmp; foreach (string candidate in tagsToExtract[MatroskaConsts.TAG_MOVIE_TMDB_ID]) { if (int.TryParse(candidate, out tmp) == true) { movieInfo.HasChanged |= MetadataUpdater.SetOrUpdateId(ref movieInfo.MovieDbId, tmp); break; } } } return(true); }