Пример #1
0
        /// <summary>
        /// Request new collection of tweets and update the report asynchronously
        /// </summary>
        public static async Task <bool> UpdateReportAsync(TwitterReport report)
        {
            IUser user = null;

            try
            {
                user = User.GetUserFromScreenName(report.UserName);
            }
            catch (TwitterNullCredentialsException)
            {
                //Write ex to log
                return(false);
            }
            if (user == null)
            {
                return(false);
            }

            var receivedTweets = await Task.Run(() => GetTweetsForUser(user));

            if (receivedTweets == null)
            {
                return(false);
            }

            if (Settings.CurrentUser != null)
            {
                report.Update(Settings.CurrentUser.Id, receivedTweets);
            }
            else
            {
                report.Update(receivedTweets);
            }
            return(true);
        }
Пример #2
0
        public async Task ScheduleReportEmail(ReportAddRequest model)
        {
            // Report Template
            string Template = File.ReadAllText(Path.Combine(HostingEnvironment.MapPath("~/Content/EmailTemplates"), "FacebookReport.html"));

            Template = Template.Replace("{{ReportLink}}", _configService.getConfigValusAsString("ReportLink"));

            if (model.Url == "twitter")
            {
                var jsonReport = _twitterService.GetTwitterReport(model.Name);

                JavaScriptSerializer js    = new JavaScriptSerializer();
                TwitterReport        sData = js.Deserialize <TwitterReport>(jsonReport);

                string overallGrade = sData.OverallGrade;
                string engGrade     = sData.EngGrade;
                string actGrade     = sData.ActGrade;
                string growGrade    = sData.GrowGrade;
                string reachGrade   = sData.ReachGrade;
                string dataGrade    = sData.DataGrade;

                Template = Template.Replace("{{FirstName}}", model.FirstName);
                Template = Template.Replace("{{OverallScore}}", overallGrade.ToString());
                Template = Template.Replace("{{EngagementScore}}", engGrade.ToString());
                Template = Template.Replace("{{ActivityScore}}", actGrade.ToString());
                Template = Template.Replace("{{GrowthScore}}", growGrade.ToString());
                Template = Template.Replace("{{ReachScore}}", reachGrade.ToString());
                Template = Template.Replace("{{DataScore}}", dataGrade.ToString());
            }
            else if (model.Url == "facebook")
            {
                ///////////////////
                //Facebook Report//
                ///////////////////
                string token = _configService.getConfigValusAsString("FbAppAuthToken"); // "340834969679482|VZHv9QJUimMQxf0Z3GDYN6EGFX4";
                string site  = model.Name;

                //Fb Profile
                string profile_item         = _facebookService.GetFbProfile(token, site);
                var    json                 = new JavaScriptSerializer();
                FbProfileAddRequest profile = json.Deserialize <FbProfileAddRequest>(profile_item);
                string userName             = profile.Name;
                int    totalLikes           = profile.Engagement.Count;
                int    talkCount            = profile.Talking_About_Count;
                bool   verified             = profile.Is_Verified;

                //Fb Feed
                int feedPerCall     = _configService.getConfigValueAsInt("FbAuthFeedFeedPerCall");     //50;
                int daysInTimeFrame = _configService.getConfigValueAsInt("FbAuthFeedDaysInTimeFrame"); // 7;

                string   feed_item = _facebookService.GetFbFeed(token, site, feedPerCall);
                FeedData fbFeed    = new JavaScriptSerializer().Deserialize <FeedData>(feed_item);

                List <FeedItem> al = new List <FeedItem>();

                //Time frame for Feed to get set to One week
                var timeFrame = DateTime.Today.Date.AddDays(-daysInTimeFrame);

                bool   moreFeed = false;
                string next;
                //Gets First batch of feed and adds to array all that is within timeframe
                if (fbFeed.Feed.Data[feedPerCall - 1].Created_Time > timeFrame)
                {
                    for (var i = 0; i < feedPerCall; i++)
                    {
                        al.Add(fbFeed.Feed.Data[i]);
                    }
                    moreFeed = true;
                }
                else
                {
                    bool continueLoop = true;
                    var  i            = 0;
                    while (continueLoop)
                    {
                        if (fbFeed.Feed.Data[i].Created_Time < timeFrame)
                        {
                            continueLoop = false;
                            continue;
                        }
                        al.Add(fbFeed.Feed.Data[i]);
                        i++;
                    }
                }
                //Loops and adds more Feed until time frame is past
                if (moreFeed)
                {
                    next = fbFeed.Feed.Paging.Next;
                    bool stopper = true;

                    while (stopper)
                    {
                        string nextItem   = _facebookService.GetFbFeedNext(next);
                        Feed   fbFeedNext = new JavaScriptSerializer().Deserialize <Feed>(nextItem);

                        if (fbFeedNext.Data[feedPerCall - 1].Created_Time > timeFrame)
                        {
                            for (var i = 0; i < feedPerCall; i++)
                            {
                                al.Add(fbFeedNext.Data[i]);
                            }
                            next = fbFeedNext.Paging.Next;
                        }
                        else
                        {
                            bool continueLoop = true;
                            var  i            = 0;
                            while (continueLoop)
                            {
                                if (fbFeedNext.Data[i].Created_Time < timeFrame)
                                {
                                    continueLoop = false;
                                }
                                al.Add(fbFeedNext.Data[i]);
                                i++;
                            }
                            stopper = false;
                        }
                    }
                }

                List <FbPostData> stats = new List <FbPostData>();
                //Loops thru all of the feed and gets likes, comments, shares, and reactions for each
                for (var i = 0; i < al.Count; i++)
                {
                    string     iPost    = al[i].Id;
                    string     postData = _facebookService.GetPostData(token, iPost);
                    FbPostData post     = new JavaScriptSerializer().Deserialize <FbPostData>(postData);
                    post.Date = al[i].Created_Time;
                    stats.Add(post);
                }

                List <FbWkPostData> lpd = new List <FbWkPostData>();
                FbWkPostData        pd  = new FbWkPostData();
                pd.Shares     = 0;
                pd.Likes      = 0;
                pd.Reactions  = 0;
                pd.Comments   = 0;
                pd.Date       = stats[0].Date.Date;
                pd.DateString = stats[0].Date.ToString(@"MM/dd/yy");
                pd.Posts      = 0;

                int sharesCount    = 0;
                int likesCount     = 0;
                int commentsCount  = 0;
                int reactionsCount = 0;

                for (var i = 0; i < al.Count; i++)
                {
                    if (stats[i].Date.Date == pd.Date.Date)
                    {
                        pd.Posts++;
                        pd.Likes     += stats[i].Likes.Summary.Total_Count;
                        pd.Reactions += stats[i].Reactions.Summary.Total_Count;
                        pd.Comments  += stats[i].Comments.Summary.Total_Count;

                        if (stats[i].Shares == null)
                        {
                            if (i == al.Count - 1)
                            {
                                lpd.Add(pd);
                            }
                            continue;
                        }
                        pd.Shares += stats[i].Shares.Count;
                        if (i == al.Count - 1)
                        {
                            lpd.Add(pd);
                        }
                    }
                    else
                    {
                        while (stats[i].Date.Date != pd.Date.Date.AddDays(-1))
                        {
                            lpd.Add(pd);
                            DateTime prev = pd.Date.Date.AddDays(-1);
                            pd            = new FbWkPostData();
                            pd.Posts      = 0;
                            pd.Date       = prev;
                            pd.DateString = pd.Date.ToString(@"MM/dd/yy");
                            pd.Likes      = 0;
                            pd.Reactions  = 0;
                            pd.Comments   = 0;
                            pd.Shares     = 0;
                        }
                        lpd.Add(pd);
                        pd            = new FbWkPostData();
                        pd.Posts      = 1;
                        pd.Date       = stats[i].Date.Date;
                        pd.DateString = stats[i].Date.ToString(@"MM/dd/yy");
                        pd.Likes      = stats[i].Likes.Summary.Total_Count;
                        pd.Reactions  = stats[i].Reactions.Summary.Total_Count;
                        pd.Comments   = stats[i].Comments.Summary.Total_Count;

                        if (stats[i].Shares == null)
                        {
                            pd.Shares = 0;
                            continue;
                        }

                        pd.Shares = stats[i].Shares.Count;
                    }

                    likesCount     += stats[i].Likes.Summary.Total_Count;
                    reactionsCount += stats[i].Reactions.Summary.Total_Count;
                    commentsCount  += stats[i].Comments.Summary.Total_Count;

                    if (stats[i].Shares == null)
                    {
                        continue;
                    }
                    sharesCount += stats[i].Shares.Count;
                }

                double engScore = ((double)(sharesCount + likesCount + commentsCount + reactionsCount) / (double)totalLikes * 100);
                int    engGrade;
                if (engScore > 3)
                {
                    engGrade = 25;
                }
                else if (engScore >= 2.5)
                {
                    engGrade = 22;
                }
                else if (engScore >= 1.5)
                {
                    engGrade = 15;
                }
                else if (engScore >= 0.5)
                {
                    engGrade = 8;
                }
                else if (engScore > 0)
                {
                    engGrade = 3;
                }
                else
                {
                    engGrade = 0;
                }

                //Fb Activity
                int feedPerCallA     = _configService.getConfigValueAsInt("FbAuthActivityFeedPerCall");     // 25;
                int daysInTimeFrameA = _configService.getConfigValueAsInt("FbAuthActivityDaysInTimeFrame"); //30;
                var timeFrameA       = DateTime.Today.AddDays(-daysInTimeFrameA);
                int actGrade         = 0;

                string   item    = _facebookService.GetFbFeed(token, site, feedPerCall);
                FeedData fbFeedA = new JavaScriptSerializer().Deserialize <FeedData>(item);

                if (fbFeedA.Feed.Data[24].Created_Time > timeFrameA)
                {
                    actGrade = 25;
                }
                else if (fbFeedA.Feed.Data[19].Created_Time > timeFrameA)
                {
                    actGrade = 20;
                }
                else if (fbFeedA.Feed.Data[14].Created_Time > timeFrameA)
                {
                    actGrade = 15;
                }
                else if (fbFeedA.Feed.Data[9].Created_Time > timeFrameA)
                {
                    actGrade = 10;
                }
                else if (fbFeedA.Feed.Data[4].Created_Time > timeFrameA)
                {
                    actGrade = 5;
                }
                else
                {
                    actGrade = 0;
                }

                //Fb Growth
                string     itemG                = _facebookService.GetFbFans(token, site);
                FbFansData dataReport           = new JavaScriptSerializer().Deserialize <FbFansData>(itemG);
                int        Total_Fans_Current   = dataReport.Data[0].Values[28].Value.Sum(x => x.Value);
                int        Total_Fans_Month_Ago = dataReport.Data[0].Values[0].Value.Sum(x => x.Value);
                double     percentChange        = ((double)(Total_Fans_Current - Total_Fans_Month_Ago) / (double)Total_Fans_Month_Ago * 100);
                int        growGrade            = 0;
                if (percentChange > 1)
                {
                    growGrade = 25;
                }
                else if (percentChange > 0.75)
                {
                    growGrade = 20;
                }
                else if (percentChange > 0.5)
                {
                    growGrade = 15;
                }
                else if (percentChange > 0.25)
                {
                    growGrade = 10;
                }
                else if (percentChange > 0)
                {
                    growGrade = 5;
                }
                else
                {
                    growGrade = 0;
                }

                //Fb Reach
                double reachScore = ((double)talkCount / (double)totalLikes * 100);
                int    reachGrade = 0;
                if (reachScore > 2)
                {
                    reachGrade = 10;
                }
                else if (reachScore >= 1.5)
                {
                    reachGrade = 8;
                }
                else if (reachScore >= 1)
                {
                    reachGrade = 6;
                }
                else if (reachScore >= 0.5)
                {
                    reachGrade = 4;
                }
                else if (reachScore >= 0.20)
                {
                    reachGrade = 2;
                }
                else
                {
                    reachGrade = 1;
                }

                int dataGrade = 8;

                if (verified == false)
                {
                    actGrade   = Convert.ToInt32((double)actGrade * 0.6);
                    engGrade   = Convert.ToInt32((double)engGrade * 0.6);
                    growGrade  = Convert.ToInt32((double)growGrade * 0.6);
                    reachGrade = Convert.ToInt32((double)reachGrade * 0.6);
                }
                else
                {
                    actGrade += 3;
                    if (actGrade > 25)
                    {
                        actGrade = 25;
                    }
                    ;
                    engGrade += 3;
                    if (engGrade > 25)
                    {
                        engGrade = 25;
                    }
                    ;
                    growGrade += 4;
                    if (growGrade > 25)
                    {
                        growGrade = 25;
                    }
                    ;
                    reachGrade += 2;
                    if (reachGrade > 10)
                    {
                        reachGrade = 10;
                    }
                    ;
                }

                int overallGrade = engGrade + actGrade + growGrade + reachGrade + dataGrade;

                Template = Template.Replace("{{FirstName}}", model.FirstName);
                Template = Template.Replace("{{OverallScore}}", overallGrade.ToString());
                Template = Template.Replace("{{EngagementScore}}", engGrade.ToString());
                Template = Template.Replace("{{ActivityScore}}", actGrade.ToString());
                Template = Template.Replace("{{GrowthScore}}", growGrade.ToString());
                Template = Template.Replace("{{ReachScore}}", reachGrade.ToString());
                Template = Template.Replace("{{DataScore}}", dataGrade.ToString());
            }

            var FromEmail        = _configService.getConfigValusAsString("FromEmailErrorReport");
            var FromName         = _configService.getConfigValusAsString("FromNameSendGrid");
            var ToEmail          = model.Email;
            var ToName           = _configService.getConfigValusAsString("AdminName");
            var apiKey           = _configService.getConfigValusAsString("SendGridKey");
            var client           = new SendGridClient(apiKey);
            var from             = new EmailAddress(FromEmail, FromName);
            var subject          = _configService.getConfigValusAsString("UserReportSubject");
            var plainTextContent = "";
            var to          = new EmailAddress(ToEmail, ToName); // send this to user for errors
            var htmlContent = Template + plainTextContent;       // report template
            var msg         = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
            var response    = await client.SendEmailAsync(msg);
        }