private static async Task HandleTweetArrival(StreamContent streamContent) { if (streamContent == null) return; var status = streamContent.Entity as Status; if (status == null) return; var tweet = new Tweet { TwitterUserIdentifier = status.User.UserIDResponse, ScreenName = status.User.ScreenNameResponse, UserName = status.User.Name, HashTags = status.Entities.HashTagEntities.Select(h => h.Tag), Urls = status.Entities.UrlEntities.Select(x => x.Url), Text = status.Text, TimeStamp = status.CreatedAt, TweetId_num = status.StatusID, TweetId = status.StatusID.ToString(), Media = status.Entities.MediaEntities }; try { TweetParser.Main(tweet); } catch (System.Exception e) { Debug.Write(e.ToString()); throw; } await Task.Yield(); }
public static void Main(Tweet twitterMessage) { var tweetId = twitterMessage.TweetId_num; var hashtags = twitterMessage.HashTags.Select(h => h.ToLower()).ToList(); if (IsSubmitHashtagMissing(hashtags)) { // Not a submission return; } var day = FindDayFromHashtag(hashtags); if (day == 0) return; using (var adventureContext = new AdventureContext()) { var challenge = GetChallengeForDay(adventureContext, day); if (challenge == null) { TwitterResponder.SendTweetReply(twitterMessage.ScreenName, "Hey! That submission doesn't make any sense to us. Reply @adventiswhat if you think it should.", tweetId); return; } var user = GetUser(adventureContext, twitterMessage.TwitterUserIdentifier) ?? NewUser(twitterMessage); var previouslyComplete = CheckChallengeComplete(adventureContext, challenge, user); if (previouslyComplete == false) { ResponseSaver.SaveNewResponse(adventureContext, twitterMessage, challenge); ContentDeterminier.DetermineContent(twitterMessage, challenge, user, adventureContext); return; } ReplyWithError(twitterMessage, tweetId); } }
private static string StripContent(Tweet tweet) { var removeMentions = new Regex(@"/(^|\b)@\S*($|\b)/"); var removeHashtags = new Regex(@"/(^|\b)#\S*($|\b)/"); var strippedTweet = removeMentions.Replace(tweet.Text, ""); strippedTweet = removeHashtags.Replace(strippedTweet, ""); return strippedTweet; }
public static void CompleteChallenge(Challenge challenge, User user, Tweet tweet, AdventureContext adventureContext) { var userChallenge = new UserChallenge { ChallengeId = challenge.ChallengeId, UserId = user.UserId, IsComplete = true }; adventureContext.UserChallenges.Add(userChallenge); adventureContext.SaveChanges(); TwitterResponder.SendMoreInfo( user.ScreenName, challenge, tweet.TweetId_num); new BadgeFinder(adventureContext).VerifyBadges(user.UserId); }
public static void SaveNewResponse(AdventureContext adventureContext, Tweet tweet, Challenge challenge) { var user = adventureContext.Users .First(u => u.TwitterId == tweet.TwitterUserIdentifier); var response = new Response { UserId = user.UserId, Tweet = tweet.Text, TweetId = tweet.TweetId, ChallengeId = challenge.ChallengeId }; adventureContext.Responses.Add(response); adventureContext.SaveChanges(); }
public static void DetermineContent(Tweet tweet, Challenge challenge, User user, AdventureContext adventureContext) { // Is image? if (tweet.Media.FirstOrDefault() != null && challenge.Type.ToLower() == "image") { TweetParser.CompleteChallenge(challenge, user, tweet, adventureContext); } // Is URL? else if (tweet.Urls.Any()) { var youtubeTest = new Regex("(https?://)?(www\\.)?(yotu\\.be/|youtube\\.com/)?((.+/)?(watch(\\?v=|.+&v=))?(v=)?)([\\w_-]{11})(&.+)?"); var instagramTest = new Regex(@"http://instagr\.?am(?:\.com)?/\S*"); var vineTest = new Regex(@"https://vine.co/v/\w*$@i"); var soundcloudTest = new Regex(@"(https?://)?(www\\.)?( soundcloud.com | snd.sc )(.)"); if (youtubeTest.IsMatch(tweet.Urls.Any().ToString()) && (challenge.Type.ToLower() == "video" | challenge.Type.ToLower() == "audio")) { TweetParser.CompleteChallenge(challenge, user, tweet, adventureContext); } if (instagramTest.IsMatch(tweet.Urls.Any().ToString()) && challenge.Type.ToLower() == "image") { TweetParser.CompleteChallenge(challenge, user, tweet, adventureContext); } if (vineTest.IsMatch(tweet.Urls.Any().ToString()) && challenge.Type.ToLower() == "video") { TweetParser.CompleteChallenge(challenge, user, tweet, adventureContext); } } // Is text response else if (challenge.Type != null && challenge.Type.ToLower() == "text") { tweet.Text = StripContent(tweet); TweetParser.CompleteChallenge(challenge, user, tweet, adventureContext); } }
private static void ReplyWithError(Tweet twitterMessage, ulong twitterUser) { var message = twitterMessage.TimeStamp.Date > DateTime.Now.Date ? ReplyWithAlreadyAnswered(twitterMessage) : ReplyWithTooSoon(); TwitterResponder.SendTweetReply( twitterMessage.ScreenName, message, twitterUser); }
private static string ReplyWithAlreadyAnswered(Tweet twitterMessage) { var dayDifference = (DateTime.Now.Date - twitterMessage.TimeStamp.Date).Days; return "Wow, you're keen! You're a bit ahead of schedule with that #hashtag. Try again in " + dayDifference + " days!"; }
private static User NewUser(Tweet twitterMessage) { var newUser = new User { TwitterId = twitterMessage.TwitterUserIdentifier, UserName = twitterMessage.UserName, ScreenName = twitterMessage.ScreenName }; var adventureContext = new AdventureContext(); adventureContext.Users.Add(newUser); adventureContext.SaveChanges(); return newUser; }