Beispiel #1
0
        // http://facebooksdk.net/docs/making-asynchronous-requests/
        public GetPostsResult GetPosts(User user, DateTime startTime, DateTime endTime)
        {
            double unixStartTime = ConvertToUnixTimestamp(startTime);
            double unixEndTime = ConvertToUnixTimestamp(endTime);

            var result = new GetPostsResult();
            if (!hasFacebookToken(user))
            {
                result.hasErrors = true;
                result.errorMessage = badUserToken;
            }
            else {
                var userAccessToken = user.FacebookToken;
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://graph.facebook.com");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    //HTTP GET

                    StringBuilder sb = new StringBuilder("/me?fields=statuses.fields(message,from,id)&");
                    sb.Append(userAccessToken);
                    sb.Append("&since=");
                    sb.Append(unixStartTime);
                    sb.Append("&until=");
                    sb.Append(unixEndTime);
                    sb.Append("&limit=50");

                    var finalResponse = client.GetAsync(sb.ToString()).Result;

                    var resString = finalResponse.Content.ReadAsStringAsync().Result;
                    List<FacebookPostModel> posts = new List<FacebookPostModel>();
                    // Creates a dynamic object with properties of the response
                    // this is what we'll use to read the responses into FacebookPostModel objects.
                    dynamic fdata = Radabite.Backend.Helpers.JsonUtils.JsonObject.GetDynamicJsonObject(resString);
                    try
                    {
                        if (fdata != null)
                        {
                            dynamic fstatuses = fdata.statuses.data;
                            foreach (dynamic status in fstatuses)
                            {
                                // double unixTime = Convert.ToDouble(status.updated_time);
                                // DateTime aspTime = ConvertFromUnixTimestamp(unixTime);
                                DateTimeOffset time = status.updated_time;
                                if (time >= startTime && time < endTime)
                                {
                                    FacebookPostModel post = new FacebookPostModel
                                    {
                                       // providerId = status.id,
                                        message = status.message,
                                        created_time = status.updated_time
                                    };
                                    if (status.id != null)
                                    {
                                        post.providerId = status.id;
                                    }
                                    posts.Add(post);
                                }
                            }
                        }
                    }
                    catch (RuntimeBinderException e)
                    {
                        // this exception probably means part of the json response wasn't what we expected.
                        // just keep going.
                    }
                    result.posts = posts;
                }
            }
            return result;
        }
Beispiel #2
0
        public GetPostsResult GetPhotos(User user, DateTime startTime, DateTime endTime)
        {
            double unixStartTime = ConvertToUnixTimestamp(startTime);
            double unixEndTime = ConvertToUnixTimestamp(endTime);

            var result = new GetPostsResult();
            if (!hasFacebookToken(user))
            {
                result.hasErrors = true;
                result.errorMessage = badUserToken;
            }
            else
            {
                var userAccessToken = user.FacebookToken;
                using (var client = new HttpClient())
                {
                    client.BaseAddress = new Uri("https://graph.facebook.com");
                    client.DefaultRequestHeaders.Accept.Clear();
                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                    //HTTP GET

                    StringBuilder sb = new StringBuilder("/");
                    sb.Append(user.FacebookUserId);
                    sb.Append("/photos?");

                    sb.Append("since=");
                    //sb.Append(unixStartTime);
                    sb.Append(startTime.ToString());
                    sb.Append("&until=");
                    //sb.Append(unixEndTime);
                    sb.Append(endTime.ToString());
                   // sb.Append("&from=");
                   // sb.Append(user.FacebookUserId);

                    sb.Append("&");

                    sb.Append(userAccessToken);
                    var finalResponse = client.GetAsync(sb.ToString()).Result;

                    var resString = finalResponse.Content.ReadAsStringAsync().Result;
                    List<FacebookPostModel> posts = new List<FacebookPostModel>();

                    dynamic fdata = Radabite.Backend.Helpers.JsonUtils.JsonObject.GetDynamicJsonObject(resString);
                    try
                    {
                        if (fdata != null)
                        {
                            dynamic fphotos = fdata.data;
                            foreach (dynamic photo in fphotos)
                            {
                                dynamic from = photo.from;
                                FacebookPostModel post = new FacebookPostModel
                                {
                                    providerId = photo.id,
                                    photoUrl = photo.source,
                                };

                                if (from != null)
                                {
                                    post.fromId = from.id;
                                    post.fromName = from.name;
                                }
                                if (photo.name != null)
                                {
                                    post.message = photo.name;
                                }
                                if (photo.created_time != null)
                                {
                                    post.created_time = photo.updated_time;
                                }
                                if (post.fromId == Convert.ToDouble(user.FacebookUserId)) {
                                    post.photoBytes = GetPhotoBytes(post.photoUrl);
                                    posts.Add(post);
                                }
                            }
                        }
                    }
                    catch (RuntimeBinderException e)
                    {
                        // this exception probably means part of the json response wasn't what we expected.
                        // just keep going.
                    }
                    result.posts = posts;

                }
            }
            return result;
        }