コード例 #1
0
        /// <summary>
        /// Stores Twitter Feed to database
        /// </summary>
        /// <param name="sTweetsOnlyList"></param>
        /// <returns></returns>
        public bool SetTweetsOnlyToDB(List <OnlyTweet> sTweetsOnlyList)
        {
            var sTweetExist = false;

            try
            {
                using (var db = new OnlyTweetContext())
                {
                    // Create and save a new status tweet
                    //Console.Write("Text for new status tweet : ");

                    int count = 0;
                    foreach (var statusTweet in sTweetsOnlyList)
                    {
                        if (statusTweet != null)
                        {
                            //Console.WriteLine((count + 1) + ". {0}-{1}\n\t{2}", statusTweet.CreatedAt, statusTweet.Text, statusTweet.ScreenName);

                            // Check if Status Tweets already exists in the database
                            var query = from t in db.OnlyTweetDB
                                        where t.Text == statusTweet.Text
                                        orderby t.Text
                                        select t;
                            sTweetExist = false;

                            //Console.WriteLine("Reading all Status Tweets in the database...");
                            foreach (var item in query)
                            {
                                if (String.Equals(item.Text, statusTweet.Text) && String.Equals(item.CreatedAt, statusTweet.CreatedAt))
                                {
                                    // Status Tweet already exists, do not duplicate
                                    Console.WriteLine("Status tweet already exists: \n{0}", statusTweet.Text);
                                    sTweetExist = true;
                                }
                            }

                            //Write Status Tweet to database if title doesn't exist in Db
                            if (!sTweetExist)
                            {
                                var sTweet = new OnlyTweet
                                {
                                    CreatedAt  = statusTweet.CreatedAt,
                                    ScreenName = statusTweet.ScreenName,
                                    Text       = statusTweet.Text,
                                    StatusID   = statusTweet.StatusID
                                };
                                db.OnlyTweetDB.Add(sTweet);
                                db.SaveChanges();

                                count++;
                                sTweetExist = true;
                            }
                        }
                        else
                        {
                            Console.WriteLine("No status tweets found in the database.");
                        }
                    }

                    //Console.WriteLine("Press any key to exit...");
                    //Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                string innerMessage =
                    (ex.InnerException != null) ?
                    ex.InnerException.Message : String.Empty;
                Console.WriteLine("{0}\n{1}", ex.Message, innerMessage);
            }
            return(sTweetExist);
        }
コード例 #2
0
        //Write tweets to azure database
        public static bool SetTweetsOnlyToDB(List<OnlyTweet> sTweetsOnlyList)
        {
            var sTweetExist = false;
            try
            {
                using (var db = new OnlyTweetContext())
                {
                    // Create and save a new status tweet 
                    //Console.Write("Text for new status tweet : ");

                    int count = 0;
                    foreach (var statusTweet in sTweetsOnlyList)
                    {
                        if (statusTweet != null)
                        {
                            //Console.WriteLine((count + 1) + ". {0}-{1}\n\t{2}", statusTweet.CreatedAt, statusTweet.Text, statusTweet.ScreenName);

                            // Check if Status Tweets already exists in the database 
                            var query = from t in db.OnlyTweetDB
                                        where t.Text == statusTweet.Text
                                        orderby t.Text
                                        select t;
                            sTweetExist = false;

                            //Console.WriteLine("Reading all Status Tweets in the database...");
                            foreach (var item in query)
                            {
                                if (String.Equals(item.Text, statusTweet.Text) && String.Equals(item.CreatedAt, statusTweet.CreatedAt))
                                {
                                    // Status Tweet already exists, do not duplicate
                                    Console.WriteLine("Status tweet already exists: \n{0}", statusTweet.Text);
                                    sTweetExist = true;
                                }

                            }

                            //Write Status Tweet to database if title doesn't exist in Db
                            if (!sTweetExist)
                            {
                                var sTweet = new OnlyTweet
                                {
                                    CreatedAt = statusTweet.CreatedAt,
                                    ScreenName = statusTweet.ScreenName,
                                    Text = statusTweet.Text,
                                    StatusID = statusTweet.StatusID
                                };
                                db.OnlyTweetDB.Add(sTweet);
                                db.SaveChanges();

                                count++;
                                sTweetExist = true;
                            }
                        }
                        else
                        {
                            Console.WriteLine("No status tweets found in the database.");
                        }
                    }

                    //Console.WriteLine("Press any key to exit...");
                    //Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                string innerMessage =
                (ex.InnerException != null) ?
                ex.InnerException.Message : String.Empty;
                Console.WriteLine("{0}\n{1}", ex.Message, innerMessage);
            }
            return sTweetExist;
        }