Skip to content
This repository has been archived by the owner on May 16, 2023. It is now read-only.

Commit

Permalink
umbraco#532 Change to return all meetups in the next 30 days
Browse files Browse the repository at this point in the history
Also added try-catch to handle Invalid responses
  • Loading branch information
jameswilki committed Apr 11, 2020
1 parent 3bdc0a6 commit dd9f877
Showing 1 changed file with 62 additions and 53 deletions.
115 changes: 62 additions & 53 deletions OurUmbraco/Community/Meetup/MeetupService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MeetupCacheItem>();
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<MeetupCacheItem>();
var meetupCacheFile = HostingEnvironment.MapPath("~/App_Data/TEMP/MeetupStatisticsCache.json");
if (File.Exists(meetupCacheFile))
{
var jsonSerializer = new JsonSerializer();
meetupCache = jsonSerializer.Deserialize<List<MeetupCacheItem>>(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<List<MeetupCacheItem>>(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<MeetupsController>("Could not get events from meetup.com", ex);
}
}


Expand All @@ -95,6 +102,7 @@ public MeetupEventsModel GetUpcomingMeetups()
if (File.Exists(configPath) == false)
{
LogHelper.Debug<MeetupsController>("Config file was not found: " + configPath);

return meetups;
}

Expand All @@ -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<MeetupItem>();

foreach (var alias in aliases)
Expand All @@ -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)
{
Expand Down

0 comments on commit dd9f877

Please sign in to comment.