private async Task <Series> AutoDetectSeries( string seriesName, int?seriesYear, TvFileOrganizationOptions options, CancellationToken cancellationToken) { if (options.AutoDetectSeries) { RemoteSearchResult finalResult = null; // Perform remote search var seriesInfo = new SeriesInfo { Name = seriesName, Year = seriesYear }; var searchQuery = new RemoteSearchQuery <SeriesInfo> { SearchInfo = seriesInfo }; var searchResults = await _providerManager.GetRemoteSearchResults <Series, SeriesInfo>(searchQuery, cancellationToken).ConfigureAwait(false); // Group series by name and year (if 2 series with the exact same name, the same year ...) var groupedResult = searchResults.GroupBy( p => new { p.Name, p.ProductionYear }, p => p, (key, g) => new { Key = key, Result = g.ToList() }).ToList(); if (groupedResult.Count == 1) { finalResult = groupedResult.First().Result.First(); } else if (groupedResult.Count > 1) { var filtredResult = groupedResult .Select(i => new { Ref = i, Score = NameUtils.GetMatchScore(seriesName, seriesYear, i.Key.Name, i.Key.ProductionYear) }) .Where(i => i.Score > 0) .OrderByDescending(i => i.Score) .Select(i => i.Ref) .FirstOrDefault(); finalResult = filtredResult?.Result.First(); } if (finalResult != null) { // We are in the good position, we can create the item var organizationRequest = new EpisodeFileOrganizationRequest { NewSeriesName = finalResult.Name, NewSeriesProviderIds = finalResult.ProviderIds, NewSeriesYear = finalResult.ProductionYear, TargetFolder = options.DefaultSeriesLibraryPath }; return(await CreateNewSeries(organizationRequest, finalResult, options, cancellationToken).ConfigureAwait(false)); } } return(null); }
public async Task<FileOrganizationResult> OrganizeWithCorrection(EpisodeFileOrganizationRequest request, AutoOrganizeOptions options, CancellationToken cancellationToken) { var result = _organizationService.GetResult(request.ResultId); Series series = null; if (request.NewSeriesProviderIds.Count > 0) { // We're having a new series here SeriesInfo seriesRequest = new SeriesInfo(); seriesRequest.ProviderIds = request.NewSeriesProviderIds; var refreshOptions = new MetadataRefreshOptions(_fileSystem); series = new Series(); series.Id = Guid.NewGuid(); series.Name = request.NewSeriesName; int year; if (int.TryParse(request.NewSeriesYear, out year)) { series.ProductionYear = year; } var seriesFolderName = series.Name; if (series.ProductionYear.HasValue) { seriesFolderName = string.Format("{0} ({1})", seriesFolderName, series.ProductionYear); } series.Path = Path.Combine(request.TargetFolder, seriesFolderName); series.ProviderIds = request.NewSeriesProviderIds; await series.RefreshMetadata(refreshOptions, cancellationToken); } if (series == null) { // Existing Series series = (Series)_libraryManager.GetItemById(new Guid(request.SeriesId)); } await OrganizeEpisode(result.OriginalPath, series, request.SeasonNumber, request.EpisodeNumber, request.EndingEpisodeNumber, null, options, true, request.RememberCorrection, result, cancellationToken).ConfigureAwait(false); await _organizationService.SaveResult(result, CancellationToken.None).ConfigureAwait(false); return result; }
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(SeriesInfo idInfo, CancellationToken cancellationToken) { return GetSearchResults(idInfo, "tv", cancellationToken); }
public static Task<Model.Epg> GetMetadata(Model.Epg epg, CancellationToken cancellationToken) { return Task.Run(async () => { var seriesItem = new MediaBrowser.Controller.Entities.TV.Series(); var seriesDataPath = ""; var seriesId = ""; var language = String.IsNullOrWhiteSpace(Plugin.Instance.Configuration.Provider) ? "en" : "pt"; var country = String.IsNullOrWhiteSpace(Plugin.Instance.Configuration.Provider) ? "us" : "br"; if (Plugin.Instance.Configuration.Provider == "CLARO" && (epg.IsMovie || epg.IsShopping || epg.IsSports || epg.IsNews)) return epg; try { var seriesInfo = new SeriesInfo { Name = epg.Title, ParentIndexNumber = epg.SeasonNumber, IndexNumber = epg.EpisodeNumber, MetadataLanguage = language, MetadataCountryCode = country }; if (Cache[epg.Title] == null) { MetadataResult<MediaBrowser.Controller.Entities.TV.Series> omdbSeries; var omdbItemProvider = new OmdbItemProvider(LiveTvService.JsonSerializer, LiveTvService.HttpClient, LiveTvService.Logger, LiveTvService.LibraryManager, LiveTvService.FileSystem, LiveTvService.Config); omdbSeries = await omdbItemProvider.GetMetadata(seriesInfo, cancellationToken); var imdbId = omdbSeries.Item.GetProviderId(MetadataProviders.Imdb); if (!String.IsNullOrWhiteSpace(imdbId)) { using (var result = await LiveTvService.HttpClient.Get(new HttpRequestOptions { Url = "http://www.thetvdb.com/api/GetSeriesByRemoteID.php?imdbid=" + imdbId + "&language=" + language, ResourcePool = new SemaphoreSlim(2, 2), CancellationToken = cancellationToken }).ConfigureAwait(false)) { var doc = new XmlDocument(); doc.Load(result); if (doc.HasChildNodes) { var node = doc.SelectSingleNode("//Series/seriesid"); if (node != null) { seriesId = node.InnerText; } } } seriesInfo.ProviderIds = new Dictionary<string, string> { { MetadataProviders.Tvdb.ToString(), seriesId }, { MetadataProviders.Imdb.ToString(), imdbId } }; } var tvdbSeries = await TvdbSeriesProvider.Current.GetMetadata(seriesInfo, cancellationToken); seriesItem = tvdbSeries.Item; Cache.Add(epg.Title, seriesItem, DateTimeOffset.Now.AddDays(1)); } else seriesItem = Cache[epg.Title] as MediaBrowser.Controller.Entities.TV.Series; seriesId = seriesItem.GetProviderId(MetadataProviders.Tvdb); seriesDataPath = TvdbSeriesProvider.GetSeriesDataPath( LiveTvService.Config.ApplicationPaths, new Dictionary<string, string> { { MetadataProviders.Tvdb.ToString(), seriesId } }); Logger.Debug("seriesDataPath: " + seriesDataPath); foreach (var providerId in seriesItem.ProviderIds) { Logger.Debug("providerId: " + providerId); } epg.EpisodeId = seriesItem.Id.ToString(); epg.Etag = seriesItem.ExternalEtag; epg.Name = !String.IsNullOrEmpty(seriesItem.Name) ? seriesItem.Name : epg.Title; epg.Name = !String.IsNullOrEmpty(seriesItem.OriginalTitle) ? seriesItem.OriginalTitle : epg.Name; epg.OriginalAirDate = seriesItem.PremiereDate; epg.ProductionYear = seriesItem.ProductionYear; epg.SeriesId = seriesId; epg.Overview = seriesItem.Overview; epg.Genres = seriesItem.Genres; epg.OfficialRating = seriesItem.OfficialRating; epg.CommunityRating = seriesItem.CommunityRating; epg.ProviderIds = seriesItem.ProviderIds; epg.IsSeries = true; epg.IsMovie = false; epg.IsSports = false; epg.IsNews = false; epg.IsKids = seriesItem.Genres.Any(a => a.Contains(Constant.Genre.Kids) || a.Contains(Constant.Genre.Children)); epg.IsSports = seriesItem.Genres.Any(a => a.Contains(Constant.Genre.Sports)); epg.IsRepeat = seriesItem.PremiereDate == null || seriesItem.PremiereDate < DateTime.UtcNow.AddDays(-7); } catch (Exception exception) { //Logger.Error("Series provider error: " + exception.Message); //Logger.ErrorException("Series provider error", exception); } try { if (seriesItem != null && epg != null && epg.SeasonNumber > 0 && epg.EpisodeNumber > 0) { var episodeInfo = new EpisodeInfo { ParentIndexNumber = epg.SeasonNumber, IndexNumber = epg.EpisodeNumber, SeriesProviderIds = seriesItem.ProviderIds, MetadataLanguage = seriesItem.GetPreferredMetadataLanguage(), MetadataCountryCode = seriesItem.GetPreferredMetadataCountryCode() }; var tvdbEpisode = await TvdbEpisodeProvider.Current.GetMetadata(episodeInfo, cancellationToken); var episode = tvdbEpisode.Item; if (episode != null) { epg.EpisodeId = episode.Id.ToString(); epg.Etag = episode.ExternalEtag; epg.EpisodeTitle = episode.Name; epg.OriginalAirDate = episode.PremiereDate; epg.Overview = episode.Overview; epg.ProviderIds = episode.ProviderIds; epg.IsRepeat = episode.PremiereDate == null || episode.PremiereDate < DateTime.UtcNow.AddDays(-7); } else { Logger.Debug("episode null"); } } } catch (Exception exception) { //Logger.Error("Series episode error: " + exception.Message); //Logger.ErrorException("Series episode provider error", exception); } try { if (seriesItem != null && epg != null && !String.IsNullOrWhiteSpace(seriesId)) { var imageProvider = new FanartSeriesProvider(LiveTvService.Config, LiveTvService.HttpClient, LiveTvService.FileSystem, LiveTvService.JsonSerializer); var images = await imageProvider.GetImages(seriesItem, cancellationToken); if (images.Count(c => c.Type == ImageType.Primary) == 0) { var tvdbSeriesImageProvider = new TvdbSeriesImageProvider(LiveTvService.Config, LiveTvService.HttpClient, LiveTvService.FileSystem); images = await tvdbSeriesImageProvider.GetImages(seriesItem, cancellationToken); } var image = images.OrderByDescending(o => o.VoteCount).FirstOrDefault(q => q.Type == ImageType.Primary && q.Language == language); epg.ImageUrl = image != null ? image.Url : null; } } catch (Exception exception) { //Logger.Error("Series image error: " + exception.Message); //Logger.ErrorException("Series image provider error", exception); } return epg; }, cancellationToken); }