Пример #1
0
        public static void SaveFacebookFeeds(string AccessToken, string ProfileId, string UserName)
        {
            dynamic feeds = FbUser.getFeeds(AccessToken);

            if (feeds != null)
            {
                apiHitsCount++;
                foreach (var result in feeds["data"])
                {
                    MongoFacebookFeed objFacebookFeed = new MongoFacebookFeed();
                    objFacebookFeed.Type      = "fb_feed";
                    objFacebookFeed.ProfileId = ProfileId;
                    objFacebookFeed.Id        = MongoDB.Bson.ObjectId.GenerateNewId();
                    try
                    {
                        objFacebookFeed.FromProfileUrl = "http://graph.facebook.com/" + result["from"]["id"] + "/picture?type=small";
                    }
                    catch (Exception)
                    {
                        objFacebookFeed.FromProfileUrl = "http://graph.facebook.com/" + ProfileId + "/picture?type=small";
                    }
                    try
                    {
                        objFacebookFeed.FromName = result["from"]["name"].ToString();
                    }
                    catch (Exception)
                    {
                        objFacebookFeed.FromName = UserName;
                    }
                    try
                    {
                        objFacebookFeed.FromId = result["from"]["id"].ToString();
                    }
                    catch (Exception)
                    {
                        objFacebookFeed.FromId = ProfileId;
                    }
                    objFacebookFeed.FeedId   = result["id"].ToString();
                    objFacebookFeed.FeedDate = DateTime.Parse(result["created_time"].ToString()).ToString("yyyy/MM/dd HH:mm:ss");
                    try
                    {
                        objFacebookFeed.FbComment = "http://graph.facebook.com/" + result["id"] + "/comments";
                        objFacebookFeed.FbLike    = "http://graph.facebook.com/" + result["id"] + "/likes";
                    }
                    catch (Exception)
                    {
                    }
                    try
                    {
                        objFacebookFeed.postType = result["type"].ToString();
                    }
                    catch
                    {
                        objFacebookFeed.postType = "status";
                    }
                    try
                    {
                        objFacebookFeed.postingFrom = result["application"]["name"].ToString();
                    }
                    catch
                    {
                        objFacebookFeed.postingFrom = "Facebook";
                    }
                    try
                    {
                        objFacebookFeed.Picture = result["picture"].ToString();
                    }
                    catch (Exception ex)
                    {
                        objFacebookFeed.Picture = "";
                    }
                    try
                    {
                        objFacebookFeed.Likecount = result["likes"]["summary"]["total_count"].ToString();
                    }
                    catch (Exception ex)
                    {
                    }
                    try
                    {
                        objFacebookFeed.Commentcount = result["comments"]["summary"]["total_count"].ToString();
                    }
                    catch (Exception ex)
                    {
                    }

                    string message = string.Empty;

                    try
                    {
                        if (result["message"] != null)
                        {
                            message = result["message"];
                        }
                    }
                    catch (Exception ex)
                    {
                        try
                        {
                            if (result["description"] != null)
                            {
                                message = result["description"];
                            }
                        }
                        catch (Exception exx)
                        {
                            try
                            {
                                if (result["story"] != null)
                                {
                                    message = result["story"];
                                }
                            }
                            catch (Exception exxx)
                            {
                                message = string.Empty;
                            }
                        }
                    }

                    if (message == null)
                    {
                        message = "";
                    }
                    objFacebookFeed.FeedDescription  = message;
                    objFacebookFeed.EntryDate        = DateTime.UtcNow.ToString("yyyy/MM/dd HH:mm:ss");
                    objFacebookFeed._facebookComment = FbPostComments(objFacebookFeed.FeedId, AccessToken);
                    try
                    {
                        MongoRepository mongorepo = new MongoRepository("MongoFacebookFeed");
                        var             ret       = mongorepo.Find <MongoFacebookFeed>(t => t.FeedId == objFacebookFeed.FeedId);
                        var             task      = Task.Run(async() =>
                        {
                            return(await ret);
                        });
                        int count = task.Result.Count;
                        if (count < 1)
                        {
                            mongorepo.Add <MongoFacebookFeed>(objFacebookFeed);
                        }
                        else
                        {
                            try
                            {
                                FilterDefinition <BsonDocument> filter = new BsonDocument("FeedId", objFacebookFeed.FeedId);
                                var update = Builders <BsonDocument> .Update.Set("postType", objFacebookFeed.postType).Set("postingFrom", objFacebookFeed.postingFrom).Set("Likecount", objFacebookFeed.Likecount).Set("Commentcount", objFacebookFeed.Commentcount).Set("_facebookComment", objFacebookFeed._facebookComment);

                                mongorepo.Update <MongoFacebookFeed>(update, filter);
                            }
                            catch { }
                        }
                    }
                    catch (Exception ex)
                    {
                        apiHitsCount = MaxapiHitsCount;
                    }
                    if (apiHitsCount < MaxapiHitsCount)
                    {
                        // AddFbPostComments(objFacebookFeed.FeedId, AccessToken);
                    }
                }
            }
            else
            {
                apiHitsCount = MaxapiHitsCount;
            }
        }
