private void CachedLibraries(EmbySettings embySettings) { if (!ValidateSettings(embySettings)) { Log.Warn("The settings are not configured"); } try { var movies = GetMovies(); // Delete everything EmbyContent.Custom(connection => { connection.Open(); connection.Query("delete from EmbyContent where type = @type", new { type = 0 }); return(new List <EmbyContent>()); }); foreach (var m in movies) { if (m.Type.Equals("boxset", StringComparison.CurrentCultureIgnoreCase)) { var info = EmbyApi.GetCollection(m.Id, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri); foreach (var item in info.Items) { var movieInfo = EmbyApi.GetInformation(item.Id, EmbyMediaType.Movie, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri).MovieInformation; ProcessMovies(movieInfo); } } else { var movieInfo = EmbyApi.GetInformation(m.Id, EmbyMediaType.Movie, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri).MovieInformation; ProcessMovies(movieInfo); } } var tv = GetTvShows(); // Delete everything EmbyContent.Custom(connection => { connection.Open(); connection.Query("delete from EmbyContent where type = @type", new { type = 1 }); return(new List <EmbyContent>()); }); foreach (var t in tv) { var tvInfo = EmbyApi.GetInformation(t.Id, EmbyMediaType.Series, embySettings.ApiKey, embySettings.AdministratorId, embySettings.FullUri).SeriesInformation; if (string.IsNullOrEmpty(tvInfo.ProviderIds?.Tvdb)) { Log.Error("Provider Id on tv {0} is null", t.Name); continue; } // Check if it exists var item = EmbyContent.Custom(connection => { connection.Open(); var media = connection.QueryFirstOrDefault <EmbyContent>("select * from EmbyContent where ProviderId = @ProviderId and type = @type", new { ProviderId = tvInfo.ProviderIds.Tvdb, type = 1 }); connection.Dispose(); return(media); }); if (item != null && item.EmbyId != t.Id) { // delete this item since the Id has changed EmbyContent.Delete(item); item = null; } if (item == null) { EmbyContent.Insert(new EmbyContent { ProviderId = tvInfo.ProviderIds.Tvdb, PremierDate = tvInfo.PremiereDate, Title = tvInfo.Name, Type = Store.Models.Plex.EmbyMediaType.Series, EmbyId = t.Id, AddedAt = DateTime.UtcNow }); } } //TODO Emby //var albums = GetPlexAlbums(results); //foreach (var a in albums) //{ // if (string.IsNullOrEmpty(a.ProviderId)) // { // Log.Error("Provider Id on album {0} is null", a.Title); // continue; // } // // Check if it exists // var item = EmbyContent.Custom(connection => // { // connection.Open(); // var media = connection.QueryFirstOrDefault<PlexContent>("select * from EmbyContent where ProviderId = @ProviderId and type = @type", new { a.ProviderId, type = 2 }); // connection.Dispose(); // return media; // }); // if (item == null) // { // EmbyContent.Insert(new PlexContent // { // ProviderId = a.ProviderId, // ReleaseYear = a.ReleaseYear ?? string.Empty, // Title = a.Title, // Type = Store.Models.Plex.PlexMediaType.Artist, // Url = a.Url // }); // } //} } catch (Exception ex) { Log.Error(ex, "Failed to obtain Emby libraries"); } }
// Note, once an episode exists, we store it and it always exists. // We might want to look at checking if something has been removed from the server in the future. public void CacheEpisodes(EmbySettings settings) { var allEpisodes = EmbyApi.GetAllEpisodes(settings.ApiKey, settings.AdministratorId, settings.FullUri); var model = new List <EmbyEpisodes>(); foreach (var ep in allEpisodes.Items) { var epInfo = EmbyApi.GetInformation(ep.Id, EmbyMediaType.Episode, settings.ApiKey, settings.AdministratorId, settings.FullUri); if (epInfo.EpisodeInformation?.ProviderIds?.Tvdb == null) { continue; } // Check it this episode exists var item = Repo.Custom(connection => { connection.Open(); var media = connection.QueryFirstOrDefault <EmbyEpisodes>( "select * from EmbyEpisodes where ProviderId = @ProviderId", new { ProviderId = epInfo.EpisodeInformation?.ProviderIds?.Tvdb }); connection.Dispose(); return(media); }); if (item != null) { if (item.EmbyId != ep.Id) // The id's dont match, delete it { Repo.Delete(item); item = null; } } if (item == null) { // add it model.Add(new EmbyEpisodes { EmbyId = ep.Id, EpisodeNumber = ep.IndexNumber, SeasonNumber = ep.ParentIndexNumber, EpisodeTitle = ep.Name, ParentId = ep.SeriesId, ShowTitle = ep.SeriesName, ProviderId = epInfo.EpisodeInformation.ProviderIds.Tvdb, AddedAt = DateTime.UtcNow }); } } // Insert the new items var result = Repo.BatchInsert(model, TableName); if (!result) { Log.Error("Saving the emby episodes to the DB Failed"); } }