Example #1
0
        private static void UpdateTwitterUserDaily()
        {
            using (var db = new LiteDatabase("TwitterData.db"))
            {
                try
                {
                    Console.WriteLine(">>> Getting Collection...");
                    var col = db.GetCollection <TwitterUserDailyModel>("TwitterUserDaily");

                    var update = col.FindAll().Where(s => s.TwitterName == null);

                    Console.WriteLine("Entries that will be updated: " + update.Count());

                    var twitterUser = db.GetCollection <TwitterUserModel>("TwitterUser");
                    Console.WriteLine(">>> Getting TwitterUser from Main Database...");

                    Console.WriteLine(">>> Starting Update...");


                    //Update TwitterID and TwitterName
                    //use Tweetinvi to get the TwitterID
                    //or use existing TwitterUserDatabase

                    foreach (var x in update)
                    {
                        try
                        {
                            var userDetail = twitterUser.FindAll().Where(s => s.Screen_name == x.Screen_name).First();

                            if (userDetail != null)
                            {
                                Console.WriteLine(">>> Updating..." + x.Screen_name + " TwitterUser found: " + userDetail.Id);


                                var twitterUserDaily = new TwitterUserDailyModel
                                {
                                    Id               = x.Id,
                                    Screen_name      = x.Screen_name,
                                    Statuses_count   = x.Statuses_count,
                                    Followers_count  = x.Followers_count,
                                    Friends_count    = x.Friends_count,
                                    Favourites_count = x.Favourites_count,
                                    Listed_count     = x.Listed_count,
                                    DateToday        = x.DateToday,
                                    TwitterId        = userDetail.Id,
                                    TwitterName      = userDetail.Name
                                };

                                col.Update(twitterUserDaily);

                                Console.WriteLine(">>> Updating..." + userDetail.Id + " successful...");
                            }
                            else
                            {
                                Console.WriteLine(">>> Skipped..." + x.Id);
                            }
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Exception: " + ex);
                        }
                    }
                }
                catch (LiteException ex)
                {
                    Console.WriteLine("LiteDB Exception: " + ex);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception: " + ex);
                }

                Console.WriteLine("Update successful");
                Console.ReadKey();
            }
        }
