Skip to content

Commit

Permalink
Merge pull request #1824 from mregni/develop
Browse files Browse the repository at this point in the history
Small fixes
  • Loading branch information
mregni authored Jun 20, 2022
2 parents c2c9903 + 6aff24f commit e6c52bd
Show file tree
Hide file tree
Showing 79 changed files with 5,025 additions and 230 deletions.
18 changes: 17 additions & 1 deletion EmbyStat.Clients.Base/ClientStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using EmbyStat.Clients.Base.Http;
using EmbyStat.Clients.Base.Metadata;
using EmbyStat.Clients.Base.WebSocket;
using EmbyStat.Common.Enums;

Expand All @@ -10,10 +11,12 @@ namespace EmbyStat.Clients.Base;
public class ClientStrategy : IClientStrategy
{
private readonly IEnumerable<IClientFactory> _httpFactories;
private readonly IEnumerable<IMetadataClientFactory> _metadataHttpClientFactories;

public ClientStrategy(IEnumerable<IClientFactory> httpFactories)
public ClientStrategy(IEnumerable<IClientFactory> httpFactories, IEnumerable<IMetadataClientFactory> metadataHttpClientFactories)
{
_httpFactories = httpFactories;
_metadataHttpClientFactories = metadataHttpClientFactories;
}

public IBaseHttpClient CreateHttpClient(ServerType type)
Expand All @@ -39,4 +42,17 @@ public IWebSocketClient CreateWebSocketClient(ServerType type)

return factory.CreateWebSocketClient();
}

public IMetadataClient CreateMetadataClient(MetadataServerType type)
{
var factory = _metadataHttpClientFactories
.FirstOrDefault(x => x.AppliesTo(type));

if (factory == null)
{
throw new TypeLoadException("type not registered");
}

return factory.CreateClient();
}
}
41 changes: 35 additions & 6 deletions EmbyStat.Clients.Base/Http/BaseHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
Expand Down Expand Up @@ -228,7 +229,7 @@ public async Task<T[]> GetMedia<T>(string parentId, int startIndex, int limit, D
return _mapper.Map<T[]>(result.Items);
}

public async Task<Show[]> GetShows(string parentId, int startIndex, int limit, DateTime? lastSynced)
public Task<Show[]> GetShows(string parentId, int startIndex, int limit, DateTime? lastSynced)
{
var query = new ItemQuery
{
Expand All @@ -251,6 +252,36 @@ public async Task<Show[]> GetShows(string parentId, int startIndex, int limit, D
}
};

return GetShows(query);
}

public Task<Show[]> GetShows(string[] showIds, int startIndex, int limit)
{
var query = new ItemQuery
{
Ids = showIds,
UserId = UserId,
EnableImageTypes = new[] {ImageType.Banner, ImageType.Primary, ImageType.Thumb, ImageType.Logo},
LocationTypes = new[] {LocationType.FileSystem},
Recursive = true,
StartIndex = startIndex,
Limit = limit,
IncludeItemTypes = new[] {"Series"},
Fields = new[]
{
ItemFields.OriginalTitle, ItemFields.Genres, ItemFields.DateCreated, ItemFields.ExternalUrls,
ItemFields.Studios, ItemFields.Path, ItemFields.ProviderIds,
ItemFields.SortName, ItemFields.ParentId, ItemFields.People, ItemFields.PremiereDate,
ItemFields.CommunityRating, ItemFields.OfficialRating, ItemFields.ProductionYear,
ItemFields.Status, ItemFields.RunTimeTicks
}
};

return GetShows(query);
}

