@@ -25,60 +25,67 @@ public class MeetupService
2525 {
2626 public void UpdateMeetupStats ( )
2727 {
28- var configPath = HostingEnvironment . MapPath ( "~/config/MeetupUmbracoGroups.txt" ) ;
29- // Get the alias (urlname) of each group from the config file
30- var aliases = File . ReadAllLines ( configPath ) . Where ( x => x . Trim ( ) != "" ) . Distinct ( ) . ToArray ( ) ;
31-
32- var counterPath = HostingEnvironment . MapPath ( "~/App_Data/TEMP/MeetupStatisticsCounter.txt" ) ;
33- var counter = 0 ;
34- if ( File . Exists ( counterPath ) )
28+ try
3529 {
36- var savedCounter = File . ReadAllLines ( counterPath ) . First ( ) ;
37- int . TryParse ( savedCounter , out counter ) ;
38- }
30+ var configPath = HostingEnvironment . MapPath ( "~/config/MeetupUmbracoGroups.txt" ) ;
31+ // Get the alias (urlname) of each group from the config file
32+ var aliases = File . ReadAllLines ( configPath ) . Where ( x => x . Trim ( ) != "" ) . Distinct ( ) . ToArray ( ) ;
3933
40- var newCounter = aliases . Length <= counter ? 0 : counter + 1 ;
41- File . WriteAllText ( counterPath , newCounter . ToString ( ) , Encoding . UTF8 ) ;
34+ var counterPath = HostingEnvironment . MapPath ( "~/App_Data/TEMP/MeetupStatisticsCounter.txt" ) ;
35+ var counter = 0 ;
36+ if ( File . Exists ( counterPath ) )
37+ {
38+ var savedCounter = File . ReadAllLines ( counterPath ) . First ( ) ;
39+ int . TryParse ( savedCounter , out counter ) ;
40+ }
4241
43- var client = new MeetupOAuth2Client ( ) ;
44- var response = client . DoHttpGetRequest ( string . Format ( "https://api.meetup.com/{0}/events?page=1000&status=past" , aliases [ counter ] ) ) ;
45- var events = MeetupGetEventsResponse . ParseResponse ( response ) . Body ;
42+ var newCounter = aliases . Length <= counter ? 0 : counter + 1 ;
43+ File . WriteAllText ( counterPath , newCounter . ToString ( ) , Encoding . UTF8 ) ;
4644
47- var meetupCache = new List < MeetupCacheItem > ( ) ;
48- var meetupCacheFile = HostingEnvironment . MapPath ( "~/App_Data/TEMP/MeetupStatisticsCache.json" ) ;
49- if ( File . Exists ( meetupCacheFile ) )
50- {
51- var json = File . ReadAllText ( meetupCacheFile ) ;
52- using ( var stringReader = new StringReader ( json ) )
53- using ( var jsonTextReader = new JsonTextReader ( stringReader ) )
45+ var client = new MeetupOAuth2Client ( ) ;
46+ var response = client . DoHttpGetRequest ( string . Format ( "https://api.meetup.com/{0}/events?page=1000&status=past" , aliases [ counter ] ) ) ;
47+ var events = MeetupGetEventsResponse . ParseResponse ( response ) . Body ;
48+
49+ var meetupCache = new List < MeetupCacheItem > ( ) ;
50+ var meetupCacheFile = HostingEnvironment . MapPath ( "~/App_Data/TEMP/MeetupStatisticsCache. json" ) ;
51+ if ( File . Exists ( meetupCacheFile ) )
5452 {
55- var jsonSerializer = new JsonSerializer ( ) ;
56- meetupCache = jsonSerializer . Deserialize < List < MeetupCacheItem > > ( jsonTextReader ) ;
53+ var json = File . ReadAllText ( meetupCacheFile ) ;
54+ using ( var stringReader = new StringReader ( json ) )
55+ using ( var jsonTextReader = new JsonTextReader ( stringReader ) )
56+ {
57+ var jsonSerializer = new JsonSerializer ( ) ;
58+ meetupCache = jsonSerializer . Deserialize < List < MeetupCacheItem > > ( jsonTextReader ) ;
59+ }
5760 }
58- }
59-
60- foreach ( var meetupEvent in events )
61- {
62- if ( meetupCache . Any ( x => x . Id == meetupEvent . Id ) )
63- continue ;
6461
65- var meetupCacheItem = new MeetupCacheItem
62+ foreach ( var meetupEvent in events )
6663 {
67- Time = meetupEvent . Time ,
68- Created = meetupEvent . Created ,
69- Description = meetupEvent . Description ,
70- HasVenue = meetupEvent . HasVenue ,
71- Id = meetupEvent . Id ,
72- Link = meetupEvent . Link ,
73- Name = meetupEvent . Name ,
74- Updated = meetupEvent . Updated ,
75- Visibility = meetupEvent . Visibility
76- } ;
77- meetupCache . Add ( meetupCacheItem ) ;
78- }
64+ if ( meetupCache . Any ( x => x . Id == meetupEvent . Id ) )
65+ continue ;
7966
80- var rawJson = JsonConvert . SerializeObject ( meetupCache , Formatting . Indented ) ;
81- File . WriteAllText ( meetupCacheFile , rawJson , Encoding . UTF8 ) ;
67+ var meetupCacheItem = new MeetupCacheItem
68+ {
69+ Time = meetupEvent . Time ,
70+ Created = meetupEvent . Created ,
71+ Description = meetupEvent . Description ,
72+ HasVenue = meetupEvent . HasVenue ,
73+ Id = meetupEvent . Id ,
74+ Link = meetupEvent . Link ,
75+ Name = meetupEvent . Name ,
76+ Updated = meetupEvent . Updated ,
77+ Visibility = meetupEvent . Visibility
78+ } ;
79+ meetupCache . Add ( meetupCacheItem ) ;
80+ }
81+
82+ var rawJson = JsonConvert . SerializeObject ( meetupCache , Formatting . Indented ) ;
83+ File . WriteAllText ( meetupCacheFile , rawJson , Encoding . UTF8 ) ;
84+ }
85+ catch ( Exception ex )
86+ {
87+ LogHelper . Error < MeetupsController > ( "Could not get events from meetup.com" , ex ) ;
88+ }
8289 }
8390
8491
@@ -95,6 +102,7 @@ public MeetupEventsModel GetUpcomingMeetups()
95102 if ( File . Exists ( configPath ) == false )
96103 {
97104 LogHelper . Debug < MeetupsController > ( "Config file was not found: " + configPath ) ;
105+
98106 return meetups ;
99107 }
100108
@@ -106,7 +114,6 @@ public MeetupEventsModel GetUpcomingMeetups()
106114 {
107115 // Initialize a new service instance (we don't specify an API key since we're accessing public data)
108116 var service = new Skybrud . Social . Meetup . MeetupService ( ) ;
109-
110117 var items = new List < MeetupItem > ( ) ;
111118
112119 foreach ( var alias in aliases )
@@ -119,17 +126,19 @@ public MeetupEventsModel GetUpcomingMeetups()
119126 if ( meetupGroup . JObject . HasValue ( "next_event" ) == false )
120127 continue ;
121128
122- var nextEventId = meetupGroup . JObject . GetString ( "next_event.id" ) ;
123-
124129 // Make the call to the Meetup.com API to get upcoming events
125130 var events = service . Events . GetEvents ( alias ) ;
126131
127- // Get the next event(s)
128- var nextEvent = events . Body . FirstOrDefault ( x => x . Id == nextEventId ) ;
132+ // Get the events in the next 30 days
133+ var nextEvents = events . Body . Where ( x => x . Time < DateTime . Now . AddDays ( 30 ) ) ;
129134
130- // Append the first event of the group
131- if ( nextEvent != null )
132- items . Add ( new MeetupItem ( meetupGroup , nextEvent ) ) ;
135+ if ( nextEvents . Any ( ) )
136+ {
137+ foreach ( var item in nextEvents )
138+ {
139+ items . Add ( new MeetupItem ( meetupGroup , item ) ) ;
140+ }
141+ }
133142 }
134143 catch ( Exception ex )
135144 {
0 commit comments