Exemplo n.º 1
0
    public bool TryGetFanArt(FanArtConstants.FanArtMediaType mediaType, FanArtConstants.FanArtType fanArtType, string name, int maxWidth, int maxHeight, bool singleRandom, out IList<FanArtImage> result)
    {
      result = null;
      Guid mediaItemId;

      var isImage = mediaType == FanArtConstants.FanArtMediaType.Image;
      if (!Guid.TryParse(name, out mediaItemId) ||
        mediaType != FanArtConstants.FanArtMediaType.Undefined && !isImage ||
        fanArtType != FanArtConstants.FanArtType.Thumbnail)
        return false;

      IMediaLibrary mediaLibrary = ServiceRegistration.Get<IMediaLibrary>(false);
      if (mediaLibrary == null)
        return false;

      // Try to load thumbnail from ML
      List<Guid> thumbGuids = new List<Guid> { ThumbnailLargeAspect.ASPECT_ID };
      // Check for Image's rotation info
      if (isImage)
        thumbGuids.Add(ImageAspect.ASPECT_ID);

      IFilter filter = new MediaItemIdFilter(mediaItemId);
      IList<MediaItem> items = mediaLibrary.Search(new MediaItemQuery(thumbGuids, filter), false);
      if (items == null || items.Count == 0)
        return false;

      MediaItem mediaItem = items.First();
      byte[] textureData;
      if (!MediaItemAspect.TryGetAttribute(mediaItem.Aspects, ThumbnailLargeAspect.ATTR_THUMBNAIL, out textureData))
        return false;

      if (isImage)
        textureData = AutoRotateThumb(mediaItem, textureData);

      // Only one record required
      result = new List<FanArtImage> { new FanArtImage(name, textureData) };
      return true;
    }
Exemplo n.º 2
0
    protected bool GetPattern(FanArtConstants.FanArtMediaType mediaType, FanArtConstants.FanArtType fanArtType, Guid mediaItemId, string name, out string[] patterns)
    {
      patterns = null;
      string basePath = null;
      if (mediaItemId != Guid.Empty)
      {
        IMediaLibrary mediaLibrary = ServiceRegistration.Get<IMediaLibrary>(false);
        if (mediaLibrary == null)
          return false;

        IFilter filter = new MediaItemIdFilter(mediaItemId);
        IList<MediaItem> items = mediaLibrary.Search(new MediaItemQuery(NECESSARY_MIAS, filter), false);
        if (items == null || items.Count == 0)
          return false;

        MediaItem mediaItem = items.First();
        int movieDbId;
        if (MediaItemAspect.TryGetAttribute(mediaItem.Aspects, MovieAspect.ATTR_TMDB_ID, out movieDbId))
        {
          switch (mediaType)
          {
            case FanArtConstants.FanArtMediaType.Undefined:
            case FanArtConstants.FanArtMediaType.Movie:
              basePath = Path.Combine(MovieTheMovieDbMatcher.CACHE_PATH, movieDbId.ToString());
              break;
          }
        }
      }
      else
      {
        switch (mediaType)
        {
          case FanArtConstants.FanArtMediaType.Undefined:
          case FanArtConstants.FanArtMediaType.Movie:
            int movieDbId;
            basePath = !MovieTheMovieDbMatcher.Instance.TryGetMovieDbId(name, out movieDbId) ? null : Path.Combine(MovieTheMovieDbMatcher.CACHE_PATH, movieDbId.ToString());
            break;
          case FanArtConstants.FanArtMediaType.MovieCollection:
            int collectionId;
            basePath = !MovieTheMovieDbMatcher.Instance.TryGetCollectionId(name, out collectionId) ? null : Path.Combine(MovieTheMovieDbMatcher.CACHE_PATH, "COLL_" + collectionId);
            break;
        }
      }

      if (string.IsNullOrWhiteSpace(basePath))
        return false;

      switch (fanArtType)
      {
        case FanArtConstants.FanArtType.Thumbnail:
        case FanArtConstants.FanArtType.Poster:
          patterns = new[] { Path.Combine(basePath, "Posters\\*.jpg") };
          return true;
        case FanArtConstants.FanArtType.FanArt:
          patterns = new[] { Path.Combine(basePath, "Backdrops\\*.jpg") };
          return true;
      }
      return false;
    }
