diff --git a/Kyoo/InternalAPI/Crawler/Crawler.cs b/Kyoo/InternalAPI/Crawler/Crawler.cs index 01d01f8d..db470e5d 100644 --- a/Kyoo/InternalAPI/Crawler/Crawler.cs +++ b/Kyoo/InternalAPI/Crawler/Crawler.cs @@ -54,17 +54,17 @@ namespace Kyoo.InternalAPI Regex regex = new Regex(patern, RegexOptions.IgnoreCase); Match match = regex.Match(file); - string ShowPath = Path.GetDirectoryName(file); - string ShowTitle = match.Groups["ShowTitle"].Value; + string showPath = Path.GetDirectoryName(file); + string showName = match.Groups["ShowTitle"].Value; bool seasonSuccess = long.TryParse(match.Groups["Season"].Value, out long seasonNumber); bool episodeSucess = long.TryParse(match.Groups["Episode"].Value, out long episodeNumber); - Debug.WriteLine("&ShowPath: " + ShowPath + " Show: " + ShowTitle + " season: " + seasonNumber + " episode: " + episodeNumber); + Debug.WriteLine("&ShowPath: " + showPath + " Show: " + showName + " season: " + seasonNumber + " episode: " + episodeNumber); - if (!libraryManager.IsShowRegistered(ShowPath, out long showID)) + if (!libraryManager.IsShowRegistered(showPath, out long showID)) { - Debug.WriteLine("&Should register show: " + ShowTitle); - Show show = await metadataProvider.GetShowFromName(ShowTitle, ShowPath); + Debug.WriteLine("&Should register show: " + showName); + Show show = await metadataProvider.GetShowFromName(showName, showPath); showID = libraryManager.RegisterShow(show); } @@ -72,8 +72,9 @@ namespace Kyoo.InternalAPI if(!libraryManager.IsSeasonRegistered(showID, seasonNumber, out long seasonID)) { - Debug.WriteLine("&Should register season: " + ShowTitle + " - " + seasonNumber); - Season season = await metadataProvider.GetSeason(showID, seasonNumber); + Debug.WriteLine("&Should register season: " + showName + " - " + seasonNumber); + Season season = await metadataProvider.GetSeason(showName, seasonNumber); + season.ShowID = showID; showID = libraryManager.RegisterSeason(season); } diff --git a/Kyoo/InternalAPI/MetadataProvider/IMetadataProvider.cs b/Kyoo/InternalAPI/MetadataProvider/IMetadataProvider.cs index 7c95cec0..970c393a 100644 --- a/Kyoo/InternalAPI/MetadataProvider/IMetadataProvider.cs +++ b/Kyoo/InternalAPI/MetadataProvider/IMetadataProvider.cs @@ -13,6 +13,8 @@ namespace Kyoo.InternalAPI Task GetImages(Show show); //For the seasons - Task GetSeason(string showName, int seasonNumber); + Task GetSeason(string showName, long seasonNumber); + + Task GetSeasonImage(string showName, long seasonNumber); } } diff --git a/Kyoo/InternalAPI/MetadataProvider/Implementations/TheTvDB/ProviderTheTvDB.cs b/Kyoo/InternalAPI/MetadataProvider/Implementations/TheTvDB/ProviderTheTvDB.cs index 85c85c89..e78b73a2 100644 --- a/Kyoo/InternalAPI/MetadataProvider/Implementations/TheTvDB/ProviderTheTvDB.cs +++ b/Kyoo/InternalAPI/MetadataProvider/Implementations/TheTvDB/ProviderTheTvDB.cs @@ -215,43 +215,55 @@ namespace Kyoo.InternalAPI.MetadataProvider foreach (KeyValuePair type in imageTypes) { - WebRequest request = WebRequest.Create("https://api.thetvdb.com/series/" + id + "/images/query?keyType=" + type.Value); - request.Method = "GET"; - request.Timeout = 12000; - request.ContentType = "application/json"; - request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token); - - HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); - - if (response.StatusCode == HttpStatusCode.OK) + try { - Stream stream = response.GetResponseStream(); - using (StreamReader reader = new StreamReader(stream)) - { - string content = await reader.ReadToEndAsync(); - stream.Close(); - response.Close(); + WebRequest request = WebRequest.Create("https://api.thetvdb.com/series/" + id + "/images/query?keyType=" + type.Value); + request.Method = "GET"; + request.Timeout = 12000; + request.ContentType = "application/json"; + request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token); - var model = new { data = new ImageTvDb[0], error = new ErrorsTvDB() }; - //Should implement language selection here - ImageTvDb data = JsonConvert.DeserializeAnonymousType(content, model).data.OrderByDescending(x => x.ratingsInfo.average).ThenByDescending(x => x.ratingsInfo.count).FirstOrDefault(); - IEnumerable datas = JsonConvert.DeserializeAnonymousType(content, model).data.OrderByDescending(x => x.ratingsInfo.average).ThenByDescending(x => x.ratingsInfo.count); - SetImage(show, "https://www.thetvdb.com/banners/" + data.fileName, type.Key); + HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); + + if (response.StatusCode == HttpStatusCode.OK) + { + Stream stream = response.GetResponseStream(); + using (StreamReader reader = new StreamReader(stream)) + { + string content = await reader.ReadToEndAsync(); + stream.Close(); + response.Close(); + + var model = new { data = new ImageTvDb[0], error = new ErrorsTvDB() }; + //Should implement language selection here + ImageTvDb data = JsonConvert.DeserializeAnonymousType(content, model).data.OrderByDescending(x => x.ratingsInfo.average).ThenByDescending(x => x.ratingsInfo.count).FirstOrDefault(); + IEnumerable datas = JsonConvert.DeserializeAnonymousType(content, model).data.OrderByDescending(x => x.ratingsInfo.average).ThenByDescending(x => x.ratingsInfo.count); + SetImage(show, "https://www.thetvdb.com/banners/" + data.fileName, type.Key); + } + } + else + { + Debug.WriteLine("&TheTvDB Provider couldn't get " + type + " for the show with the id: " + id + ".\nError Code: " + response.StatusCode + " Message: " + response.StatusDescription); + response.Close(); } } - else + catch (WebException ex) { - Debug.WriteLine("&TheTvDB Provider couldn't get " + type + " for the show with the id: " + id + ".\nError Code: " + response.StatusCode + " Message: " + response.StatusDescription); - response.Close(); + Debug.WriteLine("&TheTvDB Provider couldn't get " + type + " for the show with the id: " + id + ".\nError Code: " + ex.Status); } } return show; } - public Task GetSeason(string showName, int seasonNumber) + public Task GetSeason(string showName, long seasonNumber) { - throw new NotImplementedException(); + return new Season(-1, -1, seasonNumber, "Season " + seasonNumber, null, null, null, null); + } + + public Task GetSeasonImage(string showName, long seasonNumber) + { + return null; } } } diff --git a/Kyoo/InternalAPI/MetadataProvider/ProviderManager.cs b/Kyoo/InternalAPI/MetadataProvider/ProviderManager.cs index 83299b26..ce34186f 100644 --- a/Kyoo/InternalAPI/MetadataProvider/ProviderManager.cs +++ b/Kyoo/InternalAPI/MetadataProvider/ProviderManager.cs @@ -3,6 +3,7 @@ using Kyoo.Models; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Reflection; using System.Threading.Tasks; @@ -25,32 +26,37 @@ namespace Kyoo.InternalAPI providers.Clear(); providers.Add(new ProviderTheTvDB()); - string[] pluginsPaths = Directory.GetFiles(config.GetValue("providerPlugins")); - List plugins = new List(); - List types = new List(); + string pluginFolder = config.GetValue("providerPlugins"); - for (int i = 0; i < pluginsPaths.Length; i++) + if (Directory.Exists(pluginFolder)) { - plugins.Add(Assembly.LoadFile(pluginsPaths[i])); - types.AddRange(plugins[i].GetTypes()); - } + string[] pluginsPaths = Directory.GetFiles(pluginFolder); + List plugins = new List(); + List types = new List(); - List providersPlugins = types.FindAll(x => - { - object[] atr = x.GetCustomAttributes(typeof(MetaProvider), false); + for (int i = 0; i < pluginsPaths.Length; i++) + { + plugins.Add(Assembly.LoadFile(pluginsPaths[i])); + types.AddRange(plugins[i].GetTypes()); + } + + List providersPlugins = types.FindAll(x => + { + object[] atr = x.GetCustomAttributes(typeof(MetaProvider), false); + + if (atr == null || atr.Length == 0) + return false; + + List interfaces = new List(x.GetInterfaces()); + + if (interfaces.Contains(typeof(IMetadataProvider))) + return true; - if (atr == null || atr.Length == 0) return false; + }); - List interfaces = new List(x.GetInterfaces()); - - if (interfaces.Contains(typeof(IMetadataProvider))) - return true; - - return false; - }); - - providers.AddRange(providersPlugins.ConvertAll(x => Activator.CreateInstance(x) as IMetadataProvider)); + providers.AddRange(providersPlugins.ConvertAll(x => Activator.CreateInstance(x) as IMetadataProvider)); + } } //public Show MergeShows(Show baseShow, Show newShow) @@ -80,5 +86,15 @@ namespace Kyoo.InternalAPI { return providers[0].GetShowFromName(showName, showPath); } + + public Task GetSeason(string showName, long seasonNumber) + { + return providers[0].GetSeason(showName, seasonNumber); + } + + public Task GetSeasonImage(string showName, long seasonNumber) + { + return providers[0].GetSeasonImage(showName, seasonNumber); + } } } diff --git a/Kyoo/Models/Season.cs b/Kyoo/Models/Season.cs index e428ce41..26da948d 100644 --- a/Kyoo/Models/Season.cs +++ b/Kyoo/Models/Season.cs @@ -3,14 +3,28 @@ public class Season { public readonly long id; - public readonly long ShowID; + public long ShowID; public long seasonNumber; public string Title; public string Overview; - public long year; + public long? year; public string ImgPrimary; public string ExternalIDs; + + public Season() { } + + public Season(long id, long showID, long seasonNumber, string title, string overview, long? year, string imgPrimary, string externalIDs) + { + this.id = id; + ShowID = showID; + this.seasonNumber = seasonNumber; + Title = title; + Overview = overview; + this.year = year; + ImgPrimary = imgPrimary; + ExternalIDs = externalIDs; + } } }