【问题标题】:how to get all tweets on hashtag using Tweetsharp如何使用 Tweetsharp 获取主题标签上的所有推文
【发布时间】:2015-08-14 06:31:42
【问题描述】:

如何代表特定主题标签/关键字获取所有推文并将其显示在查看页面上?找到的唯一解决方案返回 null 异常。

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>
}

【问题讨论】:

    标签: c# asp.net tweets tweetsharp


    【解决方案1】:

    对于空值,在你看来:

    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;
    

    希望这篇文章对你有帮助,谢谢 :)

    【讨论】:

      【解决方案2】:

      我也遇到过同样的问题。这是我模糊的解决方案。只要返回所需数量的推文,该函数就会返回。希望对你有帮助。

              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 { }
                      }
                  }
      
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-10
        • 2012-12-03
        • 1970-01-01
        • 1970-01-01
        • 2017-06-07
        • 2018-07-31
        • 2017-01-05
        • 1970-01-01
        相关资源
        最近更新 更多