Пример #2
0
        public static void SaveFacebookPageFeed(string accesstoken, string facebookid, string PageName)
        {
            try
            {
                dynamic fbfeeds = FbUser.getFeeds(accesstoken, facebookid);
                if (fbfeeds != null)
                {
                    apiHitsCount++;
                    foreach (var _feed in fbfeeds["data"])
                    {
                        try
                        {
                            Domain.Socioboard.Models.Mongo.FacebookPagePost _FacebookPagePost = new Domain.Socioboard.Models.Mongo.FacebookPagePost();
                            _FacebookPagePost.Id     = ObjectId.GenerateNewId();
                            _FacebookPagePost.strId  = ObjectId.GenerateNewId().ToString();
                            _FacebookPagePost.PageId = facebookid;
                            try
                            {
                                _FacebookPagePost.PageName = _feed["from"]["name"].ToString();
                            }
                            catch {
                                _FacebookPagePost.PageName = PageName;
                            }
                            try
                            {
                                _FacebookPagePost.PostId = _feed["id"].ToString();
                            }
                            catch { }
                            try
                            {
                                _FacebookPagePost.Message = _feed["message"].ToString();
                            }
                            catch
                            {
                                _FacebookPagePost.Message = "";
                            }
                            try
                            {
                                _FacebookPagePost.Link = _feed["link"].ToString();
                            }
                            catch
                            {
                                _FacebookPagePost.Link = "";
                            }
                            try
                            {
                                _FacebookPagePost.Name = _feed["name"].ToString();
                            }
                            catch
                            {
                                _FacebookPagePost.Name = "";
                            }
                            try
                            {
                                _FacebookPagePost.Picture = _feed["picture"].ToString();
                            }
                            catch
                            {
                                _FacebookPagePost.Picture = "";
                            }
                            try
                            {
                                _FacebookPagePost.Description = _feed["description"].ToString();
                            }
                            catch
                            {
                                _FacebookPagePost.Description = "";
                            }
                            try
                            {
                                _FacebookPagePost.Type = _feed["type"].ToString();
                            }
                            catch
                            {
                                _FacebookPagePost.Type = "";
                            }
                            try
                            {
                                _FacebookPagePost.CreatedTime = ToUnixTimestamp(Convert.ToDateTime(_feed["created_time"].ToString()));
                            }
                            catch
                            {
                                _FacebookPagePost.CreatedTime = ToUnixTimestamp(DateTime.UtcNow);
                            }

                            try
                            {
                                dynamic feeddetails = FbUser.getFeedDetail(accesstoken, _FacebookPagePost.PostId);

                                try
                                {
                                    _FacebookPagePost.Likes = feeddetails["likes"]["summary"]["total_count"].ToString();
                                }
                                catch
                                {
                                    _FacebookPagePost.Likes = "0";
                                }
                                try
                                {
                                    _FacebookPagePost.Comments = feeddetails["comments"]["summary"]["total_count"].ToString();
                                }
                                catch
                                {
                                    _FacebookPagePost.Comments = "0";
                                }
                                try
                                {
                                    _FacebookPagePost.Shares = feeddetails["shares"]["count"].ToString();
                                }
                                catch
                                {
                                    _FacebookPagePost.Shares = "0";
                                }
                            }
                            catch (Exception ex)
                            {
                            }

                            try
                            {
                                dynamic postdetails = FbUser.postdetails(accesstoken, _FacebookPagePost.PostId);
                                string  _clicks     = string.Empty;
                                foreach (var _details in postdetails["data"])
                                {
                                    if (_details["name"].ToString() == "post_story_adds_unique")
                                    {
                                        try
                                        {
                                            _FacebookPagePost.Talking = _details["values"][0]["value"].ToString();
                                        }
                                        catch (Exception ex)
                                        {
                                            _FacebookPagePost.Talking = "0";
                                        }
                                    }
                                    else if (_details["name"].ToString() == "post_impressions_unique")
                                    {
                                        try
                                        {
                                            _FacebookPagePost.Reach = _details["values"][0]["value"].ToString();
                                        }
                                        catch (Exception ex)
                                        {
                                            _FacebookPagePost.Reach = "0";
                                        }
                                    }
                                    else if (_details["name"].ToString() == "post_consumptions_by_type_unique")
                                    {
                                        try
                                        {
                                            _clicks = _details["values"][0]["value"]["other clicks"].ToString();
                                        }
                                        catch (Exception ex)
                                        {
                                            _clicks = "0";
                                        }
                                    }
                                }
                                try
                                {
                                    _FacebookPagePost.EngagedUsers = (Int32.Parse(_clicks) + Int32.Parse(_FacebookPagePost.Talking)).ToString();
                                }
                                catch (Exception ex)
                                {
                                    _FacebookPagePost.EngagedUsers = "0";
                                }

                                try
                                {
                                    _FacebookPagePost.Engagement = (double.Parse(_FacebookPagePost.EngagedUsers.ToString()) * 100) / double.Parse(_FacebookPagePost.Reach.ToString());
                                }
                                catch (Exception)
                                {
                                    _FacebookPagePost.Engagement = 0.00;
                                }
                            }
                            catch { }
                            //code to save facebookpage post data
                            MongoRepository reppoFacebookPagePost = new MongoRepository("FacebookPagePost");
                            var             ret  = reppoFacebookPagePost.Find <Domain.Socioboard.Models.Mongo.FacebookPagePost>(t => t.PostId == _FacebookPagePost.PostId);
                            var             task = Task.Run(async() =>
                            {
                                return(await ret);
                            });
                            int count = task.Result.Count;
                            if (count > 0)
                            {
                                var update = Builders <Domain.Socioboard.Models.Mongo.FacebookPagePost> .Update.Set(t => t.Likes, _FacebookPagePost.Likes).Set(t => t.Comments, _FacebookPagePost.Comments).Set(t => t.Shares, _FacebookPagePost.Shares)
                                             .Set(t => t.EngagedUsers, _FacebookPagePost.EngagedUsers).Set(t => t.Talking, _FacebookPagePost.Talking).Set(t => t.Engagement, _FacebookPagePost.Engagement)
                                             .Set(t => t.Reach, _FacebookPagePost.Reach).Set(t => t.Link, _FacebookPagePost.Link);

                                reppoFacebookPagePost.Update <Domain.Socioboard.Models.Mongo.FacebookPagePost>(update, t => t.PostId == _FacebookPagePost.PostId);
                            }
                            else
                            {
                                reppoFacebookPagePost.Add(_FacebookPagePost);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    }
                }
                else
                {
                    apiHitsCount = MaxapiHitsCount;
                }
            }
            catch (Exception ex)
            {
                apiHitsCount = MaxapiHitsCount;
            }
        }