Exemplo n.º 3
0
    public bool TryGetFanArt(string mediaType, string fanArtType, string name, int maxWidth, int maxHeight, bool singleRandom, out IList<FanArtImage> result)
    {
      result = null;
      Guid mediaItemId;
      if (!Guid.TryParse(name, out mediaItemId))
        return false;

      IMediaLibrary mediaLibrary = ServiceRegistration.Get<IMediaLibrary>(false);
      if (mediaLibrary == null)
        return false;

      IFilter filter = new MediaItemIdFilter(mediaItemId);
      IList<MediaItem> items = mediaLibrary.Search(new MediaItemQuery(NECESSARY_MIAS, filter), false);
      if (items == null || items.Count == 0)
        return false;

      MediaItem mediaItem = items.First();

      string fileSystemPath = string.Empty;
      IList<string> patterns = new List<string>();
      switch (fanArtType)
      {
        case FanArtTypes.Banner:
          patterns.Add("banner.");
          break;
        case FanArtTypes.ClearArt:
          patterns.Add("clearart.");
          break;
        case FanArtTypes.Poster:
        case FanArtTypes.Thumbnail:
          patterns.Add("cover.");
          patterns.Add("poster.");
          patterns.Add("folder.");
          break;
        case FanArtTypes.FanArt:
          patterns.Add("backdrop.");
          patterns.Add("fanart.");
          break;
        default:
          return false;
      }
      // File based access
      try
      {
        var resourceLocator = mediaItem.GetResourceLocator();
        using (var accessor = resourceLocator.CreateAccessor())
        {
          ILocalFsResourceAccessor fsra = accessor as ILocalFsResourceAccessor;
          if (fsra != null)
          {
            var ext = Path.GetExtension(fsra.LocalFileSystemPath);
            if (!SUPPORTED_EXTENSIONS.Contains(ext))
              return false;

            MatroskaInfoReader mkvReader = new MatroskaInfoReader(fsra);
            byte[] binaryData = null;
            if (patterns.Any(pattern => mkvReader.GetAttachmentByName(pattern, out binaryData)))
            {
              result = new List<FanArtImage> { new FanArtImage(name, binaryData) };
              return true;
            }
          }
        }
      }
      catch (Exception ex)
      {
        ServiceRegistration.Get<ILogger>().Warn("MkvAttachmentsProvider: Exception while reading mkv attachment of type '{0}' from '{1}'", ex, fanArtType, fileSystemPath);
      }
      return false;
    }