Example #2
0
        private static void InsertNewUserDaily()
        {
            IEnumerable <TwitterUserDailyModel> update;
            IEnumerable <TwitterStreamModel>    update2;

            try
            {
                Console.WriteLine("How many past days do you want to Input...?");
                var days = Convert.ToInt16(Console.ReadLine());

                Console.WriteLine("Which Data Collection do you want to use...?\n1 - TwitterUserDaily\n2 - TwitterStream ");
                var collection = Convert.ToInt16(Console.ReadLine());

                try
                {
                    using (var db2 = new LiteDatabase("TwitterData2.db"))
                        using (var db1 = new LiteDatabase("TwitterData.db"))
                        {
                            if (collection == 1)
                            {
                                var from = db2.GetCollection <TwitterUserDailyModel>("TwitterUserDaily");
                                update = from.FindAll().Where(s => s.DateToday == DateTime.Today.AddDays(-days));
                                var updatecheck = from.FindAll();
                                Console.WriteLine("Do you want to update... " + "\n" + update.Count() + "\t" + DateTime.Today.AddDays(-1) + "\n" + updatecheck.Count() + "\t" + DateTime.Today + "\n Press enter if YES!");
                                Console.ReadKey();

                                var to = db1.GetCollection <TwitterUserDailyModel>("TwitterUserDaily");

                                foreach (var x in update)
                                {
                                    Console.WriteLine("Insert>" + x.Id);

                                    var twitterUserDaily = new TwitterUserDailyModel
                                    {
                                        Id               = x.Id,
                                        Screen_name      = x.Screen_name,
                                        Statuses_count   = x.Statuses_count,
                                        Followers_count  = x.Followers_count,
                                        Friends_count    = x.Friends_count,
                                        Favourites_count = x.Favourites_count,
                                        Listed_count     = x.Listed_count,
                                        DateToday        = x.DateToday,
                                        TwitterId        = x.Id,
                                        TwitterName      = x.TwitterName
                                    };

                                    to.Upsert(twitterUserDaily);
                                }
                            }
                            if (collection == 2)
                            {
                                var from = db2.GetCollection <TwitterStreamModel>("TwitterStream");
                                update2 = from.FindAll().Where(s => s.TweetCreatedAt > DateTime.Now.AddDays(-days));
                                var updatecheck = from.FindAll();
                                Console.WriteLine("Do you want to update... " + "\n" + update2.Count() + "\t" + DateTime.Today.AddDays(-1) + "\n" + updatecheck.Count() + "\t" + DateTime.Today + "\n Press enter if YES!");
                                Console.ReadKey();

                                var to = db1.GetCollection <TwitterStreamModel>("TwitterStream");

                                foreach (var x in update2)
                                {
                                    Console.WriteLine("Insert>" + x.TweetID);

                                    var twitterStream = new TwitterStreamModel
                                    {
                                        TweetID            = x.TweetID,
                                        TweetUser          = x.TweetUser,
                                        TweetUserName      = x.TweetUserName,
                                        TweetUserID        = x.TweetUserID,
                                        TweetUserPicture   = x.TweetUserPicture,
                                        TweetUserDesc      = x.TweetUserDesc,
                                        TweetText          = x.TweetText,
                                        TweetHashtags      = x.TweetHashtags,
                                        TweetReTweetCount  = x.TweetReTweetCount,
                                        TweetFavoriteCount = x.TweetFavoriteCount,
                                        TweetCreatedAt     = x.TweetCreatedAt,
                                        TweetUrl           = x.TweetUrl
                                    };

                                    to.Upsert(twitterStream);
                                }
                            }

                            Console.WriteLine("=>>>> DB updated....");
                        }
                }
                catch (LiteException ex)
                {
                    Console.WriteLine("=>>> fixing Lite DB error", ex);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("=>>> error", ex);
            }

            Console.ReadKey();
            return;
        }
Example #3
0
        private async void DoWork(object state)
        {
            _logger.LogInformation("===========> TwitterUserDailyService is working. " + DateTime.Now.ToString("dd.MM.yy - hh:mm"));

            //Set Last Update Time
            Config.Parameter.UserDailyDataLastUpdated = DateTime.Now;

            #region   // Try make the TwitterUserDaily Updates

            await Task.Delay((Config.Parameter.TwitterUserDailyTaskDelay * 60) * 1000);

            try
            {
                //Get all members from TwitterList - Tweetinvi
                var list = Tweetinvi.TwitterList.GetExistingList(Config.Parameter.ListName, Config.Parameter.ScreenName);
                //var AllMembers = list.GetMembers(5); //Just 5 Records for Debug Reasons
                var AllMembers = list.GetMembers(list.MemberCount);

                using (var db = new LiteDatabase("TwitterData.db"))
                {
                    // Get Datbase Connection
                    var colTUD = db.GetCollection <TwitterUserDailyModel>("TwitterUserDaily");

                    //foreach user get Data from Twitter and save to database
                    foreach (var x in AllMembers)
                    {
                        // Get timeline for screenname from twitter using Tweetinvi
                        // Change to User ID ASAP!
                        //
                        var user = Tweetinvi.User.GetUserFromScreenName(x.ScreenName);

                        var alreadyUpdated = colTUD.Find(s => s.TwitterId == x.Id).Where(s => s.DateToday == DateTime.Today);

                        if (alreadyUpdated.Count() == 0)
                        {
                            var twitterUserDaily = new TwitterUserDailyModel
                            {
                                Screen_name      = x.ScreenName,
                                Statuses_count   = x.StatusesCount,
                                Followers_count  = x.FollowersCount,
                                Friends_count    = x.FriendsCount,
                                Favourites_count = x.FavouritesCount,
                                Listed_count     = x.ListedCount,
                                DateToday        = DateTime.Today,
                                TwitterId        = x.Id,
                                TwitterName      = x.Name
                            };

                            //Create new database entry for given user
                            colTUD.Insert(twitterUserDaily);
                            _logger.LogInformation(">> TU24...created new dbentry => " + x.ScreenName + " on " + DateTime.Now.ToString("dd MM yy - hh:mm:ss"));
                        }
                        else
                        {
                            _logger.LogInformation(">> TU24...already uptodate => " + x.ScreenName);
                        }

                        await Task.Delay(Config.Parameter.TwitterUserDailyWriteDelay);
                    }
                }
            }
            catch (TwitterException ex)
            {
                _logger.LogInformation("Twitter has problems..." + ex);
            }
            catch (ArgumentException ex)
            {
                _logger.LogInformation("ArgumentException..." + ex);
            }
            catch (LiteException ex)
            {
                _logger.LogInformation("LiteDB Exception..." + ex);
            }
            catch (Exception ex)
            {
                _logger.LogInformation("Exception..." + ex);
            }

            #endregion
        }