1

我正在使用 Tweetsharp 库来显示用户发布的最新 5 条推文。我在 dev/twitter 创建了一个应用程序并获得了所需的令牌。我想显示自发布推文以来经过的时间。但是 Tweetsharp 库中的“tweet.CreatedDate”方法在发推之前提供了 5-6 小时的时间。

    public DataSet GenerateTweetDataSet()
    {          
        string time = "";
        int hrs = 0, mins = 0;
        int days = 0;

        TwitterService service = new TwitterService(ConsumerKey, ConsumerSecret);
        service.AuthenticateWith(AccessToken, AccessTokenSecret);

        ListTweetsOnUserTimelineOptions tweetoptions = new ListTweetsOnUserTimelineOptions();
        tweetoptions.Count = 5;
        tweetoptions.ScreenName = _twitter.ScreenName;

        var tweets = service.ListTweetsOnUserTimeline(tweetoptions);

        DataTable TweetsTable = new DataTable();
        TweetsTable.Columns.Add("Text");
        TweetsTable.Columns.Add("DateTime");
        TweetsTable.Columns.Add("Id");

        if (tweets != null)
            foreach (var tweet in tweets)
            {
                string UserName = tweet.User.ScreenName;
                string TweetText = tweet.Text;
                DateTime tweetDate = tweet.CreatedDate;
                DateTime currentDate = DateTime.Now;

                time = (currentDate - tweetDate).Days.ToString();
                TimeSpan t1 = currentDate.Subtract(tweetDate);

                mins = (Int32)t1.TotalMinutes;
                hrs = (Int32)t1.TotalHours;
                days = (Int32)t1.TotalDays;

                 if (mins < 60)
                {
                    time = mins + " mins ago";
                }
                else if (mins > 60 && hrs <= 24)
                {
                    time = hrs + " hours ago";
                }

                else if (hrs > 24 && days < 365)
                {
                    time = days + " days ago";
                }
                else if (days > 365)
                {
                    time = "over a year ago";
                }
                long id = tweet.Id;
                TweetsTable.Rows.Add(TweetText, time, id);
            }

        DataSet ds = new DataSet();
        ds.Tables.Add(TweetsTable);

        return ds;
    }        

有人可以指出我哪里出错或如何解决这个问题。如果没有,是否有更好/更简单的方法来获取推文?任何不使用任何 API 库的示例代码?任何帮助将不胜感激。

4

1 回答 1

0
Literal litAuthor = (Literal)e.Item.FindControl("litAuthor");
    Literal litTime = (Literal)e.Item.FindControl("litTime");
    HyperLink btnTwitter = (HyperLink)e.Item.FindControl("btnTwitter");

    TwitterStatus twitterStatus = (TwitterStatus)e.Item.DataItem;

    if (litAuthor != null)
    {
        if (twitterStatus != null)
        {
            TimeSpan diff = DateTime.Now - twitterStatus.CreatedDate;

            if (diff.Days > 0)
            {
                litTime.Text = twitterStatus.CreatedDate.ToString("dd MMM yyyy");
            }
            else if (diff.Hours < 24)
            {
                litTime.Text = diff.Hours.ToString() + "h";
            }
            else
            {
                litTime.Text = diff.Minutes.ToString() + "m";
            }

            litAuthor.Text = twitterStatus.Author.ScreenName;
            btnTwitter.Text = twitterStatus.Text;
            btnTwitter.NavigateUrl = "http://twitter.com/" + twitterStatus.User.ScreenName + "/statuses/" + twitterStatus.IdStr;
        }
    }
于 2014-12-30T04:44:35.847 回答