Exemplo n.º 4
0
    /// <summary>
    /// Gets a list of <see cref="FanArtImage"/>s for a requested <paramref name="mediaType"/>, <paramref name="fanArtType"/> and <paramref name="name"/>.
    /// The name can be: Series name, Actor name, Artist name depending on the <paramref name="mediaType"/>.
    /// </summary>
    /// <param name="mediaType">Requested FanArtMediaType</param>
    /// <param name="fanArtType">Requested FanArtType</param>
    /// <param name="name">Requested name of Series, Actor, Artist...</param>
    /// <param name="maxWidth">Maximum width for image. <c>0</c> returns image in original size.</param>
    /// <param name="maxHeight">Maximum height for image. <c>0</c> returns image in original size.</param>
    /// <param name="singleRandom">If <c>true</c> only one random image URI will be returned</param>
    /// <param name="result">Result if return code is <c>true</c>.</param>
    /// <returns><c>true</c> if at least one match was found.</returns>
    public bool TryGetFanArt(string mediaType, string fanArtType, string name, int maxWidth, int maxHeight, bool singleRandom, out IList<IResourceLocator> result)
    {
      result = null;
      Guid mediaItemId;

      // Don't try to load "fanart" for images
      if (!Guid.TryParse(name, out mediaItemId) || mediaType == FanArtMediaTypes.Image)
        return false;

      IMediaLibrary mediaLibrary = ServiceRegistration.Get<IMediaLibrary>(false);
      if (mediaLibrary == null)
        return false;

      IFilter filter = new MediaItemIdFilter(mediaItemId);
      IList<MediaItem> items = mediaLibrary.Search(new MediaItemQuery(NECESSARY_MIAS, filter), false);
      if (items == null || items.Count == 0)
        return false;

      MediaItem mediaItem = items.First();
      var mediaIteamLocator = mediaItem.GetResourceLocator();
      var fanArtPaths = new List<ResourcePath>();
      var files = new List<IResourceLocator>();
      // File based access
      try
      {
        var mediaItemPath = mediaIteamLocator.NativeResourcePath;
        var mediaItemDirectoryPath = ResourcePathHelper.Combine(mediaItemPath, "../");
        var mediaItemFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(mediaItemPath.ToString());
        var mediaItemExtension = ResourcePathHelper.GetExtension(mediaItemPath.ToString());

        using (var directoryRa = new ResourceLocator(mediaIteamLocator.NativeSystemId, mediaItemDirectoryPath).CreateAccessor())
        {
          var directoryFsra = directoryRa as IFileSystemResourceAccessor;
          if (directoryFsra != null)
          {
            var potentialFanArtFiles = GetPotentialFanArtFiles(directoryFsra);

            if (fanArtType == FanArtTypes.Poster || fanArtType == FanArtTypes.Thumbnail)
              fanArtPaths.AddRange(
                from potentialFanArtFile in potentialFanArtFiles
                let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString())
                where /* Allow same file name only for non-images, otherwise each image would be its own thumbnail */
                      potentialFanArtFileNameWithoutExtension == mediaItemFileNameWithoutExtension && !EXTENSIONS.Contains(mediaItemExtension) || 
                      potentialFanArtFileNameWithoutExtension == mediaItemFileNameWithoutExtension + "-poster" ||
                      potentialFanArtFileNameWithoutExtension == "folder"
                select potentialFanArtFile);

            if (fanArtType == FanArtTypes.FanArt)
            {
              fanArtPaths.AddRange(
                from potentialFanArtFile in potentialFanArtFiles
                let potentialFanArtFileNameWithoutExtension = ResourcePathHelper.GetFileNameWithoutExtension(potentialFanArtFile.ToString())
                where potentialFanArtFileNameWithoutExtension == "backdrop" ||
                      potentialFanArtFileNameWithoutExtension.StartsWith(mediaItemFileNameWithoutExtension + "-fanart")
                select potentialFanArtFile);
              if (directoryFsra.ResourceExists("ExtraFanArt/"))
                using (var extraFanArtDirectoryFsra = directoryFsra.GetResource("ExtraFanArt/"))
                  fanArtPaths.AddRange(GetPotentialFanArtFiles(extraFanArtDirectoryFsra));
            }

            files.AddRange(fanArtPaths.Select(path => new ResourceLocator(mediaIteamLocator.NativeSystemId, path)));
          }
        }
      }
      catch (Exception ex)
      {
#if DEBUG
        ServiceRegistration.Get<ILogger>().Warn("LocalFanArtProvider: Error while searching fanart of type '{0}' for '{1}'", ex, fanArtType, mediaIteamLocator);
#endif
      }
      result = files;
      return files.Count > 0;
    }
