Ejemplo n.º 1
0
 public TweetViewModel(Tweet tweet)
 {
     _tweet = tweet;
 }
Ejemplo n.º 2
0
        public List<Tweet> ParseTweets(List<TweetResponse> tweetResponses)
        {
            int days = 0;
            int hours = 0;
            int minutes = 0;
            int seconds = 0;
            string tweetedAt = "";

            List<Tweet> tweets = new List<Tweet>();

            var isoDateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;

            try
            {
                foreach (TweetResponse tweetResponse in tweetResponses)
                {
                    days = 0;
                    hours = 0;
                    minutes = 0;
                    seconds = 0;
                    tweetedAt = "";

                    try
                    {
                        Tweet tweet = new Tweet();

                        tweet.TweetID = tweetResponse.Id;

                        string createdAt = tweetResponse.createdAt;
                        DateTime createdAtDate = DateTime.ParseExact(createdAt, "ddd MMM dd HH:mm:ss zzzz yyyy", CultureInfo.InvariantCulture);

                        System.DateTime now = DateTime.Now;
                        System.TimeSpan diff1 = now.Subtract(createdAtDate);

                        days = diff1.Days;
                        hours = diff1.Hours;
                        minutes = diff1.Minutes;
                        seconds = diff1.Seconds;

                        if (days > 0)
                        {
                            if (days == 1)
                            {
                                tweetedAt = "1 day ago";
                            }
                            else
                            {
                                tweetedAt = days + " days ago";
                            }
                        }
                        else if (hours > 0)
                        {
                            if (hours == 1)
                            {
                                tweetedAt = "1 hour ago";
                            }
                            else
                            {
                                tweetedAt = hours + " hours ago";
                            }
                        }
                        else if (minutes > 0)
                        {
                            if (minutes == 1)
                            {
                                tweetedAt = "1 minute ago";
                            }
                            else
                            {
                                tweetedAt = minutes + " minutes ago";
                            }
                        }

                        tweet.CreatedAt = tweetedAt;

                        using (var client = new WebClient())
                        {
                            string profileImageUrl = tweetResponse.user.profileImageUrl;
                            string fileName = profileImageUrl.Substring(profileImageUrl.LastIndexOf("/") + 1);
                            string localFolder = ConfigurationManager.AppSettings["LocalImageDirectory"].ToString() + "\\Twitter";
                            string localFile = localFolder + "\\" + tweetResponse.Id.ToString() + "_normal.jpg";

                            if (Directory.Exists(localFolder) == false)
                            {
                                Directory.CreateDirectory(localFolder);
                            }

                            if (File.Exists(localFile) == false)
                            {
                                client.DownloadFile(profileImageUrl, localFile);
                            }

                            if (File.Exists(localFile.Replace("_normal", "_bigger")) == false)
                            {
                                client.DownloadFile(profileImageUrl.Replace("_normal", "_bigger"), localFile.Replace("_normal", "_bigger"));
                            }

                            tweet.Text = tweetResponse.tweetText;
                            tweet.User = tweetResponse.user.Name;
                            tweet.ScreenName = tweetResponse.user.screenName;

                            tweet.UserImage = localFile;
                        }

                        tweets.Add(tweet);
                    }
                    catch (Exception ex)
                    {

                    }
                }

            }
            catch (Exception ex)
            {

            }

            return tweets;
        }