如何使用 Tweetsharp 获取主题标签上的所有推文

how to get all tweets on hashtag using Tweetsharp

如何获取代表特定 hashtag/keyword 的所有推文并将其显示在查看页面上?唯一的解决方案发现 returns 一个空异常。

public ActionResult TweetView(string txtTwitterName)
{
    //TwitterService("consumer key", "consumer secret");
    var service = new TwitterService("", "");

    //AuthenticateWith("Access Token", "AccessTokenSecret");
    service.AuthenticateWith("", "");

    TwitterSearchResult tweets = service.Search(new SearchOptions { Q = txtTwitterName, SinceId=29999 });
    IEnumerable<TwitterStatus> status = tweets.Statuses;
    ViewBag.Tweets = tweets;

    return View();
}

查看:

IEnumerable<TwitterStatus> tweets = ViewBag.Tweets as IEnumerable<TwitterStatus>;
foreach (var tweet in tweets)
{
    <div class="tweet">
        <div class="picture">
            <img src="@tweet.User.ProfileImageUrl" alt="@tweet.User.ScreenName" title="@tweet.User.ScreenName" />
        </div>
        <div class="info">
            <span>@tweet.User.Name, @tweet.User.Description - @tweet.User.Location </span>
            <br />
            <a href="https://twitter.com/statuses/@tweet.Id" class="text">
                @tweet.Text
            </a>
            <div class="action">
                @tweet.CreatedDate.AddHours(3).ToString("d/M/yyyy HH:mm:ss")
            </div>
        </div>
        <div class="clear">

        </div>

    </div>
}

我遇到了同样的问题。这是我模糊的解决方案。只要您returned 了所需数量的推文,该功能就会return。希望对你有帮助。

        string maxid = "1000000000000"; // dummy value
        int tweetcount = 0;


        if (maxid != null)
        {
            var tweets_search = twitterService.Search(new SearchOptions { Q = keyword, Count = Convert.ToInt32(count) });
            List<TwitterStatus> resultList = new List<TwitterStatus>(tweets_search.Statuses);
            maxid = resultList.Last().IdStr;
            foreach (var tweet in tweets_search.Statuses)
            {
                try
                {
                    ResultSearch.Add(new KeyValuePair<String, String>(tweet.Id.ToString(), tweet.Text));
                    tweetcount++;
                }
                catch { }
            }

            while (maxid != null && tweetcount < Convert.ToInt32(count))
            {
                maxid = resultList.Last().IdStr;
                tweets_search = twitterService.Search(new SearchOptions { Q = keyword, Count = Convert.ToInt32(count), MaxId = Convert.ToInt64(maxid) });
                resultList = new List<TwitterStatus>(tweets_search.Statuses);
                foreach (var tweet in tweets_search.Statuses)
                {
                    try
                    {
                        ResultSearch.Add(new KeyValuePair<String, String>(tweet.Id.ToString(), tweet.Text));
                        tweetcount++;
                    }
                    catch { }
                }
            }

        }

对于空值,在您看来:

IEnumerable<TwitterStatus> status = ViewBag.Tweets as IEnumerable<TwitterStatus>;
if(status != null )
{
    foreach (var tweet in status)
    {
        <div class="tweet">
            <div class="picture">
                <img src="@tweet.User.ProfileImageUrl" alt="@tweet.User.ScreenName"     
                 title="@tweet.User.ScreenName" />
            </div>
            <div class="info">
                <span>@tweet.User.Name, @tweet.User.Description - @tweet.User.Location </span>
                <br />
                    <a href="https://twitter.com/statuses/@tweet.Id" class="text">
                @tweet.Text
                </a>
                <div class="action">
                    @tweet.CreatedDate.AddHours(3).ToString("d/M/yyyy HH:mm:ss")
                </div>
                </div>
            <div class="clear">

            </div>

        </div>
   }
}

对于您的控制器,将最后的代码修改为:

ViewBag.Tweets = status;

我希望这个 post 会有所帮助,thx :)