LinqToTwitter用户流

时间:2014-04-30 01:09:15

标签: twitter webforms stream linq-to-twitter

看过这篇文章:Streaming Tweets with LinqToTwitter

我有这个WebForms代码:

 protected void uxBtnGetTweets_Click(object sender, EventArgs e)
    {
        DataManager dman = new DataManager();
        UsersOpenAuthData data = dman.GetUserTokens(uxTxtScreenName.Text);
        if (data == null)
        {
            throw new Exception("User Not Authorized, No Tokens On File");
        }
        credentials = new SessionStateCredentials()
        {
            ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"],
            ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"],
            OAuthToken = data.OAuthToken,
            AccessToken = data.AccessToken
        };
        auth = new WebAuthorizer
        {
            Credentials = credentials,
            PerformRedirect = authUrl => Response.Redirect(authUrl),
        };
        TwitterContext twitterCtx = new TwitterContext(auth);
        int count = 0;
        int max = 5;
        lines = new List<string>();

        var s = (from strm in twitterCtx.UserStream
                 where strm.Type == UserStreamType.User
                 select strm).StreamingCallback(strm =>
                      {
                          if (strm.Status == TwitterErrorStatus.RequestProcessingException)
                          {
                              WebException wex = strm.Error as WebException;
                              if (wex != null && wex.Status == WebExceptionStatus.ConnectFailure)
                              {
                                  throw (wex);
                              }
                              throw new Exception(strm.Error.ToString());
                          }
                          //Here both "lines" and statusJson are populated
                          lines.Add(strm.Content);
                          JsonData statusJson = JsonMapper.ToObject(strm.Content);
                          if (++count >= max)
                          {
                              strm.CloseStream();
                          }
                      }).SingleOrDefault();

             //Here lc == 0
             int lc = lines.Count;
    }

当steam处于活动状态时,正在填充“lines”列表,就像JsonData对象一样,但是当流关闭IList的行为空时。

我能想到的只是某种范围的问题,但除此之外,我仍然被困在如何从蒸汽循环中收集数据 - 我们必须在循环中将它持久化到SQL或会话吗?

任何建议都非常感谢!

1 个答案:

答案 0 :(得分:0)