private void StartTwitterTrackerStream(TwitterContext context)
{
hadTrackerStreamFailure = false;
List<string> trackList = null;
context.Log = log;
string track = ConfigurationManager.AppSettings["Track"] ?? (PrimaryUser.Track ?? "");
string[] ignore = (ConfigurationManager.AppSettings["Ignore"] ?? "").ToLower().Split(',');
int minFollowers = int.Parse(ConfigurationManager.AppSettings["MinFollowerCount"] ?? "0");
try
{
if (string.IsNullOrEmpty(track) && !(processingStep is IKeywordSuggestionStep))
{
log.WriteLine("{0}: To track keywords one of the following must be true: \n\t1) AppSetting Property 'Track' Cannot be Null or Empty.\n\t2) UserCollection Property 'Track' Cannot be Null or Empty.\n\t3) ProcessingStep must Implement IKeywordSuggestionStep.", DateTime.Now);
return;
}
else
{
var processIgnoreWords = !string.IsNullOrEmpty(track) ? track.ToLower().Split(',').ToList() : new List<string>();
trackList = !string.IsNullOrEmpty(track) ? track.ToLower().Split(',').ToList() : new List<string>();
if (processingStep is IKeywordSuggestionStep)
{
var keywordSuggestionStep = processingStep as IKeywordSuggestionStep;
if (keywordSuggestionStep != null)
{
/* I have chosen to wrap these calls in seperate try catch statements incase one fails
* the other can still run. This way if the get fails we may still have hope of a reset.
*/
try { keywordSuggestionStep.SetIgnoreKeywords(processIgnoreWords); }
catch { }
try { trackList.AddRange(keywordSuggestionStep.GetKeywordSuggestions().Select(x => x.ToLower()).ToList()); }
catch { }
try { keywordSuggestionStep.ResetHasNewKeywordSuggestions(); }
catch { }
}
}
if (trackList.Count == 0)
{
log.WriteLine("{0}: No Keywords to Track at this time.", DateTime.Now);
return;
}
else
{
if (trackList.Count > MAX_TRACK)
{
trackList = trackList.OrderByDescending(x => x.Length).Take(400).ToList();
log.WriteLine("{0}: Tracking List Exceeds Max {1} Reducing List", DateTime.Now, MAX_TRACK);
}
log.WriteLine("{0}: Attempting to Track: {1}", DateTime.Now, string.Join(",", trackList));
log.WriteLine("{0}: Ignoring : {1}", DateTime.Now, string.Join(",", ignore));
}
trackerStreamTask = context.Streaming
.Where(s => s.Type == LinqToTwitter.StreamingType.Filter && s.Track == string.Join(",", trackList.Distinct()))
.Select(strm => strm)
.StartAsync(async strm =>
{
await Task.Run(() =>
{
try
{
lastCallBackTimeTrackerStream = DateTime.Now;
if (trackerStream == null)
log.WriteLine("{0}: Twitter Connection Established (TrackerStream)", DateTime.Now);
trackerStream = 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 Stream Connection Failure (TrackerStream)", DateTime.Now);
hadTrackerStreamFailure = 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)
{
string statusText = status.Text.ToLower();
if (
trackList.Any(x => statusText.Contains(x)) && //Looking for exact matches
status.User.FollowersCount >= minFollowers && //Meets the follower cutoff
!ignore.Any(x => x != "" && statusText.Contains(x)) //Ignore these
)
{
var tweet = new Tweet(status.RetweetedStatus.StatusID == 0 ? status : status.RetweetedStatus);
lock (queue_lock)
{
queue.Add(tweet);
}
log.WriteLine("{0}: Added Item to Queue (TrackerStream): @{1} said [{2}]", DateTime.Now, tweet.User.ScreenName, tweet.TweetText);
}
}
else
log.WriteLine("{0}: Unhandled Item in Stream (TrackerStream): {1}", DateTime.Now, strm.Content);
}
else
log.WriteLine("{0}: Twitter Keep Alive (TrackerStream)", DateTime.Now);
}
else
throw new ArgumentNullException("strm", "This value should never be null!");
}
catch (Exception ex)
{
log.WriteLine("{0}: Error (TrackerStream): {1}", DateTime.Now, ex.ToString());
}
});
});
}
}
catch (Exception ex)
{
log.WriteLine("{0}: Error (TrackerStream): {1}", DateTime.Now, ex.ToString());
}
}