Ejemplo n.º 1
0
        // Gets Twitter Feed from azure database
        public static ICollection <StatusTweet> GetTweetsOnlyFeedFromDB(string twitterAccountToDisplay)
        {
            var stList = new List <StatusTweet>();

            try
            {
                using (var dbContext = new OnlyTweetContext())
                {
                    dbContext.Database.Connection.Open();
                    //Get top 20 Status Tweets from the database
                    var query = (from t in dbContext.OnlyTweetDB
                                 where t.ScreenName == twitterAccountToDisplay
                                 orderby t.CreatedAt descending
                                 select t).Take(20);

                    //Console.WriteLine("All Status Tweet in the database:");
                    foreach (var item in query)
                    {
                        var st = new StatusTweet();
                        //Console.WriteLine("{0}:\n{1}\n\t{2}\n\t{3}\n\", item.StatusID, item.ScreenName, item.Text, item.CreatedAt);

                        st.Text           = item.Text;
                        st.CreatedAt      = item.CreatedAt;
                        st.StatusID       = item.StatusID;
                        st.ScreenName     = item.ScreenName;
                        st.StringStatusID = item.StatusID.ToString();

                        switch (item.ScreenName)
                        {
                        case "Linq2Twitr":     //Sample Username: https://twitter.com/Linq2Twitr
                            st.ProfileImageUrlHttps = "https://pbs.twimg.com/profile_images/378800000625948439/57f4351535721aeedc632745ceaacfea_400x400.png";
                            break;

                        default:
                            st.ProfileImageUrlHttps = "";
                            break;
                        }

                        stList.Add(st);
                    }
                }
            }
            catch (Exception ex)
            {
                string innerMessage = (ex.InnerException != null) ? ex.InnerException.Message : String.Empty;
                //Console.WriteLine("{0}\n{1}", ex.Message, innerMessage);
            }

            return(stList.ToList());
        }
        //Gets new maxID
        public static ulong GetNewMaxID(string twitterAccountToDisplay)
        {
            //string newStatusID = "No MaxID found"; //UInt64
            ulong newMaxID = 563722001925218304; // 544516702412723892;
            try
            {
                using (var db = new OnlyTweetContext())
                {
                    //Get only Status Tweet with New MaxID/StatusID from the database 
                    var query = (from t in db.OnlyTweetDB
                                 where t.ScreenName == twitterAccountToDisplay
                                 orderby t.CreatedAt descending
                                 select new
                                 {
                                     maxID = t.StatusID
                                 }).Take(1);

                    //Console.WriteLine("All Status Tweet in the database:");
                    foreach (var item in query)
                    {
                        //Console.WriteLine("New MaxID is: {0}", item.StatusID);                       
                        //newStatusID = item.maxID.ToString();
                        newMaxID = (ulong)item.maxID;
                        break;
                    }

                }
            }
            catch (Exception ex)
            {
                string innerMessage = (ex.InnerException != null) ? ex.InnerException.Message : String.Empty;
                //Console.WriteLine("{0}\n{1}", ex.Message, innerMessage);
            }

            return newMaxID; // newStatusID;
        }
Ejemplo n.º 3
0
        //Gets new maxID
        public static ulong GetNewMaxID(string twitterAccountToDisplay)
        {
            //string newStatusID = "No MaxID found"; //UInt64
            ulong newMaxID = 563722001925218304; // 544516702412723892;

            try
            {
                using (var db = new OnlyTweetContext())
                {
                    //Get only Status Tweet with New MaxID/StatusID from the database
                    var query = (from t in db.OnlyTweetDB
                                 where t.ScreenName == twitterAccountToDisplay
                                 orderby t.CreatedAt descending
                                 select new
                    {
                        maxID = t.StatusID
                    }).Take(1);

                    //Console.WriteLine("All Status Tweet in the database:");
                    foreach (var item in query)
                    {
                        //Console.WriteLine("New MaxID is: {0}", item.StatusID);
                        //newStatusID = item.maxID.ToString();
                        newMaxID = (ulong)item.maxID;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                string innerMessage = (ex.InnerException != null) ? ex.InnerException.Message : String.Empty;
                //Console.WriteLine("{0}\n{1}", ex.Message, innerMessage);
            }

            return(newMaxID); // newStatusID;
        }
Ejemplo n.º 4
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);
        }
        // Gets Twitter Feed from azure database
        public static ICollection<StatusTweet> GetTweetsOnlyFeedFromDB(string twitterAccountToDisplay)
        {
            var stList = new List<StatusTweet>();
            try
            {
                using (var dbContext = new OnlyTweetContext())
                {
                    dbContext.Database.Connection.Open();
                    //Get top 20 Status Tweets from the database 
                    var query = (from t in dbContext.OnlyTweetDB
                                 where t.ScreenName == twitterAccountToDisplay
                                 orderby t.CreatedAt descending
                                 select t).Take(20);

                    //Console.WriteLine("All Status Tweet in the database:");
                    foreach (var item in query)
                    {
                        var st = new StatusTweet();
                        //Console.WriteLine("{0}:\n{1}\n\t{2}\n\t{3}\n\", item.StatusID, item.ScreenName, item.Text, item.CreatedAt);

                        st.Text = item.Text;
                        st.CreatedAt = item.CreatedAt;
                        st.StatusID = item.StatusID;
                        st.ScreenName = item.ScreenName;
                        st.StringStatusID = item.StatusID.ToString();

                        switch (item.ScreenName)
                        {
                            case "Linq2Twitr": //Sample Username: https://twitter.com/Linq2Twitr
                                st.ProfileImageUrlHttps = "https://pbs.twimg.com/profile_images/378800000625948439/57f4351535721aeedc632745ceaacfea_400x400.png";
                                break;
                            default:
                                st.ProfileImageUrlHttps = "";
                                break;
                        }

                        stList.Add(st);
                    }

                }
            }
            catch (Exception ex)
            {
                string innerMessage = (ex.InnerException != null) ? ex.InnerException.Message : String.Empty;
                //Console.WriteLine("{0}\n{1}", ex.Message, innerMessage);
            }

            return stList.ToList();
        }
        //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;
        }