private void StartTwitterUserStream(TwitterContext context)
{
hadUserStreamFailure = false;
context.Log = log;
try
{
userStreamTask = context.Streaming
.Where(s => s.Type == LinqToTwitter.StreamingType.User)
.Select(strm => strm)
.StartAsync(async strm =>
{
await Task.Run(() =>
{
try
{
lastCallBackTimeUserStream = DateTime.Now;
if (userStream == null)
log.WriteLine("{0}: Twitter Connection Established (UserStream)", DateTime.Now);
userStream = strm;
if (strm != null)
{
/* LinqToTwitter v3.0 no longer has *.Status
if (strm.Status == TwitterErrorStatus.RequestProcessingException)
{
var wex = strm.Error as WebException;
if (wex != null && wex.Status == WebExceptionStatus.ConnectFailure)
{
log.WriteLine("{0}: LinqToTwitter UserStream Connection Failure (UserStream)", DateTime.Now);
hadUserStreamFailure = true;
//Will Be Restarted By Processing Queue
}
}
else
*/
if (!string.IsNullOrEmpty(strm.Content))
{
var status = new LinqToTwitter.Status(LitJson.JsonMapper.ToObject(strm.Content));
if (status != null && status.StatusID > 0)
{
var tweet = new Tweet(status.RetweetedStatus.StatusID == 0 ? status : status.RetweetedStatus);
lock (queue_lock)
{
queue.Add(tweet);
}
log.WriteLine("{0}: Added Item to Queue (UserStream): {1}", DateTime.Now, tweet.TweetText);
}
else
{
//If you can handle friends we will look for them
if (processingStep is ITweepProcessingStep)
{
var jsonDataFriends = LitJson.JsonMapper.ToObject(strm.Content)
.FirstOrDefault(x => x.Key == "friends");
//If this is a friends collection update we will notify you
if (!jsonDataFriends.Equals(default(KeyValuePair<string, LitJson.JsonData>)) &&
jsonDataFriends.Value != null &&
jsonDataFriends.Value.IsArray)
{
var friends = new List<LazyLoader<Tweep>>();
for (int i = 0; i < jsonDataFriends.Value.Count; i++)
{
friends.Add(TwitterModel.Instance(PrimaryUser.TwitterScreenName).GetLazyLoadedTweep(ulong.Parse(jsonDataFriends.Value[i].ToString()), Tweep.TweepType.Follower));
}
(processingStep as ITweepProcessingStep).ProcessTweeps(friends);
}
else
log.WriteLine("{0}: Unhandled Item in Stream (UserStream): {1}", DateTime.Now, strm.Content);
}
else
log.WriteLine("{0}: Unhandled Item in Stream (UserStream): {1}", DateTime.Now, strm.Content);
}
}
else
log.WriteLine("{0}: Twitter Keep Alive (UserStream)", DateTime.Now);
}
else
throw new ArgumentNullException("strm", "This value should never be null!");
}
catch (Exception ex)
{
log.WriteLine("{0}: Error (UserStream): {1}", DateTime.Now, ex.ToString());
}
});
});
}
catch (Exception ex)
{
log.WriteLine("{0}: Error (UserStream): {1}", DateTime.Now, ex.ToString());
}
}