public DateInterval Convert(PublicationPeriod publicationPeriod) { IWeekYearRule rule = WeekYearRules.Iso; var today = LocalDate.FromDateTime(DateTime.Today); var weekNumber = rule.GetWeekOfWeekYear(today); switch (publicationPeriod) { case PublicationPeriod.ThisWeek: LocalDate startOfThisWeek = LocalDate.FromWeekYearWeekAndDay(today.Year, weekNumber, IsoDayOfWeek.Monday); return(new DateInterval(startOfThisWeek, LocalDate.FromDateTime(DateTime.Today))); case PublicationPeriod.LastWeek: LocalDate startOfLastWeek = LocalDate.FromWeekYearWeekAndDay(today.Year, rule.GetWeekOfWeekYear(today.PlusWeeks(-1)), IsoDayOfWeek.Monday); return(new DateInterval(startOfLastWeek, startOfLastWeek.PlusWeeks(1).PlusDays(-1))); case PublicationPeriod.ThisMonth: var startOfThisMonth = LocalDate.FromDateTime(new DateTime(today.Year, today.Month, 1)); return(new DateInterval(startOfThisMonth, today)); case PublicationPeriod.LastMonth: var startOfLastMonth = LocalDate.FromDateTime(new DateTime(today.Year, today.Month, 1)).PlusMonths(-1); var endOfLastMonth = new LocalDate(startOfLastMonth.Year, startOfLastMonth.Month, startOfLastMonth.Calendar.GetDaysInMonth(startOfLastMonth.Year, startOfLastMonth.Month)); return(new DateInterval(startOfLastMonth, endOfLastMonth)); case PublicationPeriod.ThisYear: var startOfThisYear = LocalDate.FromDateTime(new DateTime(today.Year, 1, 1)); return(new DateInterval(startOfThisYear, today)); case PublicationPeriod.LastYear: var startOfLastYear = LocalDate.FromDateTime(new DateTime(today.Year, 1, 1)).PlusYears(-1); var endOfLastYear = LocalDate.FromDateTime(new DateTime(today.Year, 12, 31)).PlusYears(-1); return(new DateInterval(startOfLastYear, endOfLastYear)); default: throw new ArgumentOutOfRangeException(nameof(publicationPeriod), publicationPeriod, null); } }
public async Task <IEnumerable <ContentItem> > LoadContentItemsAsync(string contentFilePath, PublicationPeriod publicationPeriod, DateTime fromDate, DateTime toDate, int itemCount) { var content = JsonConvert.DeserializeObject <IEnumerable <ContentItem> >(await File.ReadAllTextAsync(contentFilePath).ConfigureAwait(false)); if (publicationPeriod != PublicationPeriod.None) { var dateRange = new PublicationPeriodConverter().Convert(publicationPeriod); content = content.Where(p => (LocalDate.FromDateTime(p.PublishedOn.LocalDateTime) >= dateRange.Start) && (LocalDate.FromDateTime(p.PublishedOn.LocalDateTime) <= dateRange.End)); } else { // if fromDate is specified, but toDate isn't, set toDate to now. If toDate is specified, use that. if (fromDate != DateTime.MinValue) { if (toDate == DateTime.MinValue) { toDate = DateTime.Now; } else { if (toDate.ToString("HH:mm:ss") == "00:00:00") { toDate = new DateTime(toDate.Year, toDate.Month, toDate.Day, 23, 59, 59); } } content = content.Where(p => p.PublishedOn.LocalDateTime >= fromDate && p.PublishedOn.LocalDateTime <= toDate); } else { // if fromDate isn't specified, but toDate is if (toDate != DateTime.MinValue) { content = content.Where(p => p.PublishedOn.LocalDateTime >= fromDate && p.PublishedOn.LocalDateTime <= toDate); } } } // Sort so that content with the shortest lifespan are first. content = content.OrderBy(p => p.PromoteUntil).ToList(); var contentItems = content.ToList(); if (itemCount == 0) { itemCount = contentItems.Count; } Console.WriteLine($"Total Posts: {contentItems.Count}"); Console.WriteLine($"Promoting first: {itemCount}"); return(contentItems.Take(itemCount)); }
public async Task BufferContentItemsAsync <TContentFormatter>(string contentFilePath, string profilePrefix, string profileName, PublicationPeriod publicationPeriod, DateTime fromDate, DateTime toDate, int itemCount) where TContentFormatter : class, IContentFormatter, new() { TContentFormatter formatter = new TContentFormatter(); string profileKey = profilePrefix + profileName; var settings = this.settingsManager.LoadSettings(nameof(StackerSettings)); if (settings.BufferProfiles.ContainsKey(profileKey)) { var profileId = settings.BufferProfiles[profileKey]; Console.WriteLine($"Buffer Profile: {profileKey} = {profileId}"); Console.WriteLine($"Loading: {contentFilePath}"); var contentItems = await this.LoadContentItemsAsync(contentFilePath, publicationPeriod, fromDate, toDate, itemCount).ConfigureAwait(false); var formattedContentItems = formatter.Format("social", profileName, contentItems); await this.bufferClient.UploadAsync(formattedContentItems, profileId).ConfigureAwait(false); } else { Console.WriteLine($"Settings for {profileKey} not found. Please check your Stacker configuration."); } }