Exemplo n.º 5
0
    /// <summary>
    /// Gets a list of <see cref="FanArtImage"/>s for a requested <paramref name="mediaType"/>, <paramref name="fanArtType"/> and <paramref name="name"/>.
    /// The name can be: Series name, Actor name, Artist name depending on the <paramref name="mediaType"/>.
    /// </summary>
    /// <param name="mediaType">Requested FanArtMediaType</param>
    /// <param name="fanArtType">Requested FanArtType</param>
    /// <param name="name">Requested name of Series, Actor, Artist...</param>
    /// <param name="maxWidth">Maximum width for image. <c>0</c> returns image in original size.</param>
    /// <param name="maxHeight">Maximum height for image. <c>0</c> returns image in original size.</param>
    /// <param name="singleRandom">If <c>true</c> only one random image URI will be returned</param>
    /// <param name="result">Result if return code is <c>true</c>.</param>
    /// <returns><c>true</c> if at least one match was found.</returns>
    public bool TryGetFanArt(FanArtConstants.FanArtMediaType mediaType, FanArtConstants.FanArtType fanArtType, string name, int maxWidth, int maxHeight, bool singleRandom, out IList<IResourceLocator> result)
    {
      result = null;
      Guid mediaItemId;

      // Don't try to load "fanart" for images
      if (!Guid.TryParse(name, out mediaItemId) || mediaType == FanArtConstants.FanArtMediaType.Image)
        return false;

      IMediaLibrary mediaLibrary = ServiceRegistration.Get<IMediaLibrary>(false);
      if (mediaLibrary == null)
        return false;

      IFilter filter = new MediaItemIdFilter(mediaItemId);
      IList<MediaItem> items = mediaLibrary.Search(new MediaItemQuery(_necessarryMIAs, filter), false);
      if (items == null || items.Count == 0)
        return false;

      MediaItem mediaItem = items.First();
      string fileSystemPath = null;
      List<string> localPatterns = new List<string>();
      List<IResourceLocator> files = new List<IResourceLocator>();
      // File based access
      try
      {
        var resourceLocator = mediaItem.GetResourceLocator();
        using (var accessor = resourceLocator.CreateAccessor())
        {
          ILocalFsResourceAccessor fsra = accessor as ILocalFsResourceAccessor;
          if (fsra != null)
          {
            fileSystemPath = fsra.LocalFileSystemPath;
            var path = Path.GetDirectoryName(fileSystemPath);
            var file = Path.GetFileNameWithoutExtension(fileSystemPath);

            if (fanArtType == FanArtConstants.FanArtType.Poster || fanArtType == FanArtConstants.FanArtType.Thumbnail)
            {
              localPatterns.Add(Path.ChangeExtension(fileSystemPath, ".jpg"));
              localPatterns.Add(Path.Combine(path, file + "-poster.jpg"));
              localPatterns.Add(Path.Combine(path, "folder.jpg"));
            }
            if (fanArtType == FanArtConstants.FanArtType.FanArt)
            {
              localPatterns.Add(Path.Combine(path, "backdrop.jpg"));
              localPatterns.Add(Path.Combine(path, file + "-fanart*.jpg"));
              localPatterns.Add(Path.Combine(path, "ExtraFanArt\\*.jpg"));
            }

            // Copy all patterns for .jpg -> .png + .tbn
            localPatterns.AddRange(new List<string>(localPatterns).Select(p => p.Replace(".jpg", ".png")));
            localPatterns.AddRange(new List<string>(localPatterns).Select(p => p.Replace(".jpg", ".tbn")));

            foreach (var pattern in localPatterns)
            {
              try
              {
                var pathPart = Path.GetDirectoryName(pattern);
                var filePart = Path.GetFileName(pattern);
                DirectoryInfo directoryInfo = new DirectoryInfo(pathPart);
                if (directoryInfo.Exists)
                {
                  files.AddRange(directoryInfo.GetFiles(filePart)
                    .Select(f => f.FullName)
                    .Select(fileName => new ResourceLocator(resourceLocator.NativeSystemId, ResourcePath.BuildBaseProviderPath(resourceLocator.NativeResourcePath.LastPathSegment.ProviderId, fileName))));
                }
              }
              catch
              {
              }
            }
          }
        }
      }
      catch (Exception ex)
      {
#if DEBUG
        ServiceRegistration.Get<ILogger>().Warn("Error while search fanart of type '{0}' for path '{1}'", ex, fanArtType, fileSystemPath);
#endif
      }
      result = files;
      return files.Count > 0;
    }
Exemplo n.º 6
0
 public IList<MediaItem> LoadCustomPlaylist(IList<Guid> mediaItemIds,
     IEnumerable<Guid> necessaryMIATypes, IEnumerable<Guid> optionalMIATypes, uint? offset = null, uint? limit = null)
 {
   IFilter filter = new MediaItemIdFilter(mediaItemIds);
   MediaItemQuery query = new MediaItemQuery(necessaryMIATypes, optionalMIATypes, filter);
   query.Limit = limit;
   query.Offset = offset;
   // Sort media items
   IDictionary<Guid, MediaItem> searchResult = new Dictionary<Guid, MediaItem>();
   foreach (MediaItem item in Search(query, false))
     searchResult[item.MediaItemId] = item;
   IList<MediaItem> result = new List<MediaItem>(searchResult.Count);
   foreach (Guid mediaItemId in mediaItemIds)
   {
     MediaItem item;
     if (searchResult.TryGetValue(mediaItemId, out item))
       result.Add(item);
   }
   return result;
 }