using System;
using System.Globalization;
using System.Linq;
using Kyoo.Models;
using TvDbSharper.Dto;
namespace Kyoo.TheTvdb
{
///
/// A set of extensions methods used to convert tvdb models to Kyoo models.
///
public static class Convertors
{
///
/// Convert the string representation of the status in the tvdb API to a Kyoo's enum.
///
/// The string representing the status.
/// A kyoo value or null.
private static Status _GetStatus(string status)
{
return status switch
{
"Ended" => Status.Finished,
"Continuing" => Status.Airing,
_ => Status.Unknown
};
}
///
/// Parse a TVDB date and return a or null if the string is invalid.
///
/// The date string to parse
/// The parsed or null.
private static DateTime? _ParseDate(string date)
{
return DateTime.TryParseExact(date, "yyyy-MM-dd", CultureInfo.InvariantCulture,
DateTimeStyles.None, out DateTime parsed)
? parsed
: null;
}
///
/// Convert a series search to a show.
///
/// The search result
/// The provider representing the tvdb inside kyoo
/// A show representing the given search result.
public static Show ToShow(this SeriesSearchResult result, Provider provider)
{
return new()
{
Slug = result.Slug,
Title = result.SeriesName,
Aliases = result.Aliases,
Overview = result.Overview,
Status = _GetStatus(result.Status),
StartAir = _ParseDate(result.FirstAired),
Poster = result.Poster != null ? $"https://www.thetvdb.com{result.Poster}" : null,
ExternalIDs = new[]
{
new MetadataID
{
DataID = result.Id.ToString(),
Link = $"https://www.thetvdb.com/series/{result.Slug}",
Second = provider
}
}
};
}
///
/// Convert a tvdb series to a kyoo show.
///
/// The series to convert
/// The provider representing the tvdb inside kyoo
/// A show representing the given series.
public static Show ToShow(this Series series, Provider provider)
{
return new()
{
Slug = series.Slug,
Title = series.SeriesName,
Aliases = series.Aliases,
Overview = series.Overview,
Status = _GetStatus(series.Status),
StartAir = _ParseDate(series.FirstAired),
Poster = series.Poster != null ? $"https://www.thetvdb.com/banners/{series.Poster}" : null,
Backdrop = series.FanArt != null ? $"https://www.thetvdb.com/banners/{series.FanArt}" : null,
Genres = series.Genre.Select(y => new Genre(y)).ToList(),
ExternalIDs = new[]
{
new MetadataID
{
DataID = series.Id.ToString(),
Link = $"https://www.thetvdb.com/series/{series.Slug}",
Second = provider
}
}
};
}
///
/// Convert a tvdb actor to a kyoo .
///
/// The actor to convert
/// The provider representing the tvdb inside kyoo
/// A people role representing the given actor in the role they played.
public static PeopleRole ToPeopleRole(this Actor actor, Provider provider)
{
return new()
{
People = new People
{
Slug = Utility.ToSlug(actor.Name),
Name = actor.Name,
Poster = actor.Image != null ? $"https://www.thetvdb.com/banners/{actor.Image}" : null,
ExternalIDs = new []
{
new MetadataID()
{
DataID = actor.Id.ToString(),
Link = $"https://www.thetvdb.com/people/{actor.Id}",
Second = provider
}
}
},
Role = actor.Role,
Type = "Actor"
};
}
///
/// Convert a tvdb episode to a kyoo .
///
/// The episode to convert
/// The provider representing the tvdb inside kyoo
/// A episode representing the given tvdb episode.
public static Episode ToEpisode(this EpisodeRecord episode, Provider provider)
{
return new()
{
SeasonNumber = episode.AiredSeason,
EpisodeNumber = episode.AiredEpisodeNumber,
AbsoluteNumber = episode.AbsoluteNumber,
Title = episode.EpisodeName,
Overview = episode.Overview,
Thumb = episode.Filename != null ? $"https://www.thetvdb.com/banners/{episode.Filename}" : null,
ExternalIDs = new[]
{
new MetadataID
{
DataID = episode.Id.ToString(),
Link = $"https://www.thetvdb.com/series/{episode.SeriesId}/episodes/{episode.Id}",
Second = provider
}
}
};
}
}
}