void IEpgDataSink.EndChannelPrograms(string id, string name) { if (_currentChannels == null) { return; } if (_channelPrograms.Count == 0) { return; } // Sort programs _channelPrograms.SortIfNeeded(); _channelPrograms.AlreadySorted = true; // Fix end times _channelPrograms.FixEndTimes(); ClipProgramsToWindow(); // Remove overlapping programs _channelPrograms.RemoveOverlappingPrograms(); if (!_deleteExisting) { // Remove programs overlapping ones in DB: // First retrieve all programs for current channels SqlBuilder sb = new SqlBuilder(StatementType.Select, typeof(Program)); sb.AddConstraint(Operator.In, "idChannel", _currentChannels, "IdChannel"); //sb.AddConstraint(Operator.Equals, "idChannel", _currentChannels.IdChannel); //sb.AddOrderByField(false, "starttime"); SqlStatement stmt = sb.GetStatement(true); ProgramList dbPrograms = new ProgramList(ObjectFactory.GetCollection <Program>(stmt.Execute())); _channelPrograms.RemoveOverlappingPrograms(dbPrograms); } foreach (Channel chan in _currentChannels) { layer.RemoveOldPrograms(chan.IdChannel); } DeleteBeforeImportOption programsToDelete = _deleteExisting ? DeleteBeforeImportOption.OverlappingPrograms : DeleteBeforeImportOption.None; layer.InsertPrograms(_channelPrograms, programsToDelete, ThreadPriority.BelowNormal); //_channelPrograms.Clear(); _channelPrograms = null; _currentChannels = null; }
private void ImportPrograms(Channel dbChannel, IList <EpgProgram> epgPrograms) { int iInserted = 0; bool hasGaps = false; _layer.RemoveOldPrograms(dbChannel.IdChannel); EpgHoleCollection holes = new EpgHoleCollection(); if ((dbChannel.EpgHasGaps || _alwaysFillHoles) && !_alwaysReplace) { Log.Epg("{0}: {1} is marked to have epg gaps. Calculating them...", _grabberName, dbChannel.DisplayName); IList <Program> infos = _layer.GetPrograms(dbChannel, DateTime.Now); if (infos.Count > 1) { for (int i = 1; i < infos.Count; i++) { Program prev = infos[i - 1]; Program current = infos[i]; TimeSpan diff = current.StartTime - prev.EndTime; if (diff.TotalMinutes > 5) { holes.Add(new EpgHole(prev.EndTime, current.StartTime)); } } } Log.Epg("{0}: {1} Found {2} epg holes.", _grabberName, dbChannel.DisplayName, holes.Count); } DateTime dbLastProgram = _layer.GetNewestProgramForChannel(dbChannel.IdChannel); EpgProgram lastProgram = null; for (int i = 0; i < epgPrograms.Count; i++) { EpgProgram epgProgram = epgPrograms[i]; // Check for dupes if (lastProgram != null) { if (epgProgram.StartTime == lastProgram.StartTime && epgProgram.EndTime == lastProgram.EndTime) { continue; } TimeSpan diff = epgProgram.StartTime - lastProgram.EndTime; if (diff.Minutes > 5) { hasGaps = true; } } if (epgProgram.StartTime <= dbLastProgram && !_alwaysReplace) { if (epgProgram.StartTime < DateTime.Now) { continue; } if (!holes.FitsInAnyHole(epgProgram.StartTime, epgProgram.EndTime)) { continue; } Log.Epg("{0}: Great we stuffed an epg hole {1}-{2} :-)", _grabberName, epgProgram.StartTime.ToShortDateString() + " " + epgProgram.StartTime.ToShortTimeString(), epgProgram.EndTime.ToShortDateString() + " " + epgProgram.EndTime.ToShortTimeString()); } Program prog = null; if (_alwaysReplace) { try { IList <Program> epgs = _layer.GetProgramExists(dbChannel, epgProgram.StartTime, epgProgram.EndTime); if (epgs.Count > 0) { prog = epgs[0]; if (epgs.Count > 1) { Log.Epg("- {0} entries are obsolete for {1} from {2} to {3}", epgs.Count - 1, dbChannel.DisplayName, epgProgram.StartTime, epgProgram.EndTime); } for (int idx = 1; idx < epgs.Count; idx++) { try { epgs[idx].Delete(); Log.Epg("- Deleted the epg entry {0} ({1} - {2})", epgs[idx].Title, epgs[idx].StartTime, epgs[idx].EndTime); } catch (Exception ex) { Log.Epg("Error during epg entry deletion: {0}", ex.Message); } } } } catch (Exception ex) { Log.Epg("Error the existing epg entry check: {0}", ex.Message); } } AddProgramAndApplyTemplates(dbChannel, epgProgram, prog); iInserted++; lastProgram = epgProgram; } dbChannel.LastGrabTime = DateTime.Now; dbChannel.EpgHasGaps = hasGaps; dbChannel.Persist(); //_layer.StartResetProgramStatesThread(System.Threading.ThreadPriority.Lowest); Log.Epg("- Inserted {0} epg entries for channel {1}", iInserted, dbChannel.DisplayName); }