2019-01-13 15:03:10 -05:00
|
|
|
using System;
|
2013-12-30 11:32:01 -05:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-07-29 17:47:25 -04:00
|
|
|
using System.Net.Http;
|
2013-12-30 11:32:01 -05:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
2019-01-13 14:26:31 -05:00
|
|
|
using MediaBrowser.Common.Net;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Providers;
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
using MediaBrowser.Model.Providers;
|
2013-12-30 11:32:01 -05:00
|
|
|
|
2014-01-28 13:37:01 -05:00
|
|
|
namespace MediaBrowser.Providers.Studios
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
2014-01-28 13:37:01 -05:00
|
|
|
public class StudiosImageProvider : IRemoteImageProvider
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
|
|
|
private readonly IServerConfigurationManager _config;
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
2014-01-28 13:37:01 -05:00
|
|
|
public StudiosImageProvider(IServerConfigurationManager config, IHttpClient httpClient, IFileSystem fileSystem)
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
|
|
|
_config = config;
|
|
|
|
_httpClient = httpClient;
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
}
|
|
|
|
|
2019-01-13 15:31:14 -05:00
|
|
|
public string Name => "Emby Designs";
|
2013-12-30 11:32:01 -05:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public bool Supports(BaseItem item)
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
|
|
|
return item is Studio;
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
|
2014-01-28 13:37:01 -05:00
|
|
|
{
|
|
|
|
return new List<ImageType>
|
|
|
|
{
|
2019-01-07 18:27:46 -05:00
|
|
|
ImageType.Primary,
|
2014-01-28 13:37:01 -05:00
|
|
|
ImageType.Thumb
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
|
|
|
return GetImages(item, true, true, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private async Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, bool posters, bool thumbs, CancellationToken cancellationToken)
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
|
|
|
var list = new List<RemoteImageInfo>();
|
|
|
|
|
|
|
|
if (posters)
|
|
|
|
{
|
|
|
|
var posterPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudioposters.txt");
|
|
|
|
|
2017-04-29 02:22:33 -04:00
|
|
|
posterPath = await EnsurePosterList(posterPath, cancellationToken).ConfigureAwait(false);
|
2013-12-30 11:32:01 -05:00
|
|
|
|
|
|
|
list.Add(GetImage(item, posterPath, ImageType.Primary, "folder"));
|
|
|
|
}
|
|
|
|
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
|
|
|
|
2013-12-30 11:50:57 -05:00
|
|
|
if (thumbs)
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
|
|
|
var thumbsPath = Path.Combine(_config.ApplicationPaths.CachePath, "imagesbyname", "remotestudiothumbs.txt");
|
|
|
|
|
2017-04-29 02:22:33 -04:00
|
|
|
thumbsPath = await EnsureThumbsList(thumbsPath, cancellationToken).ConfigureAwait(false);
|
2013-12-30 11:32:01 -05:00
|
|
|
|
|
|
|
list.Add(GetImage(item, thumbsPath, ImageType.Thumb, "thumb"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return list.Where(i => i != null);
|
|
|
|
}
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
private RemoteImageInfo GetImage(BaseItem item, string filename, ImageType type, string remoteFilename)
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
2018-09-12 13:26:21 -04:00
|
|
|
var list = GetAvailableImages(filename, _fileSystem);
|
2013-12-30 11:32:01 -05:00
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
var match = FindMatch(item, list);
|
2013-12-30 11:32:01 -05:00
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(match))
|
|
|
|
{
|
|
|
|
var url = GetUrl(match, remoteFilename);
|
|
|
|
|
|
|
|
return new RemoteImageInfo
|
|
|
|
{
|
|
|
|
ProviderName = Name,
|
|
|
|
Type = type,
|
|
|
|
Url = url
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetUrl(string image, string filename)
|
|
|
|
{
|
|
|
|
return string.Format("https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studios/{0}/{1}.jpg", image, filename);
|
|
|
|
}
|
|
|
|
|
2017-04-29 02:22:33 -04:00
|
|
|
private Task<string> EnsureThumbsList(string file, CancellationToken cancellationToken)
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
|
|
|
const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studiothumbs.txt";
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
return EnsureList(url, file, _httpClient, _fileSystem, cancellationToken);
|
2013-12-30 11:32:01 -05:00
|
|
|
}
|
|
|
|
|
2017-04-29 02:22:33 -04:00
|
|
|
private Task<string> EnsurePosterList(string file, CancellationToken cancellationToken)
|
2013-12-30 11:32:01 -05:00
|
|
|
{
|
|
|
|
const string url = "https://raw.github.com/MediaBrowser/MediaBrowser.Resources/master/images/imagesbyname/studioposters.txt";
|
|
|
|
|
2018-09-12 13:26:21 -04:00
|
|
|
return EnsureList(url, file, _httpClient, _fileSystem, cancellationToken);
|
2013-12-30 11:32:01 -05:00
|
|
|
}
|
|
|
|
|
2019-01-13 15:31:14 -05:00
|
|
|
public int Order => 0;
|
2014-01-28 13:37:01 -05:00
|
|
|
|
|
|
|
public Task<HttpResponseInfo> GetImageResponse(string url, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
return _httpClient.GetResponse(new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
Url = url,
|
2016-10-06 14:55:01 -04:00
|
|
|
BufferContent = false
|
2014-01-28 13:37:01 -05:00
|
|
|
});
|
|
|
|
}
|
2018-09-12 13:26:21 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Ensures the list.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="url">The URL.</param>
|
|
|
|
/// <param name="file">The file.</param>
|
|
|
|
/// <param name="httpClient">The HTTP client.</param>
|
|
|
|
/// <param name="fileSystem">The file system.</param>
|
|
|
|
/// <param name="cancellationToken">The cancellation token.</param>
|
|
|
|
/// <returns>Task.</returns>
|
|
|
|
public async Task<string> EnsureList(string url, string file, IHttpClient httpClient, IFileSystem fileSystem, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var fileInfo = fileSystem.GetFileInfo(file);
|
|
|
|
|
|
|
|
if (!fileInfo.Exists || (DateTime.UtcNow - fileSystem.GetLastWriteTimeUtc(fileInfo)).TotalDays > 1)
|
|
|
|
{
|
2019-01-26 16:08:04 -05:00
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(file));
|
2018-09-12 13:26:21 -04:00
|
|
|
|
2019-07-29 17:47:25 -04:00
|
|
|
using (var res = await httpClient.SendAsync(
|
|
|
|
new HttpRequestOptions
|
|
|
|
{
|
|
|
|
CancellationToken = cancellationToken,
|
|
|
|
Url = url
|
|
|
|
},
|
|
|
|
HttpMethod.Get).ConfigureAwait(false))
|
|
|
|
using (var content = res.Content)
|
|
|
|
using (var fileStream = new FileStream(file, FileMode.Create))
|
2018-09-12 13:26:21 -04:00
|
|
|
{
|
2019-07-29 17:47:25 -04:00
|
|
|
await content.CopyToAsync(fileStream).ConfigureAwait(false);
|
2018-09-12 13:26:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
public string FindMatch(BaseItem item, IEnumerable<string> images)
|
|
|
|
{
|
|
|
|
var name = GetComparableName(item.Name);
|
|
|
|
|
|
|
|
return images.FirstOrDefault(i => string.Equals(name, GetComparableName(i), StringComparison.OrdinalIgnoreCase));
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetComparableName(string name)
|
|
|
|
{
|
|
|
|
return name.Replace(" ", string.Empty)
|
|
|
|
.Replace(".", string.Empty)
|
|
|
|
.Replace("&", string.Empty)
|
|
|
|
.Replace("!", string.Empty)
|
|
|
|
.Replace(",", string.Empty)
|
|
|
|
.Replace("/", string.Empty);
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<string> GetAvailableImages(string file, IFileSystem fileSystem)
|
|
|
|
{
|
|
|
|
using (var fileStream = fileSystem.GetFileStream(file, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read))
|
|
|
|
{
|
|
|
|
using (var reader = new StreamReader(fileStream))
|
|
|
|
{
|
|
|
|
var lines = new List<string>();
|
|
|
|
|
|
|
|
while (!reader.EndOfStream)
|
|
|
|
{
|
|
|
|
var text = reader.ReadLine();
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(text))
|
|
|
|
{
|
|
|
|
lines.Add(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return lines;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-30 11:32:01 -05:00
|
|
|
}
|
|
|
|
}
|