public TwitterConversationDo GetTwitterConversation(int id)
        {
            TwitterConversationDo twitterConversationDo = new TwitterConversationDo();
            var db = new TwitterDataContext(DbConnectionString.Value);

            var dbRelevantTweets = (from t in db.Tweets
                                    where t.TwitterConversationId == id
                                    orderby t.TweetDate ascending
                                    select t).ToList();
            if (dbRelevantTweets.Count > 0)
            {
                try
                {
                    var dbRelevantConversation = (from c in db.TwitterConversations
                                                  where c.Id == id
                                                  select c).Single();

                    twitterConversationDo.TwitterConvId = id;
                    twitterConversationDo.GameAbbrev = dbRelevantConversation.BlizzArea.Game.GameAbbreviation;
                    twitterConversationDo.LanguageAbbrev = dbRelevantConversation.BlizzArea.Language.LanguageAbbreviation;
                    twitterConversationDo.RegionAbbrev = dbRelevantConversation.BlizzArea.Region.RegionAbbreviation;
                    twitterConversationDo.TopicTitle = dbRelevantConversation.TwitterConvTitle;
                    twitterConversationDo.RegionFullName = dbRelevantConversation.BlizzArea.Region.RegionName;

                    List<TweetDo> tweets = new List<TweetDo>();
                    foreach (var tweet in dbRelevantTweets)
                    {
                        TweetDo tweetDo = new TweetDo
                        {
                            DevId = tweet.DevId,
                            PostContent = tweet.TweetContent,
                            PosterName = tweet.TwitterUserName,
                            RawDateStr = tweet.TweetDate.ToString(),
                            Avatar = tweet.UserAvatarUrl,
                            SourceUri = String.Format("https://twitter.com/{0}/status/{1}", tweet.TwitterUserName, tweet.TweetTwitterId),
                            TweetTwitterId = tweet.TweetTwitterId,
                            TweetInResponseToId = tweet.TwitterInResponseToId
                        };
                        tweets.Add(tweetDo);
                    }

                    twitterConversationDo.Tweets = tweets;

                }
                catch (InvalidOperationException)
                {
                    return null;
                }

            }
            else
            {
                return null; //return 404 in presentation layer
            }

            return twitterConversationDo;
        }
Пример #2
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();
                                }
                            }
                        }

                    }

                }
            }
        }
Пример #3
0
        private static void FixTweets()
        {
            var service = new TwitterService(CONSUMER_KEY, CONSUMER_SECRET);
            service.AuthenticateWith(ACCESS_TOKEN, ACCESS_SECRET);

            using (var db = new TwitterDataContext(_connStr))
            {
                var allTweets = (from t in db.Tweets
                                 where t.TwitterInResponseToId == null
                                 select t).ToList();
                allTweets.Reverse();
                Console.WriteLine(allTweets.Count);
                foreach (var tweet in allTweets)
                {
                    Console.WriteLine("{0}\n\n", tweet.TweetContent);
                    try
                    {
                        var desiredTweet = service.GetTweet(
                                    new GetTweetOptions()
                                    {
                                        Id = tweet.TweetTwitterId
                                    });
                        var desiredId = desiredTweet.InReplyToStatusId;
                        tweet.TwitterInResponseToId = desiredId;
                        db.SubmitChanges();
                        Console.WriteLine("Inserted. \n\n");
                    }
                    catch
                    {

                    }
                }
            }
        }
