Exemplo n.º 1
0
        private static void DoMining()
        {
            using (var db = new TwitterDataContext(_connStr))
            {
                var devTwitters = (from d in db.DevTwitterAccounts
                                   select d).ToList();
                foreach (var devAccount in devTwitters)
                {
                    ListTweetsOnListOptions listTweetOptions = new ListTweetsOnListOptions();

                    var service = new TwitterService(CONSUMER_KEY, CONSUMER_SECRET);
                    service.AuthenticateWith(ACCESS_TOKEN, ACCESS_SECRET);
                    var currentTweets = service.ListTweetsOnUserTimeline(
                        new ListTweetsOnUserTimelineOptions()
                        {
                            Count = 20,
                            ExcludeReplies = false,
                            IncludeRts = false,
                            ScreenName = "@" + devAccount.DevTwitterName,
                            SinceId = null,
                            MaxId = null
                        });

                    foreach (var tweet in currentTweets)
                    {
                        List<TwitterStatus> tweetsToInsert = new List<TwitterStatus>();
                        int? convIdToAddTo = null;

                        //We are basically going to keep on getting the reply-to tweets.
                        var currentTweet = tweet;
                        while (true)
                        {
                            if (currentTweet == null)
                                break;

                            List<Tweet> tweetExists;

                            tweetExists = (from t in db.Tweets
                                           where t.TweetTwitterId == currentTweet.Id
                                           select t).ToList();

                            if (tweetExists.Count > 0)
                            {
                                convIdToAddTo = tweetExists[0].TwitterConversationId;
                                break;
                            }
                            else
                            {
                                tweetsToInsert.Add(currentTweet);
                            }

                            if (currentTweet.InReplyToStatusId != null)
                            {
                                currentTweet = service.GetTweet(
                                    new GetTweetOptions()
                                    {
                                        Id = currentTweet.InReplyToStatusId.Value
                                    });
                            }
                            else
                            {
                                break;
                            }
                        }

                        if (tweetsToInsert.Count > 0)
                        {
                            tweetsToInsert = tweetsToInsert.OrderBy(x => x.CreatedDate).ToList();

                            if (convIdToAddTo == null)
                            {
                                string title = String.Empty;

                                if (tweetsToInsert[0].Text.Length > 60)
                                {
                                    for (int i = 0; i < 60; i++)
                                    {
                                        title += tweetsToInsert[0].Text[i];
                                    }
                                    title += " (...)";
                                }
                                else
                                {
                                    title = tweetsToInsert[0].Text;
                                }

                                TwitterConversation conversation = new TwitterConversation();
                                conversation.TwitterConvTitle = title;
                                conversation.LastBlueResponder = tweet.Author.ScreenName;
                                conversation.LastDevResponseDate = tweet.CreatedDate;
                                conversation.TwitterConvAuthor = tweetsToInsert[0].Author.ScreenName;

                                conversation.BlizzAreaId = (from t in tweetsToInsert
                                                            from d in db.DevTwitterAccounts
                                                            where d.DevTwitterName.ToLower() == t.Author.ScreenName.ToLower()
                                                            select d.BlizzAreaId).ToList()[0];

                                foreach (var twitterTweet in tweetsToInsert)
                                {
                                    Tweet bpTweet = new Tweet();

                                    try
                                    {
                                        var devTwitter = (from d in db.DevTwitterAccounts
                                                          where d.DevTwitterName == twitterTweet.Author.ScreenName.Replace("@", "")
                                                          select d).Single();
                                        bpTweet.DevId = devTwitter.DevId;
                                    }
                                    catch (InvalidOperationException) { /*Then we simply leave DevId as null.*/ }

                                    bpTweet.TweetContent = twitterTweet.TextAsHtml;
                                    bpTweet.TweetDate = twitterTweet.CreatedDate;
                                    bpTweet.TweetTwitterId = twitterTweet.Id;
                                    bpTweet.TwitterUserName = twitterTweet.Author.ScreenName;
                                    bpTweet.TweetContentNonHtml = twitterTweet.Text;
                                    bpTweet.UserAvatarUrl = twitterTweet.Author.ProfileImageUrl;
                                    conversation.LastDevResponseDate = twitterTweet.CreatedDate;
                                    bpTweet.TwitterInResponseToId = twitterTweet.InReplyToStatusId;

                                    conversation.Tweets.Add(bpTweet);
                                }

                                db.TwitterConversations.InsertOnSubmit(conversation);
                                db.SubmitChanges();

                            }
                            else
                            {
                                foreach (var twitterTweet in tweetsToInsert)
                                {
                                    Tweet bpTweet = new Tweet();

                                    try
                                    {
                                        var devTwitter = (from d in db.DevTwitterAccounts
                                                          where d.DevTwitterName == twitterTweet.Author.ScreenName.Replace("@", "")
                                                          select d).Single();
                                        bpTweet.DevId = devTwitter.DevId;
                                    }
                                    catch (InvalidOperationException) { /*Then we simply leave DevId as null.*/ }

                                    bpTweet.TweetContent = twitterTweet.TextAsHtml;
                                    bpTweet.TweetDate = twitterTweet.CreatedDate;
                                    bpTweet.TweetTwitterId = twitterTweet.Id;
                                    bpTweet.TwitterUserName = twitterTweet.Author.ScreenName;
                                    bpTweet.TwitterConversationId = convIdToAddTo.Value;
                                    bpTweet.TweetContentNonHtml = twitterTweet.Text;
                                    bpTweet.UserAvatarUrl = twitterTweet.Author.ProfileImageUrl;
                                    bpTweet.TwitterInResponseToId = twitterTweet.InReplyToStatusId;

                                    var conversation = db.TwitterConversations.Where(x => x.Id == convIdToAddTo).Single();
                                    conversation.LastDevResponseDate = twitterTweet.CreatedDate;

                                    db.Tweets.InsertOnSubmit(bpTweet);
                                    db.SubmitChanges();
                                }
                            }
                        }

                    }

                }
            }
        }
 partial void DeleteTweet(Tweet instance);
 partial void UpdateTweet(Tweet instance);
 partial void InsertTweet(Tweet instance);
 private void detach_Tweets(Tweet entity)
 {
     this.SendPropertyChanging("Tweets");
     entity.TwitterConversation = null;
 }
 private void detach_Tweets(Tweet entity)
 {
     this.SendPropertyChanging("Tweets");
     entity.DevTwitterAccount = null;
 }