private async Task<Show[]> GetShows(ItemQuery query)
{
var paramList = query.ConvertToStringDictionary();
var client = _refitFactory.CreateClient(BaseUrl);
var result = await client.GetItems(ApiKey, AuthorizationString, paramList);
Expand All @@ -259,7 +290,7 @@ public async Task<Show[]> GetShows(string parentId, int startIndex, int limit, D
return shows;
}

public async Task<Season[]> GetSeasons(string parentId, DateTime? lastSynced)
public async Task<Season[]> GetSeasons(string parentId)
{
var query = new ItemQuery
{
Expand All @@ -269,7 +300,6 @@ public async Task<Season[]> GetSeasons(string parentId, DateTime? lastSynced)
LocationTypes = new[] {LocationType.FileSystem},
Recursive = true,
IncludeItemTypes = new[] {nameof(Season)},
MinDateLastSaved = lastSynced,
Fields = new[]
{
ItemFields.OriginalTitle, ItemFields.Genres, ItemFields.DateCreated, ItemFields.ExternalUrls,
Expand All @@ -288,7 +318,7 @@ public async Task<Season[]> GetSeasons(string parentId, DateTime? lastSynced)
return seasons;
}

public async Task<Episode[]> GetEpisodes(string parentId, DateTime? lastSynced)
public async Task<Episode[]> GetEpisodes(string parentId)
{
var query = new ItemQuery
{
Expand All @@ -298,7 +328,6 @@ public async Task<Episode[]> GetEpisodes(string parentId, DateTime? lastSynced)
LocationTypes = new[] {LocationType.FileSystem},
Recursive = true,
IncludeItemTypes = new[] {nameof(Episode)},
MinDateLastSaved = lastSynced,
Fields = new[]
{
ItemFields.OriginalTitle, ItemFields.Genres, ItemFields.DateCreated, ItemFields.ExternalUrls,
Expand All @@ -312,7 +341,7 @@ public async Task<Episode[]> GetEpisodes(string parentId, DateTime? lastSynced)
var paramList = query.ConvertToStringDictionary();
var client = _refitFactory.CreateClient(BaseUrl);
var result = await client.GetItems(ApiKey, AuthorizationString, paramList);

var episodes = _mapper.Map<Episode[]>(result.Items);
return episodes;
}
Expand Down
5 changes: 3 additions & 2 deletions EmbyStat.Clients.Base/Http/IBaseHttpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ public interface IBaseHttpClient
Task<int> GetPeopleCount();

Task<Show[]> GetShows(string parentId, int startIndex, int limit, DateTime? lastSynced);
Task<Season[]> GetSeasons(string parentId, DateTime? lastSynced);
Task<Episode[]> GetEpisodes(string parentId, DateTime? lastSynced);
Task<Show[]> GetShows(string[] showIds, int startIndex, int limit);
Task<Season[]> GetSeasons(string parentId);
Task<Episode[]> GetEpisodes(string parentId);

Task<List<PluginInfo>> GetInstalledPlugins();
Task<MediaServerInfo> GetServerInfo();
Expand Down
2 changes: 2 additions & 0 deletions EmbyStat.Clients.Base/IClientStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EmbyStat.Clients.Base.Http;
using EmbyStat.Clients.Base.Metadata;
using EmbyStat.Clients.Base.WebSocket;
using EmbyStat.Common.Enums;

Expand All @@ -8,4 +9,5 @@ public interface IClientStrategy
{
IBaseHttpClient CreateHttpClient(ServerType type);
IWebSocketClient CreateWebSocketClient(ServerType type);
IMetadataClient CreateMetadataClient(MetadataServerType type);
}
10 changes: 10 additions & 0 deletions EmbyStat.Clients.Base/IMetadataClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using EmbyStat.Clients.Base.Metadata;
using EmbyStat.Common.Enums;

namespace EmbyStat.Clients.Base;

public interface IMetadataClientFactory
{
IMetadataClient CreateClient();
bool AppliesTo(MetadataServerType type);
}
10 changes: 10 additions & 0 deletions EmbyStat.Clients.Base/Metadata/IMetadataClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EmbyStat.Common.Models.Show;

namespace EmbyStat.Clients.Base.Metadata;

public interface IMetadataClient
{
Task<IEnumerable<VirtualEpisode>> GetEpisodesAsync(int? id);
}
1 change: 1 addition & 0 deletions EmbyStat.Clients.Tmdb/EmbyStat.Clients.Tmdb.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EmbyStat.Clients.Base\EmbyStat.Clients.Base.csproj" />
<ProjectReference Include="..\EmbyStat.Common\EmbyStat.Common.csproj" />
<ProjectReference Include="..\EmbyStat.Configuration\EmbyStat.Configuration.csproj" />
</ItemGroup>
Expand Down
8 changes: 3 additions & 5 deletions EmbyStat.Clients.Tmdb/ITmdbClient.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EmbyStat.Common.Models.Show;
using EmbyStat.Clients.Base.Metadata;

namespace EmbyStat.Clients.Tmdb;

public interface ITmdbClient
public interface ITmdbClient : IMetadataClient
{
Task<IEnumerable<VirtualEpisode>> GetEpisodesAsync(int? tmdbShowId);

}
9 changes: 5 additions & 4 deletions EmbyStat.Clients.Tmdb/TmdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using EmbyStat.Clients.Base.Metadata;
using EmbyStat.Common.Models.Show;
using EmbyStat.Configuration;
using EmbyStat.Configuration.Interfaces;
Expand All @@ -19,16 +20,16 @@ public TmdbClient(IConfigurationService configurationService, IMapper mapper)
_config = configurationService.Get();
_mapper = mapper;
}
public async Task<IEnumerable<VirtualEpisode>> GetEpisodesAsync(int? tmdbShowId)
public async Task<IEnumerable<VirtualEpisode>> GetEpisodesAsync(int? id)
{
if (!tmdbShowId.HasValue)
if (!id.HasValue)
{
return null;
}

var client = new TMDbClient(_config.UserConfig.Tmdb.ApiKey);

var show = await client.GetTvShowAsync(tmdbShowId.Value);
var show = await client.GetTvShowAsync(id.Value);
if (show == null)
{
return null;
Expand All @@ -37,7 +38,7 @@ public async Task<IEnumerable<VirtualEpisode>> GetEpisodesAsync(int? tmdbShowId)
var episodes = new List<VirtualEpisode>();
foreach (var tmdbSeason in show.Seasons)
{
var season = await client.GetTvSeasonAsync(tmdbShowId.Value, tmdbSeason.SeasonNumber);
var season = await client.GetTvSeasonAsync(id.Value, tmdbSeason.SeasonNumber);
episodes.AddRange(_mapper.Map<IList<VirtualEpisode>>(season.Episodes));
}

Expand Down
25 changes: 25 additions & 0 deletions EmbyStat.Clients.Tmdb/TmdbClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using EmbyStat.Clients.Base;
using EmbyStat.Clients.Base.Metadata;
using EmbyStat.Common.Enums;

namespace EmbyStat.Clients.Tmdb;

public class TmdbClientFactory : IMetadataClientFactory
{
private readonly ITmdbClient _client;

public TmdbClientFactory(ITmdbClient client)
{
_client = client;
}

public IMetadataClient CreateClient()
{
return _client;
}

public bool AppliesTo(MetadataServerType type)
{
return type == MetadataServerType.Tmdb;
}
}
18 changes: 18 additions & 0 deletions EmbyStat.Clients.TvMaze/EmbyStat.Clients.TvMaze.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="TvMaze.Api.Client" Version="0.1.72" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\EmbyStat.Clients.Base\EmbyStat.Clients.Base.csproj" />
<ProjectReference Include="..\EmbyStat.Clients.Tmdb\EmbyStat.Clients.Tmdb.csproj" />
</ItemGroup>

</Project>
8 changes: 8 additions & 0 deletions EmbyStat.Clients.TvMaze/ITvMazeShowClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using EmbyStat.Clients.Base.Metadata;

namespace EmbyStat.Clients.TvMaze;

public interface ITvMazeShowClient : IMetadataClient
{

}
25 changes: 25 additions & 0 deletions EmbyStat.Clients.TvMaze/TvMazeClientFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using EmbyStat.Clients.Base;
using EmbyStat.Clients.Base.Metadata;
using EmbyStat.Common.Enums;

namespace EmbyStat.Clients.TvMaze;

public class TvMazeClientFactory : IMetadataClientFactory
{
private readonly ITvMazeShowClient _showClient;

public TvMazeClientFactory(ITvMazeShowClient showClient)
{
_showClient = showClient;
}

public IMetadataClient CreateClient()
{
return _showClient;
}

public bool AppliesTo(MetadataServerType type)
{
return type == MetadataServerType.TvMaze;
}
}
43 changes: 43 additions & 0 deletions EmbyStat.Clients.TvMaze/TvMazeShowClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using AutoMapper;
using EmbyStat.Clients.Base.Metadata;
using EmbyStat.Clients.Tmdb;
using EmbyStat.Common.Models.Show;
using EmbyStat.Configuration;
using EmbyStat.Configuration.Interfaces;
using TvMaze.Api.Client;
using TvMaze.Api.Client.Models;

namespace EmbyStat.Clients.TvMaze;

public class TvMazeShowClient : ITvMazeShowClient
{
private readonly IMapper _mapper;

public TvMazeShowClient(IConfigurationService configurationService, IMapper mapper)
{
_mapper = mapper;
}

public async Task<IEnumerable<VirtualEpisode>?> GetEpisodesAsync(int? tmdbShowId)
{
if (!tmdbShowId.HasValue)
{
return null;
}

var client = new TvMazeClient();
var show = await client.Lookup.GetShowByTheTvdbIdAsync(tmdbShowId.Value);

if (show == null)
{
return null;
}

var episodes = await client.Shows.GetShowEpisodeListAsync(show.Id);
return episodes?
.Where(x => x.Type == EpisodeType.Regular)
.Select(x => _mapper.Map<VirtualEpisode>(x))
.Where(x => x.FirstAired != null && x.FirstAired < DateTime.Now);

}
}
6 changes: 3 additions & 3 deletions EmbyStat.Common/EmbyStat.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
<ItemGroup>
<PackageReference Include="AutoMapper" Version="11.0.1" />
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="MediaBrowser.Common" Version="4.7.1" />
<PackageReference Include="MediaBrowser.Common" Version="4.7.3" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.SignalR" Version="1.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.6" />
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="6.0.6" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="6.19.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Rollbar.NetCore.AspNet" Version="5.2.0" />
Expand Down
7 changes: 7 additions & 0 deletions EmbyStat.Common/Enums/MetadataServerType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace EmbyStat.Common.Enums;

public enum MetadataServerType
{
Tmdb = 0,
TvMaze = 1
}
Loading

0 comments on commit e6c52bd

Please sign in to comment.