Пример #4
0
        static void Main(string[] args)
        {
            while (true)
            {
                try
                {
                    using (TrackerDataContext db = new TrackerDataContext(_connStr))
                    {
                        var trackerItemsNeedingUpdates = (from t in db.TrackerTableListItems
                                                          where t.IsPstSummaryUpdateRequired == null
                                                          || t.IsPstSummaryUpdateRequired.Value == true
                                                          select t).ToList(); //Need to get them all in a list, because of a weird bug in the ORM.
                        foreach (var item in trackerItemsNeedingUpdates)
                        {
                            if (item.EntryType == 0) //ForumBoards
                            {
                                using (ForumBlogsDataContext postsContext = new ForumBlogsDataContext(_connStr))
                                {
                                    try
                                    {
                                        var desiredPost = (from p in postsContext.Posts
                                                           where p.TopicId == item.BlogOrTopicId
                                                           && p.DirectPostLink.EndsWith("#1")
                                                           select p).Single();

                                        item.FirstPostSummary = GenerateSummary(desiredPost.PostContent, true);
                                        item.IsPstSummaryUpdateRequired = false;
                                    }
                                    catch
                                    {
                                        HandleFailure(item);
                                    }
                                    db.SubmitChanges();
                                }
                            }
                            else if (item.EntryType == 1) //Blog entries
                            {
                                using (ForumBlogsDataContext blogEntriesContext = new ForumBlogsDataContext(_connStr))
                                {
                                    try
                                    {
                                        var desiredPost = (from p in blogEntriesContext.BlogEntries
                                                           where p.Id == item.BlogOrTopicId
                                                           select p).Single();

                                        item.FirstPostSummary = GenerateSummary(desiredPost.BlogContent, false);
                                        item.IsPstSummaryUpdateRequired = false;
                                    }
                                    catch
                                    {
                                        HandleFailure(item);
                                    }
                                    db.SubmitChanges();
                                }
                            }
                            else if (item.EntryType == 2) //Twitter conversations
                            {
                                using (TwitterDataContext twitterContext = new TwitterDataContext(_connStr))
                                {
                                    try
                                    {
                                        var desiredPost = (from p in twitterContext.Tweets
                                                           where p.TwitterConversationId == item.BlogOrTopicId
                                                           orderby p.Id ascending
                                                           select p).Take(1).Single();

                                        item.FirstPostSummary = desiredPost.TweetContentNonHtml;
                                        item.IsPstSummaryUpdateRequired = false;
                                    }
                                    catch
                                    {
                                        HandleFailure(item);
                                    }
                                    db.SubmitChanges();
                                }
                            }
                            else
                            {
                                throw new NotImplementedException("EntryType");
                            }
                        }
                    }
                }
                catch { }

                //Job should run once per minute.
                Console.WriteLine("\nJob done. Sleeping for a minute. - " + DateTime.Now + "\n");
                System.Threading.Thread.Sleep(30 * 1000);
            }
        }
Пример #5
0
        public TrackerDo GetTwitterConversations(int page, string gameAbbrev)
        {
            if (gameAbbrev == null)
                throw new ArgumentNullException("gameAbbrev");

            gameAbbrev = gameAbbrev.ToUpper();

            TrackerDo tracker = new TrackerDo();
            var db = new TwitterDataContext(DbConnectionString.Value);

            List<TwitterConversation> conversations = new List<TwitterConversation>();
            int itemCount = 0;

            if (gameAbbrev == "ALL")
            {
                var query = (from c in db.TwitterConversations
                             orderby c.LastDevResponseDate descending
                             select c);
                itemCount = query.Count();
                conversations = query.Skip((page - 1) * 50).Take(50).ToList();
            }
            else
            {
                var query = (from c in db.TwitterConversations
                             where c.BlizzArea.Game.GameAbbreviation == gameAbbrev
                             orderby c.LastDevResponseDate descending
                             select c);
                itemCount = query.Count();
                conversations = query.Skip((page - 1) * 50).Take(50).ToList();
            }
            List<TrackerTableListItem> trackerListItems = new List<TrackerTableListItem>();
            foreach (var conversation in conversations)
            {
                TrackerTableListItem item = new TrackerTableListItem
                {
                    BlogOrTopicId = conversation.Id,
                    BoardNumber = 0,
                    EntryNumber = 0,
                    EntryType = 2,
                    Game = conversation.BlizzArea.Game.GameAbbreviation,
                    Id = 0,
                    Lang = conversation.BlizzArea.Language.LanguageAbbreviation,
                    LastBlue = conversation.LastBlueResponder,
                    //LastPoster = "",
                    NoReplies = conversation.Tweets.Count,
                    //OpSummary = conversation.Tweets[0].TweetContentNonHtml,
                    Region = conversation.BlizzArea.Region.RegionAbbreviation,
                    SubForum = "Twitter",
                    ThreadAuthor = conversation.Tweets[0].TwitterUserName,
                    TimeOfLatestPost = conversation.LastDevResponseDate,
                    Title = conversation.TwitterConvTitle
                };

                trackerListItems.Add(item);
            }

            tracker.TrackerItems = GetItemsDomainObjects(trackerListItems);
            tracker.PageTitle = "Twitter";
            tracker.TotalItems = itemCount;

            return tracker;
        }