From dd9f877d6700a7c4479edaa93ba48f26f7e53d37 Mon Sep 17 00:00:00 2001 From: James Wilkinson Date: Sat, 11 Apr 2020 21:35:11 +0100 Subject: [PATCH] #532 Change to return all meetups in the next 30 days Also added try-catch to handle Invalid responses --- OurUmbraco/Community/Meetup/MeetupService.cs | 115 ++++++++++--------- 1 file changed, 62 insertions(+), 53 deletions(-) diff --git a/OurUmbraco/Community/Meetup/MeetupService.cs b/OurUmbraco/Community/Meetup/MeetupService.cs index 48286c6f9a..212a1533e5 100644 --- a/OurUmbraco/Community/Meetup/MeetupService.cs +++ b/OurUmbraco/Community/Meetup/MeetupService.cs @@ -25,60 +25,67 @@ public class MeetupService { public void UpdateMeetupStats() { - var configPath = HostingEnvironment.MapPath("~/config/MeetupUmbracoGroups.txt"); - // Get the alias (urlname) of each group from the config file - var aliases = File.ReadAllLines(configPath).Where(x => x.Trim() != "").Distinct().ToArray(); - - var counterPath = HostingEnvironment.MapPath("~/App_Data/TEMP/MeetupStatisticsCounter.txt"); - var counter = 0; - if (File.Exists(counterPath)) + try { - var savedCounter = File.ReadAllLines(counterPath).First(); - int.TryParse(savedCounter, out counter); - } + var configPath = HostingEnvironment.MapPath("~/config/MeetupUmbracoGroups.txt"); + // Get the alias (urlname) of each group from the config file + var aliases = File.ReadAllLines(configPath).Where(x => x.Trim() != "").Distinct().ToArray(); - var newCounter = aliases.Length <= counter ? 0 : counter + 1; - File.WriteAllText(counterPath, newCounter.ToString(), Encoding.UTF8); + var counterPath = HostingEnvironment.MapPath("~/App_Data/TEMP/MeetupStatisticsCounter.txt"); + var counter = 0; + if (File.Exists(counterPath)) + { + var savedCounter = File.ReadAllLines(counterPath).First(); + int.TryParse(savedCounter, out counter); + } - var client = new MeetupOAuth2Client(); - var response = client.DoHttpGetRequest(string.Format("https://api.meetup.com/{0}/events?page=1000&status=past", aliases[counter])); - var events = MeetupGetEventsResponse.ParseResponse(response).Body; + var newCounter = aliases.Length <= counter ? 0 : counter + 1; + File.WriteAllText(counterPath, newCounter.ToString(), Encoding.UTF8); - var meetupCache = new List(); - var meetupCacheFile = HostingEnvironment.MapPath("~/App_Data/TEMP/MeetupStatisticsCache.json"); - if (File.Exists(meetupCacheFile)) - { - var json = File.ReadAllText(meetupCacheFile); - using (var stringReader = new StringReader(json)) - using (var jsonTextReader = new JsonTextReader(stringReader)) + var client = new MeetupOAuth2Client(); + var response = client.DoHttpGetRequest(string.Format("https://api.meetup.com/{0}/events?page=1000&status=past", aliases[counter])); + var events = MeetupGetEventsResponse.ParseResponse(response).Body; + + var meetupCache = new List(); + var meetupCacheFile = HostingEnvironment.MapPath("~/App_Data/TEMP/MeetupStatisticsCache.json"); + if (File.Exists(meetupCacheFile)) { - var jsonSerializer = new JsonSerializer(); - meetupCache = jsonSerializer.Deserialize>(jsonTextReader); + var json = File.ReadAllText(meetupCacheFile); + using (var stringReader = new StringReader(json)) + using (var jsonTextReader = new JsonTextReader(stringReader)) + { + var jsonSerializer = new JsonSerializer(); + meetupCache = jsonSerializer.Deserialize>(jsonTextReader); + } } - } - - foreach (var meetupEvent in events) - { - if (meetupCache.Any(x => x.Id == meetupEvent.Id)) - continue; - var meetupCacheItem = new MeetupCacheItem + foreach (var meetupEvent in events) { - Time = meetupEvent.Time, - Created = meetupEvent.Created, - Description = meetupEvent.Description, - HasVenue = meetupEvent.HasVenue, - Id = meetupEvent.Id, - Link = meetupEvent.Link, - Name = meetupEvent.Name, - Updated = meetupEvent.Updated, - Visibility = meetupEvent.Visibility - }; - meetupCache.Add(meetupCacheItem); - } + if (meetupCache.Any(x => x.Id == meetupEvent.Id)) + continue; - var rawJson = JsonConvert.SerializeObject(meetupCache, Formatting.Indented); - File.WriteAllText(meetupCacheFile, rawJson, Encoding.UTF8); + var meetupCacheItem = new MeetupCacheItem + { + Time = meetupEvent.Time, + Created = meetupEvent.Created, + Description = meetupEvent.Description, + HasVenue = meetupEvent.HasVenue, + Id = meetupEvent.Id, + Link = meetupEvent.Link, + Name = meetupEvent.Name, + Updated = meetupEvent.Updated, + Visibility = meetupEvent.Visibility + }; + meetupCache.Add(meetupCacheItem); + } + + var rawJson = JsonConvert.SerializeObject(meetupCache, Formatting.Indented); + File.WriteAllText(meetupCacheFile, rawJson, Encoding.UTF8); + } + catch (Exception ex) + { + LogHelper.Error("Could not get events from meetup.com", ex); + } } @@ -95,6 +102,7 @@ public MeetupEventsModel GetUpcomingMeetups() if (File.Exists(configPath) == false) { LogHelper.Debug("Config file was not found: " + configPath); + return meetups; } @@ -106,7 +114,6 @@ public MeetupEventsModel GetUpcomingMeetups() { // Initialize a new service instance (we don't specify an API key since we're accessing public data) var service = new Skybrud.Social.Meetup.MeetupService(); - var items = new List(); foreach (var alias in aliases) @@ -119,17 +126,19 @@ public MeetupEventsModel GetUpcomingMeetups() if (meetupGroup.JObject.HasValue("next_event") == false) continue; - var nextEventId = meetupGroup.JObject.GetString("next_event.id"); - // Make the call to the Meetup.com API to get upcoming events var events = service.Events.GetEvents(alias); - // Get the next event(s) - var nextEvent = events.Body.FirstOrDefault(x => x.Id == nextEventId); + // Get the events in the next 30 days + var nextEvents = events.Body.Where(x => x.Time < DateTime.Now.AddDays(30) ); - // Append the first event of the group - if (nextEvent != null) - items.Add(new MeetupItem(meetupGroup, nextEvent)); + if (nextEvents.Any()) + { + foreach (var item in nextEvents) + { + items.Add(new MeetupItem(meetupGroup, item)); + } + } } catch (Exception ex) {