using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Kyoo.Abstractions.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 Show
{
Slug = result.Slug,
Title = result.SeriesName,
Aliases = result.Aliases,
Overview = result.Overview,
Status = _GetStatus(result.Status),
StartAir = _ParseDate(result.FirstAired),
Images = new Dictionary
{
[Images.Poster] = !string.IsNullOrEmpty(result.Poster)
? $"https://www.thetvdb.com{result.Poster}"
: null,
},
ExternalIDs = new[]
{
new MetadataID
{
DataID = result.Id.ToString(),
Link = $"https://www.thetvdb.com/series/{result.Slug}",
Provider = 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 Show
{
Slug = series.Slug,
Title = series.SeriesName,
Aliases = series.Aliases,
Overview = series.Overview,
Status = _GetStatus(series.Status),
StartAir = _ParseDate(series.FirstAired),
Images = new Dictionary
{
[Images.Poster] = !string.IsNullOrEmpty(series.Poster)
? $"https://www.thetvdb.com/banners/{series.Poster}"
: null,
[Images.Thumbnail] = !string.IsNullOrEmpty(series.FanArt)
? $"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}",
Provider = provider
}
}
};
}
///
/// Convert a tvdb actor to a kyoo .
///
/// The actor to convert
/// A people role representing the given actor in the role they played.
public static PeopleRole ToPeopleRole(this Actor actor)
{
return new PeopleRole
{
People = new People
{
Slug = Utility.ToSlug(actor.Name),
Name = actor.Name,
Images = new Dictionary
{
[Images.Poster] = !string.IsNullOrEmpty(actor.Image)
? $"https://www.thetvdb.com/banners/{actor.Image}"
: null
}
},
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 Episode
{
SeasonNumber = episode.AiredSeason,
EpisodeNumber = episode.AiredEpisodeNumber,
AbsoluteNumber = episode.AbsoluteNumber,
Title = episode.EpisodeName,
Overview = episode.Overview,
Images = new Dictionary
{
[Images.Thumbnail] = !string.IsNullOrEmpty(episode.Filename)
? $"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}",
Provider = provider
}
}
};
}
}
}