Meetup’s API

One of the things I want to do at this year’s TRINUG’s code camp is to have self-service kiosks the way that Mark Wilson did for the Charlotte code camp.  To that end, I took a look at Justin Saraceno’s awesome project on Github of consuming the Meetup API to do the door prize at TRINUG.

Instead of javascript, I am going to write the app in C# and side-load it onto a couple of kiosks.  To that end, I built a web request/response to the meetup API:

  1. static void GetMeetupInformation()
  2. {
  3.     String url = @"https://api.meetup.com/2/event/125656692?key=" + _apiKey;
  4.     WebRequest request = WebRequest.Create(url);
  5.     request.ContentType = @"application/json; charset=utf-8";
  6.  
  7.     WebResponse response = (HttpWebResponse)request.GetResponse();
  8.     using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  9.     {
  10.         String responseText = reader.ReadToEnd();
  11.         Console.WriteLine(responseText);
  12.     }
  13. }

 

I then pushed the response text into JSONToCSharp and got the following classes:

  1. namespace Tff.MeetUpAPI
  2. {
  3.     public class Member
  4.     {
  5.         public string name { get; set; }
  6.         public int member_id { get; set; }
  7.     }
  8.  
  9.     public class MemberPhoto
  10.     {
  11.         public string photo_link { get; set; }
  12.         public string thumb_link { get; set; }
  13.         public int photo_id { get; set; }
  14.         public string highres_link { get; set; }
  15.     }
  16.  
  17.     public class Event
  18.     {
  19.         public string id { get; set; }
  20.         public object time { get; set; }
  21.         public string event_url { get; set; }
  22.         public string name { get; set; }
  23.     }
  24.  
  25.     public class Venue
  26.     {
  27.         public int id { get; set; }
  28.         public string zip { get; set; }
  29.         public double lon { get; set; }
  30.         public bool repinned { get; set; }
  31.         public string name { get; set; }
  32.         public string state { get; set; }
  33.         public string address_1 { get; set; }
  34.         public double lat { get; set; }
  35.         public string city { get; set; }
  36.         public string country { get; set; }
  37.     }
  38.  
  39.     public class Group
  40.     {
  41.         public int id { get; set; }
  42.         public double group_lat { get; set; }
  43.         public double group_lon { get; set; }
  44.         public string join_mode { get; set; }
  45.         public string urlname { get; set; }
  46.     }
  47.  
  48.     public class Result
  49.     {
  50.         public string response { get; set; }
  51.         public Member member { get; set; }
  52.         public MemberPhoto member_photo { get; set; }
  53.         public object created { get; set; }
  54.         public Event @event { get; set; }
  55.         public object mtime { get; set; }
  56.         public int guests { get; set; }
  57.         public int rsvp_id { get; set; }
  58.         public Venue venue { get; set; }
  59.         public Group group { get; set; }
  60.         public string comments { get; set; }
  61.     }
  62.  
  63.     public class Meta
  64.     {
  65.         public string lon { get; set; }
  66.         public int count { get; set; }
  67.         public string link { get; set; }
  68.         public string next { get; set; }
  69.         public int total_count { get; set; }
  70.         public string url { get; set; }
  71.         public string id { get; set; }
  72.         public string title { get; set; }
  73.         public long updated { get; set; }
  74.         public string description { get; set; }
  75.         public string method { get; set; }
  76.         public string lat { get; set; }
  77.     }
  78.  
  79.     public class RootObject
  80.     {
  81.         public List<Result> results { get; set; }
  82.         public Meta meta { get; set; }
  83.     }
  84.  
  85.  
  86. }

 

I then created a data structure that contains only the data that I care about and build a quick translator function:

  1. private static Attendee CreateAttendeeFromMember(Result result)
  2. {
  3.     Attendee attendee = new Attendee();
  4.     attendee.MemberId = result.member.member_id;
  5.     attendee.Name = result.member.name;
  6.     if (result.member_photo != null)
  7.     {
  8.         attendee.PhotoUrl = result.member_photo.thumb_link;
  9.     }
  10.     
  11.     return attendee;
  12. }

 

Putting it together, I now have a way of parsing the event and getting a list of the attendees that I can use on the signin page:

  1. static List<Attendee> GetMeetupAttendees()
  2. {
  3.     List<Attendee> attendees = new List<Attendee>();
  4.  
  5.     String url = @"https://api.meetup.com/2/rsvps/?rsvps=yes&event_id=125656692&key=&quot; + _apiKey;
  6.     WebRequest request = WebRequest.Create(url);
  7.     request.ContentType = @"application/json; charset=utf-8";
  8.  
  9.     WebResponse response = (HttpWebResponse)request.GetResponse();
  10.     using (StreamReader reader = new StreamReader(response.GetResponseStream()))
  11.     {
  12.         String responseText = reader.ReadToEnd();
  13.         var outObject = JsonConvert.DeserializeObject<RootObject>(responseText);
  14.  
  15.         Attendee attendee = null;
  16.         foreach (Result result in outObject.results)
  17.         {
  18.             attendee = CreateAttendeeFromMember(result);
  19.             attendees.Add(attendee);
  20.         }
  21.     }
  22.  
  23.     return attendees;
  24. }

 

And in action:

  1. Console.WriteLine("Start");
  2. List<Attendee> attendees = GetMeetupAttendees();
  3.  
  4. foreach (Attendee attendee in attendees)
  5. {
  6.     Console.WriteLine(attendee.Name);
  7. }
  8. Console.WriteLine("End");
  9. Console.ReadKey();

 

gives:

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: