Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arabcoders committed Mar 22, 2024
1 parent 33041f1 commit 3410065
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 104 deletions.
32 changes: 18 additions & 14 deletions YTINFOReader/Helpers/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ public static string GetVideoInfoPath(IServerApplicationPaths appPaths, string y
public static YTDLData ReadYTDLInfo(string fpath, FileSystemMetadata path, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();

string jsonString = File.ReadAllText(fpath);

YTDLData data = JsonSerializer.Deserialize<YTDLData>(jsonString, JSON_OPTS);
Expand All @@ -128,10 +129,12 @@ public static YTDLData ReadYTDLInfo(string fpath, FileSystemMetadata path, Cance
/// Provides a Movie Metadata Result from a json object.
/// </summary>
/// <param name="json"></param>
/// <param name="name"></param>
/// <returns></returns>
public static MetadataResult<Movie> YTDLJsonToMovie(YTDLData json)
public static MetadataResult<Movie> YTDLJsonToMovie(YTDLData json, string name = "")
{
Logger?.LogDebug($"Processing movie data '{json}'.");
Logger?.LogDebug($"{name} Processing: '{json}'.");

var item = new Movie();
var result = new MetadataResult<Movie>
{
Expand All @@ -145,9 +148,7 @@ public static MetadataResult<Movie> YTDLJsonToMovie(YTDLData json)
{
date = DateTime.ParseExact(json.Upload_date, "yyyyMMdd", null);
}
catch
{
}
catch { }
result.Item.ProductionYear = date.Year;
result.Item.PremiereDate = date;
result.AddPerson(CreatePerson(json.Uploader.Trim(), json.Channel_id));
Expand All @@ -160,10 +161,11 @@ public static MetadataResult<Movie> YTDLJsonToMovie(YTDLData json)
/// Provides a MusicVideo Metadata Result from a json object.
/// </summary>
/// <param name="json"></param>
/// <param name="name"></param>
/// <returns></returns>
public static MetadataResult<MusicVideo> YTDLJsonToMusicVideo(YTDLData json)
public static MetadataResult<MusicVideo> YTDLJsonToMusicVideo(YTDLData json, string name = "")
{
Logger?.LogDebug($"Processing music video data '{json}'.");
Logger?.LogDebug($"{name} processing: '{json}'.");
var item = new MusicVideo();
var result = new MetadataResult<MusicVideo>
{
Expand Down Expand Up @@ -201,10 +203,11 @@ public static MetadataResult<MusicVideo> YTDLJsonToMusicVideo(YTDLData json)
/// Provides a Episode Metadata Result from a json object.
/// </summary>
/// <param name="json"></param>
/// <param name="name"></param>
/// <returns></returns>
public static MetadataResult<Episode> YTDLJsonToEpisode(YTDLData json)
public static MetadataResult<Episode> YTDLJsonToEpisode(YTDLData json, string name = "")
{
Logger?.LogDebug($"Processing episode data '{json}'.");
Logger?.LogDebug($"{name} Processing: '{json}'.");
var item = new Episode();
var result = new MetadataResult<Episode>
{
Expand Down Expand Up @@ -232,20 +235,20 @@ public static MetadataResult<Episode> YTDLJsonToEpisode(YTDLData json)
// if no file was found, use epoch time.
if (json.Epoch != null)
{
Logger?.LogDebug($"Using epoch for episode index number for {json.Id} {json.Title}.");
Logger?.LogDebug($"{name} Using epoch for episode index number for '{json.Id}' - '{json.Title}'.");
result.Item.IndexNumber = int.Parse("1" + date.ToString("MMdd") + DateTimeOffset.FromUnixTimeSeconds(json.Epoch ?? new long()).ToString("mmss"));
}

// append file last write time to index number if available.
if (json.Epoch == null && json.File_path != null)
{
Logger?.LogDebug($"Using file last write time for episode index number for {json.Id} {json.Title}.");
Logger?.LogDebug($"{name} Using file last write time for episode index number for '{json.Id}'- '{json.Title}'.");
result.Item.IndexNumber = int.Parse("1" + date.ToString("MMdd") + json.File_path.LastWriteTimeUtc.ToString("mmss"));
}

if (json.File_path == null && json.Epoch == null)
{
Logger?.LogError($"No file or epoch data found for e{json.Id} {json.Title}.");
Logger?.LogError($"{name} No file or epoch data found for '{json.Id}' - '{json.Title}'.");
}

return result;
Expand All @@ -255,10 +258,11 @@ public static MetadataResult<Episode> YTDLJsonToEpisode(YTDLData json)
/// Provides a MusicVideo Metadata Result from a json object.
/// </summary>
/// <param name="json"></param>
/// <param name="name"></param>
/// <returns></returns>
public static MetadataResult<Series> YTDLJsonToSeries(YTDLData json)
public static MetadataResult<Series> YTDLJsonToSeries(YTDLData json, string name = "")
{
Logger?.LogDebug($"Processing series data '{json}'.");
Logger?.LogDebug($"{name} Processing: '{json}'.");
var item = new Series();
var result = new MetadataResult<Series>
{
Expand Down
18 changes: 10 additions & 8 deletions YTINFOReader/Provider/AbstractProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,50 +27,52 @@ public AbstractProvider(IFileSystem fileSystem, ILogger<B> logger)
Utils.Logger = logger;
}

public string Name => Constants.PLUGIN_NAME;
public abstract string Name { get; }

public virtual Task<MetadataResult<T>> GetMetadata(E info, CancellationToken cancellationToken)
{
MetadataResult<T> result = new();

if (!Utils.IsYouTubeContent(info.Path))
{
_logger.LogDebug("YIR GetMetadata: is not youtube content [{Path}].", info.Path);
_logger.LogDebug($"{Name} GetMetadata: is not youtube content '{info.Path}'.");
return Task.FromResult(result);
}

_logger.LogDebug("YIR GetMetadata: {Path}", info.Path);
_logger.LogDebug($"{Name} GetMetadata: '{info.Path}'.");

var infoFile = Path.ChangeExtension(info.Path, "info.json");

if (!File.Exists(infoFile))
{
_logger.LogDebug("YIR GetMetadata: No json file was found for [{Path}].", info.Path);
_logger.LogDebug($"{Name} GetMetadata: No json file was found for '{info.Path}'.");
return Task.FromResult(result);
}

var jsonObj = Utils.ReadYTDLInfo(infoFile, _fileSystem.GetFileSystemInfo(info.Path), cancellationToken);
_logger.LogDebug("YIR GetMetadata Result: {JSON}", jsonObj.ToString());

_logger.LogDebug($"{Name} GetMetadata Result: '{jsonObj}'.");

return Task.FromResult(GetMetadataImpl(jsonObj));
}

public virtual bool HasChanged(BaseItem item, IDirectoryService directoryService)
{
_logger.LogDebug("YIR HasChanged: {Path}", item.Path);
_logger.LogDebug($"{Name} HasChanged: '{item.Path}'.");

var infoFile = Path.ChangeExtension(item.Path, "info.json");

if (!File.Exists(infoFile))
{
_logger.LogDebug("YIR HasChanged: No json file was found for [{Path}].", item.Path);
_logger.LogDebug($"{Name} HasChanged: No json file was found for '{item.Path}'.");
return false;
}

FileSystemMetadata infoJson = directoryService.GetFile(infoFile);
bool result = infoJson.Exists && infoJson.LastWriteTimeUtc.ToUniversalTime() > item.DateLastSaved.ToUniversalTime();
string status = result ? "Has Changed" : "Has Not Changed";

_logger.LogDebug("YIR HasChanged Result: {status}", status);
_logger.LogDebug($"{Name} HasChanged Result: '{status}'.");

return result;
}
Expand Down
2 changes: 2 additions & 0 deletions YTINFOReader/Provider/EpisodeProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ public class EpisodeProvider : AbstractProvider<EpisodeProvider, Episode, Episod
{
public EpisodeProvider(IFileSystem fileSystem, ILogger<EpisodeProvider> logger) : base(fileSystem, logger) { }

public override string Name => $"{Constants.PLUGIN_NAME}: Episode Provider";

internal override MetadataResult<Episode> GetMetadataImpl(YTDLData jsonObj) => Utils.YTDLJsonToEpisode(jsonObj);
}
76 changes: 47 additions & 29 deletions YTINFOReader/Provider/ImageProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using MediaBrowser.Model.Providers;
using System.Threading;
using System.Net.Http;
using System.IO;

namespace YTINFOReader;

Expand All @@ -20,36 +21,61 @@ public class ImageProvider : IRemoteImageProvider
private readonly ILogger<ImageProvider> _logger;
private readonly string[] _supportedExtensions = { ".jpg", ".jpeg", ".png", ".webp" };

public string Name => Constants.PLUGIN_NAME;
private readonly Matcher _matcher = new();

public string Name => $"{Constants.PLUGIN_NAME}: Image Provider";

public ImageProvider(IFileSystem fileSystem, ILogger<ImageProvider> logger)
{
_logger = logger;
Utils.Logger = logger;
_fileSystem = fileSystem;

for (int i = 0; i < _supportedExtensions.Length; i++)
{
_matcher.AddInclude($"**/*{_supportedExtensions[i]}");
}

}

public bool Supports(BaseItem item) => item is Series || item is Episode;

private string GetSeriesInfo(string path)
private string GetInfo(BaseItem item)
{
_logger.LogDebug("YIR Series Image GetSeriesInfo: {Path}", path);
Matcher matcher = new();
for (int i = 0; i < _supportedExtensions.Length; i++)
var path = item is Episode ? Path.GetDirectoryName(item.Path) : item.Path;

_logger.LogDebug($"{Name} GetInfo: '{path}'.");

if (string.IsNullOrEmpty(path))
{
matcher.AddInclude($"**/*{_supportedExtensions[i]}");
return "";
}

string infoPath = "";
foreach (string file in matcher.GetResultsInFullPath(path))

foreach (string file in _matcher.GetResultsInFullPath(path))
{
if (Utils.RX_C.IsMatch(file) || Utils.RX_P.IsMatch(file))
switch (item)
{
infoPath = file;
break;
default:
break;
case Series:
if (Utils.RX_C.IsMatch(file) || Utils.RX_P.IsMatch(file))
{
infoPath = file;
}
break;
case Episode:
if (Utils.RX_V.IsMatch(file))
{
infoPath = file;
}
break;
}
}
_logger.LogDebug("YIR Series Image GetSeriesInfo Result: {InfoPath}", infoPath);

_logger.LogDebug($"{Name} GetInfo Result: '{infoPath}'.");

return infoPath;
}

Expand All @@ -61,39 +87,31 @@ private string GetSeriesInfo(string path)
/// <returns>A list of local image information for the specified item.</returns>
public IEnumerable<LocalImageInfo> GetImages(BaseItem item, IDirectoryService directoryService)
{
_logger.LogDebug("YIR Series Image GetImages: {Name}", item.Name);
_logger.LogDebug($"{Name} GetImages: {item.Name}");
var list = new List<LocalImageInfo>();

if (!Utils.IsYouTubeContent(item.Path))
{
return list;
}

string jpgPath = GetSeriesInfo(item.Path);
string jpgPath = GetInfo(item);
if (string.IsNullOrEmpty(jpgPath))
{
return list;
}
var localImg = new LocalImageInfo();

var fileInfo = _fileSystem.GetFileSystemInfo(jpgPath);
localImg.FileInfo = fileInfo;
list.Add(localImg);
_logger.LogDebug("YIR Series Image GetImages Result: {Result}", list.ToString());
list.Add(new LocalImageInfo { FileInfo = fileInfo });

_logger.LogDebug($"{Name} GetImages Result: {list}");

return list;
}

public IEnumerable<ImageType> GetSupportedImages(BaseItem item)
{
return new List<ImageType> { ImageType.Primary };
}
public IEnumerable<ImageType> GetSupportedImages(BaseItem item) => new List<ImageType> { ImageType.Primary };

public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken)
{
throw new System.NotImplementedException();
}
public Task<IEnumerable<RemoteImageInfo>> GetImages(BaseItem item, CancellationToken cancellationToken) => throw new System.NotImplementedException();

public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken)
{
throw new System.NotImplementedException();
}
public Task<HttpResponseMessage> GetImageResponse(string url, CancellationToken cancellationToken) => throw new System.NotImplementedException();
}
43 changes: 0 additions & 43 deletions YTINFOReader/Provider/LocalSeasonProvider.cs

This file was deleted.

1 change: 1 addition & 0 deletions YTINFOReader/Provider/MovieProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace YTINFOReader;
public class MovieProvider : AbstractProvider<MovieProvider, Movie, MovieInfo>
{
public MovieProvider(IFileSystem fileSystem, ILogger<MovieProvider> logger) : base(fileSystem, logger) { }
public override string Name => $"{Constants.PLUGIN_NAME}: Movie Provider";

internal override MetadataResult<Movie> GetMetadataImpl(YTDLData jsonObj) => Utils.YTDLJsonToMovie(jsonObj);
}
1 change: 1 addition & 0 deletions YTINFOReader/Provider/MusicProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace YTINFOReader;
public class MusicProvider : AbstractProvider<MusicProvider, MusicVideo, MusicVideoInfo>
{
public MusicProvider(IFileSystem fileSystem, ILogger<MusicProvider> logger) : base(fileSystem, logger) { }
public override string Name => $"{Constants.PLUGIN_NAME}: Music Provider";

internal override MetadataResult<MusicVideo> GetMetadataImpl(YTDLData jsonObj) => Utils.YTDLJsonToMusicVideo(jsonObj);
}
Loading

0 comments on commit 3410065

Please sign in to comment.