Advertisement
Guest User

Facebook Event Serialization

a guest
Mar 22nd, 2012
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using mCMS.Controllers.Helpers;
  6. using Newtonsoft.Json;
  7.  
  8. namespace Models.JSON
  9. {
  10.     public static class FacebookEvent
  11.     {
  12.         private class AttendingCollection
  13.         {
  14.             public List<Attending> data { get; set; }
  15.         }
  16.  
  17.         private class Attending
  18.         {
  19.             public String name { get; set; }
  20.             public String id { get; set; }
  21.             public String attending { get; set; }
  22.         }
  23.  
  24.         private class LightEventCollection
  25.         {
  26.             public List<LightEvent> data { get; set; }
  27.         }
  28.  
  29.         private class LightEvent
  30.         {
  31.             public String name { get; set; }
  32.             public String start_time { get; set; }
  33.             public String end_time { get; set; }
  34.             public String location { get; set; }
  35.             public String id { get; set; }
  36.         }
  37.  
  38.         private class Event
  39.         {
  40.             public String id { get; set; }
  41.             public String name { get; set; }
  42.             public String description { get; set; }
  43.             public String start_time { get; set; }
  44.             public String end_time { get; set; }
  45.             public String location { get; set; }
  46.             public String ImageURL { get; set; }
  47.  
  48.             public Venue venue { get; set; }
  49.             public int Attendees { get; set; }
  50.         }
  51.  
  52.         private class Venue
  53.         {
  54.             public String latitude { get; set; }
  55.             public String longitude { get; set; }
  56.             public String id { get; set; }
  57.         }
  58.  
  59.         private class EventViewModel
  60.         {
  61.             public String ID { get; set; }
  62.             public String Description { get; set; }
  63.             public String StartTime { get; set; }
  64.             public String EndTime { get; set; }
  65.             public String ImageURL { get; set; }
  66.             public String Name { get; set; }
  67.             public Venue Venue { get; set; }
  68.             public int Attendees { get; set; }
  69.         }
  70.  
  71.         private class EventViewModelCollection
  72.         {
  73.             public List<EventViewModel> events { get; set; }
  74.         }
  75.  
  76.         public static String GetEventViewModels(String stream, String authToken)
  77.         {
  78.             var lightWeightEvents = JsonConvert.DeserializeObject<LightEventCollection>(stream);
  79.             var events = GetEventsGraph(lightWeightEvents.data.ToList(), authToken);
  80.             var viewModels = events.Select(item => new EventViewModel()
  81.                                                        {
  82.                                                            ID = item.id,
  83.                                                            Description = item.description,
  84.                                                            StartTime = item.start_time,
  85.                                                            EndTime = item.end_time,
  86.                                                            Name = item.name,
  87.                                                            ImageURL = item.ImageURL,
  88.                                                            Venue = item.venue,
  89.                                                            Attendees = item.Attendees
  90.                                                        }).ToList();
  91.              viewModels.Sort((x, y) => DateTime.Parse(x.StartTime).CompareTo(DateTime.Parse(y.StartTime)));
  92.             var col = new EventViewModelCollection()
  93.                        {
  94.                            events = viewModels
  95.                        };
  96.  
  97.             return JsonConvert.SerializeObject(col);
  98.         }
  99.  
  100.         private static IEnumerable<Event> GetEventsGraph(IEnumerable<LightEvent> lightEvents, String authToken)
  101.         {
  102.             var events = new List<Event>();
  103.             foreach (var lightEvent in lightEvents)
  104.             {
  105.                 var stream = WebStream.GetWebStream("https://graph.facebook.com/" + lightEvent.id + "&" + authToken);
  106.                 var detailedEvent = JsonConvert.DeserializeObject<Event>(stream);
  107.                 detailedEvent.ImageURL = WebStream.GetFacebookImage("https://graph.facebook.com/" + lightEvent.id + "/picture&" + authToken);
  108.  
  109.                 //Get the amount of people signed up for the event
  110.                 var attendee = WebStream.GetWebStream("https://graph.facebook.com/" + lightEvent.id + "/attending?limit=999999&" + authToken);
  111.                 var attendees = JsonConvert.DeserializeObject<AttendingCollection>(attendee);
  112.                 detailedEvent.Attendees = attendees.data.Select(m => m.attending == "attending").Count();
  113.                
  114.                 events.Add(detailedEvent);
  115.             }
  116.             return events;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement