public async Task <bool> TryExtractRelationshipsAsync(IResourceAccessor mediaItemAccessor, IDictionary <Guid, IList <MediaItemAspect> > aspects, IList <IDictionary <Guid, IList <MediaItemAspect> > > extractedLinkedAspects) { AlbumInfo albumInfo = new AlbumInfo(); if (!albumInfo.FromMetadata(aspects)) { return(false); } AudioMetadataExtractor.TryUpdateAlbum(mediaItemAccessor, albumInfo); if (AudioMetadataExtractor.IncludeArtistDetails && !AudioMetadataExtractor.SkipOnlineSearches) { await OnlineMatcherService.Instance.UpdateAlbumPersonsAsync(albumInfo, PersonAspect.OCCUPATION_ARTIST).ConfigureAwait(false); } foreach (PersonInfo person in albumInfo.Artists) { IDictionary <Guid, IList <MediaItemAspect> > personAspects = new Dictionary <Guid, IList <MediaItemAspect> >(); if (person.SetMetadata(personAspects) && personAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID)) { extractedLinkedAspects.Add(personAspects); } } return(extractedLinkedAspects.Count > 0); }
public async Task <bool> TryExtractRelationshipsAsync(IResourceAccessor mediaItemAccessor, IDictionary <Guid, IList <MediaItemAspect> > aspects, IList <IDictionary <Guid, IList <MediaItemAspect> > > extractedLinkedAspects) { TrackInfo trackInfo = new TrackInfo(); if (!trackInfo.FromMetadata(aspects)) { return(false); } AlbumInfo albumInfo = trackInfo.CloneBasicInstance <AlbumInfo>(); AudioMetadataExtractor.TryUpdateAlbum(mediaItemAccessor, albumInfo); if (!AudioMetadataExtractor.SkipOnlineSearches) { await OnlineMatcherService.Instance.UpdateAlbumAsync(albumInfo, false).ConfigureAwait(false); } IDictionary <Guid, IList <MediaItemAspect> > albumAspects = new Dictionary <Guid, IList <MediaItemAspect> >(); albumInfo.SetMetadata(albumAspects); bool trackVirtual = true; if (MediaItemAspect.TryGetAttribute(aspects, MediaAspect.ATTR_ISVIRTUAL, false, out trackVirtual)) { MediaItemAspect.SetAttribute(albumAspects, MediaAspect.ATTR_ISVIRTUAL, trackVirtual); } if (!aspects.ContainsKey(ReimportAspect.ASPECT_ID)) //Ignore for reimports because the image might be wrong { byte[] data; if (MediaItemAspect.TryGetAttribute(aspects, ThumbnailLargeAspect.ATTR_THUMBNAIL, out data)) { //Use image from track as image MediaItemAspect.SetAttribute(albumAspects, ThumbnailLargeAspect.ATTR_THUMBNAIL, data); } } if (!albumAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID)) { return(false); } extractedLinkedAspects.Add(albumAspects); return(true); }
public async Task<bool> TryExtractRelationshipsAsync(IResourceAccessor mediaItemAccessor, IDictionary<Guid, IList<MediaItemAspect>> aspects, IList<IDictionary<Guid, IList<MediaItemAspect>>> extractedLinkedAspects) { if (BaseInfo.IsVirtualResource(aspects)) return false; TrackInfo trackInfo = new TrackInfo(); if (!trackInfo.FromMetadata(aspects)) return false; AudioMetadataExtractor.TryUpdateArtists(mediaItemAccessor, trackInfo.Artists, false); if (AudioMetadataExtractor.IncludeArtistDetails && !AudioMetadataExtractor.SkipOnlineSearches) await OnlineMatcherService.Instance.UpdateTrackPersonsAsync(trackInfo, PersonAspect.OCCUPATION_ARTIST, false, _category).ConfigureAwait(false); foreach (PersonInfo person in trackInfo.Artists) { IDictionary<Guid, IList<MediaItemAspect>> personAspects = new Dictionary<Guid, IList<MediaItemAspect>>(); if (person.SetMetadata(personAspects) && personAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID)) extractedLinkedAspects.Add(personAspects); } return extractedLinkedAspects.Count > 0; }
public async Task <bool> TryExtractRelationshipsAsync(IResourceAccessor mediaItemAccessor, IDictionary <Guid, IList <MediaItemAspect> > aspects, IList <IDictionary <Guid, IList <MediaItemAspect> > > extractedLinkedAspects) { TrackInfo trackInfo = new TrackInfo(); if (!trackInfo.FromMetadata(aspects)) { return(false); } AlbumInfo albumInfo = trackInfo.CloneBasicInstance <AlbumInfo>(); AudioMetadataExtractor.TryUpdateAlbum(mediaItemAccessor, albumInfo); if (!AudioMetadataExtractor.SkipOnlineSearches) { await OnlineMatcherService.Instance.UpdateAlbumAsync(albumInfo, false, _category).ConfigureAwait(false); } IDictionary <Guid, IList <MediaItemAspect> > albumAspects = new Dictionary <Guid, IList <MediaItemAspect> >(); albumInfo.SetMetadata(albumAspects); bool trackVirtual = true; if (MediaItemAspect.TryGetAttribute(aspects, MediaAspect.ATTR_ISVIRTUAL, false, out trackVirtual)) { MediaItemAspect.SetAttribute(albumAspects, MediaAspect.ATTR_ISVIRTUAL, trackVirtual); } if (!albumAspects.ContainsKey(ExternalIdentifierAspect.ASPECT_ID)) { return(false); } extractedLinkedAspects.Add(albumAspects); return(true); }
protected async Task ExtractAlbumAndArtistFanArt(Guid mediaItemId, IDictionary <Guid, IList <MediaItemAspect> > aspects) { bool shouldCacheLocal = false; IResourceLocator mediaItemLocator = null; if (!BaseInfo.IsVirtualResource(aspects)) { mediaItemLocator = GetResourceLocator(aspects); //Whether local fanart should be stored in the fanart cache shouldCacheLocal = ShouldCacheLocalFanArt(mediaItemLocator.NativeResourcePath, AudioMetadataExtractor.CacheLocalFanArt, AudioMetadataExtractor.CacheOfflineFanArt); } if (mediaItemLocator == null) { return; } if (!shouldCacheLocal && AudioMetadataExtractor.SkipFanArtDownload) { return; //Nothing to do } TrackInfo trackInfo = new TrackInfo(); trackInfo.FromMetadata(aspects); AlbumInfo albumInfo = trackInfo.CloneBasicInstance <AlbumInfo>(); string albumTitle = albumInfo.ToString(); ResourcePath albumDirectory = null; if (shouldCacheLocal) { albumDirectory = ResourcePathHelper.Combine(mediaItemLocator.NativeResourcePath, "../"); if (AudioMetadataExtractor.IsDiscFolder(albumTitle, albumDirectory.FileName)) { //Probably a CD folder so try next parent albumDirectory = ResourcePathHelper.Combine(albumDirectory, "../"); } } //Artist fanart may be stored in the album directory, so get the artists now IList <Tuple <Guid, string> > artists = GetArtists(aspects); //Album fanart if (RelationshipExtractorUtils.TryGetLinkedId(AudioAlbumAspect.ROLE_ALBUM, aspects, out Guid albumMediaItemId) && AddToCache(albumMediaItemId)) { if (shouldCacheLocal) { //If the track is not a stub, Store track tag images in the album if (!aspects.ContainsKey(ReimportAspect.ASPECT_ID) && MediaItemAspect.TryGetAttribute(aspects, MediaAspect.ATTR_ISSTUB, out bool isStub) && isStub == false) { await ExtractTagFanArt(mediaItemLocator, albumMediaItemId, albumTitle); } await ExtractAlbumFolderFanArt(mediaItemLocator.NativeSystemId, albumDirectory, albumMediaItemId, albumTitle, artists).ConfigureAwait(false); } if (!AudioMetadataExtractor.SkipFanArtDownload) { await OnlineMatcherService.Instance.DownloadAudioFanArtAsync(albumMediaItemId, albumInfo).ConfigureAwait(false); } } if (shouldCacheLocal && artists != null) { await ExtractArtistFolderFanArt(mediaItemLocator.NativeSystemId, albumDirectory, artists).ConfigureAwait(false); } }
private void ExtractFolderImages(IResourceLocator mediaItemLocater, Guid?albumMediaItemId, IDictionary <Guid, string> artistMediaItems, string albumTitle) { string fileSystemPath = string.Empty; // File based access try { if (mediaItemLocater != null) { fileSystemPath = mediaItemLocater.NativeResourcePath.FileName; var mediaItemPath = mediaItemLocater.NativeResourcePath; var mediaItemFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(mediaItemPath.ToString()).ToLowerInvariant(); var albumMediaItemDirectoryPath = ResourcePathHelper.Combine(mediaItemPath, "../"); var artistMediaItemDirectoryPath = ResourcePathHelper.Combine(mediaItemPath, "../../"); if (AudioMetadataExtractor.IsDiscFolder(albumTitle, albumMediaItemDirectoryPath.FileName)) { //Probably a CD folder so try next parent albumMediaItemDirectoryPath = ResourcePathHelper.Combine(mediaItemPath, "../../"); artistMediaItemDirectoryPath = ResourcePathHelper.Combine(mediaItemPath, "../../../"); } //Album fanart var fanArtPaths = new List <ResourcePath>(); var coverPaths = new List <ResourcePath>(); var bannerPaths = new List <ResourcePath>(); var logoPaths = new List <ResourcePath>(); var clearArtPaths = new List <ResourcePath>(); var thumbPaths = new List <ResourcePath>(); if (albumMediaItemId.HasValue) { using (var directoryRa = new ResourceLocator(mediaItemLocater.NativeSystemId, albumMediaItemDirectoryPath).CreateAccessor()) { var directoryFsra = directoryRa as IFileSystemResourceAccessor; if (directoryFsra != null) { if (artistMediaItems.Count > 0) { //Get Artists thumbs IFileSystemResourceAccessor alternateArtistMediaItemDirectory = directoryFsra.GetResource(".artists"); if (alternateArtistMediaItemDirectory != null) { foreach (var artist in artistMediaItems) { var potentialArtistFanArtFiles = GetPotentialFanArtFiles(alternateArtistMediaItemDirectory); foreach (ResourcePath thumbPath in from potentialFanArtFile in potentialArtistFanArtFiles let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString()) where potentialFanArtFileNameWithoutExtension.StartsWith(artist.Value.Replace(" ", "_"), StringComparison.InvariantCultureIgnoreCase) select potentialFanArtFile) { SaveFolderFile(mediaItemLocater, thumbPath, FanArtTypes.Thumbnail, artist.Key, artist.Value); } } } } var potentialFanArtFiles = GetPotentialFanArtFiles(directoryFsra); coverPaths.AddRange( from potentialFanArtFile in potentialFanArtFiles let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString()).ToLowerInvariant() where potentialFanArtFileNameWithoutExtension == "poster" || potentialFanArtFileNameWithoutExtension == "folder" || potentialFanArtFileNameWithoutExtension == "cover" select potentialFanArtFile); fanArtPaths.AddRange( from potentialFanArtFile in potentialFanArtFiles let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString()).ToLowerInvariant() where potentialFanArtFileNameWithoutExtension == "backdrop" || potentialFanArtFileNameWithoutExtension == "fanart" select potentialFanArtFile); if (directoryFsra.ResourceExists("ExtraFanArt/")) { using (var extraFanArtDirectoryFsra = directoryFsra.GetResource("ExtraFanArt/")) fanArtPaths.AddRange(GetPotentialFanArtFiles(extraFanArtDirectoryFsra)); } } } foreach (ResourcePath posterPath in coverPaths) { SaveFolderFile(mediaItemLocater, posterPath, FanArtTypes.Cover, albumMediaItemId.Value, albumTitle); } foreach (ResourcePath fanartPath in fanArtPaths) { SaveFolderFile(mediaItemLocater, fanartPath, FanArtTypes.FanArt, albumMediaItemId.Value, albumTitle); } //Artist fanart fanArtPaths.Clear(); coverPaths.Clear(); bannerPaths.Clear(); logoPaths.Clear(); clearArtPaths.Clear(); thumbPaths.Clear(); if (artistMediaItems.Count > 0) { using (var directoryRa = new ResourceLocator(mediaItemLocater.NativeSystemId, artistMediaItemDirectoryPath).CreateAccessor()) { var directoryFsra = directoryRa as IFileSystemResourceAccessor; if (directoryFsra != null) { Guid artistId = artistMediaItems.Where(a => string.Compare(a.Value, directoryFsra.ResourceName, true) == 0).Select(a => a.Key).FirstOrDefault(); if (artistId == Guid.Empty && artistMediaItems.Count == 1) { artistId = artistMediaItems.First().Key; } if (artistId != Guid.Empty) { var potentialFanArtFiles = GetPotentialFanArtFiles(directoryFsra); thumbPaths.AddRange( from potentialFanArtFile in potentialFanArtFiles let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString()).ToLowerInvariant() where potentialFanArtFileNameWithoutExtension.StartsWith("thumb") || potentialFanArtFileNameWithoutExtension.StartsWith("folder") || potentialFanArtFileNameWithoutExtension.StartsWith("artist") select potentialFanArtFile); bannerPaths.AddRange( from potentialFanArtFile in potentialFanArtFiles let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString()).ToLowerInvariant() where potentialFanArtFileNameWithoutExtension.StartsWith("banner") select potentialFanArtFile); logoPaths.AddRange( from potentialFanArtFile in potentialFanArtFiles let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString()).ToLowerInvariant() where potentialFanArtFileNameWithoutExtension.StartsWith("logo") select potentialFanArtFile); clearArtPaths.AddRange( from potentialFanArtFile in potentialFanArtFiles let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString()).ToLowerInvariant() where potentialFanArtFileNameWithoutExtension.StartsWith("clearart") select potentialFanArtFile); fanArtPaths.AddRange( from potentialFanArtFile in potentialFanArtFiles let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString()).ToLowerInvariant() where potentialFanArtFileNameWithoutExtension.StartsWith("backdrop") || potentialFanArtFileNameWithoutExtension.StartsWith("fanart") select potentialFanArtFile); if (directoryFsra.ResourceExists("ExtraFanArt/")) { using (var extraFanArtDirectoryFsra = directoryFsra.GetResource("ExtraFanArt/")) fanArtPaths.AddRange(GetPotentialFanArtFiles(extraFanArtDirectoryFsra)); } foreach (ResourcePath thumbPath in thumbPaths) { SaveFolderFile(mediaItemLocater, thumbPath, FanArtTypes.Thumbnail, artistId, artistMediaItems[artistId]); } foreach (ResourcePath bannerPath in bannerPaths) { SaveFolderFile(mediaItemLocater, bannerPath, FanArtTypes.Banner, artistId, artistMediaItems[artistId]); } foreach (ResourcePath logoPath in logoPaths) { SaveFolderFile(mediaItemLocater, logoPath, FanArtTypes.Logo, artistId, artistMediaItems[artistId]); } foreach (ResourcePath clearArtPath in clearArtPaths) { SaveFolderFile(mediaItemLocater, clearArtPath, FanArtTypes.ClearArt, artistId, artistMediaItems[artistId]); } foreach (ResourcePath fanartPath in fanArtPaths) { SaveFolderFile(mediaItemLocater, fanartPath, FanArtTypes.FanArt, artistId, artistMediaItems[artistId]); } } } } } } } } catch (Exception ex) { ServiceRegistration.Get <ILogger>().Warn("AudioFanArtHandler: Exception while reading folder images for '{0}'", ex, fileSystemPath); } }