Ejemplo n.º 1
0
        private static string GetPagePostIds(string page, string accessToken, string untilDateTime, string after = "")
        {
            string until = DateUtility.GetUnixFromDate(untilDateTime);
            string since = DateUtility.GetUnixFromDate(DateUtility.GetDateTimeRelativeFromNow(untilDateTime, daysToGoBack - 2));
            Dictionary <string, string> param = new Dictionary <string, string>();

            param.Add("access_token", accessToken);
            param.Add("fields", "id,message,updated_time,created_time,icon,link,name,object_id,permalink_url,picture,source,shares,to,type,story,status_type,is_hidden,is_published");
            param.Add("until", until);

            param.Add("since", since);

            return($"https://graph.facebook.com/v2.10/{page}/posts?" + GetQueryParameters(param));
        }
        public static async Task <List <JObject> > GetPostsAsync(string page, string untilDateTime, string accessToken)
        {
            List <JObject> posts      = new List <JObject>();
            JObject        post       = null;
            string         until      = DateUtility.GetUnixFromDate(untilDateTime);
            string         since      = DateUtility.GetUnixFromDate(DateUtility.GetDateTimeRelativeFromNow(untilDateTime, -1));
            string         requestUri = GetRequestUrlForPage(page, accessToken, until, since);

            do
            {
                HttpClient client   = new HttpClient();
                var        response = await client.GetAsync(requestUri);

                string responseObj = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    throw new Exception();
                }


                post = JObject.Parse(responseObj);
                posts.Add(post);

                if (post?["paging"] != null && post["paging"]?["cursors"] != null && post["paging"]["cursors"]?["after"] != null)
                {
                    string after = post["paging"]["cursors"]["after"].ToString();
                    requestUri = GetRequestUrlForPage(page, accessToken, until, since, after);
                }
                else if (post?["paging"] != null && post["paging"]?["next"] != null)
                {
                    requestUri = post["paging"]?["next"].ToString();
                }
            }while (post != null && post?["paging"] != null);

            return(posts);
        }
Ejemplo n.º 3
0
        public static async Task <List <JObject> > GetPageMetricAnalytics(string page, string untilDateTime, string accessToken, string metricsGroup)
        {
            List <JObject> posts      = new List <JObject>();
            JObject        post       = null;
            string         until      = DateUtility.GetUnixFromDate(untilDateTime);
            string         since      = DateUtility.GetUnixFromDate(DateUtility.GetDateTimeRelativeFromNow(untilDateTime, daysToGoBack));
            string         requestUri = metricsGroup == FacebookPageAnalyticsMetricGroups.PagePostIds ?
                                        GetPagePostIds(page, accessToken, untilDateTime, since) :
                                        GetPageAnalyticsRequestUrl(page, accessToken, until, since, metricsGroup);

            do
            {
                using (var client = new HttpClient())
                {
                    var response = await client.GetAsync(requestUri);

                    string responseObj = await response.Content.ReadAsStringAsync();

                    if (!response.IsSuccessStatusCode)
                    {
                        Thread.Sleep(new TimeSpan(0, 0, 3));
                        response = await client.GetAsync(requestUri);

                        responseObj = await response.Content.ReadAsStringAsync();

                        if (!response.IsSuccessStatusCode)
                        {
                            throw new Exception(responseObj);
                        }
                    }

                    post = JObject.Parse(responseObj);

                    if (!posts.Contains(post) && (post?["data"] as JArray).Count > 0)
                    {
                        posts.Add(post);
                    }

                    if (post?["paging"] != null && post["paging"]?["cursors"] != null && post["paging"]["cursors"]?["after"] != null)
                    {
                        string after = post["paging"]["cursors"]["after"].ToString();
                        requestUri = GetRequestUrlForPage(page, accessToken, until, since, after);
                    }
                    else if (post?["paging"] != null && post["paging"]?["next"] != null)
                    {
                        requestUri = post["paging"]?["next"].ToString();
                    }
                }

                if (post?["data"] != null && (post?["data"] as JArray).Count > 0)
                {
                    var endTime = post?["data"]?[0]?["values"]?[0]?["end_time"];

                    if (endTime != null)
                    {
                        var et        = DateTime.Parse(endTime.ToString());
                        var untilTime = DateTime.Parse(untilDateTime);
                        if (untilTime.Subtract(et).TotalDays < 1)
                        {
                            break;
                        }
                    }
                }

                if (post?["data"] != null &&
                    (post["data"] as JArray).Count > 0 &&
                    post?["data"]?[0]?["period"] != null &&
                    post?["data"]?[0]?["period"].ToString().ToLower() == "lifetime")
                {
                    break;
                }
            }while (post != null && post?["paging"] != null && post["paging"]?["next"] != null);

            return(posts);
        }