public static void Setup(TestContext test_context) { using (var context = new TwitterAPIContainer()) { context.Database.ExecuteSqlCommand("TRUNCATE TABLE Tweets"); context.Database.ExecuteSqlCommand("TRUNCATE TABLE Words"); for (int i = 0; i < 10; i++) { context.Tweets.Add(new Tweet() { Text = RandomString(140), Mood = null }); context.Words.Add(new Word() { Text = RandomString(6), HappyCount = 100 }); context.Words.Add(new Word() { Text = RandomString(6), AngryCount = 100 }); } context.SaveChanges(); } }
public static void Teardown() { using (var context = new TwitterAPIContainer()) { context.Database.ExecuteSqlCommand("TRUNCATE TABLE Tweets"); context.Database.ExecuteSqlCommand("TRUNCATE TABLE Words"); } }
public static List <Word> MatchedWords(List <String> words) { using (var context = new TwitterAPIContainer()) { return((from t in context.Words where words.Contains(t.Text) select t) .ToList()); } }
public static void ParseTweet(int id, bool Happy) { Tweet tweet = db.Tweets.Find(id); if (tweet == null || tweet.Mood != null) { return; } tweet.Mood = (Happy)? HAPPY : ANGRY; using (var context = new TwitterAPIContainer()) { context.Entry(tweet).State = EntityState.Modified; String[] words = tweet.Text.Trim().Split(WORD_SEPARATORS); foreach (String word in words) { Regex special_chars = new Regex(@"!|\.|\?|;|`|~"); String cleaned_word = special_chars.Replace(word.ToLowerInvariant(), "").Trim(); if (String.IsNullOrWhiteSpace(cleaned_word) || cleaned_word.ToCharArray()[0] == '@') { continue; } Word stored_word = context.Words.FirstOrDefault(w => w.Text == cleaned_word); if (stored_word == null && !BLACKLIST.Contains(cleaned_word)) { int angry_count = (Happy) ? 0 : 1; int happy_count = (Happy) ? 1 : 0; context.Words.Add(new Word() { Text = cleaned_word, AngryCount = angry_count, HappyCount = happy_count }); } else if (stored_word != null && !BLACKLIST.Contains(stored_word.Text)) { if (Happy) { stored_word.HappyCount += 1; } else { stored_word.AngryCount += 1; } context.Entry(stored_word).State = EntityState.Modified; } } context.SaveChanges(); } }
public static List <Tweet> UnratedTweets(int limit = 5, string mood = null) { using (var context = new TwitterAPIContainer()) { return((from t in context.Tweets where t.Mood == mood select t) .OrderBy(x => Guid.NewGuid()) .Take(limit) .ToList()); } }
public static List <Word> AngryWords(int limit = 10) { using (var context = new TwitterAPIContainer()) { return((from t in context.Words where t.HappyCount < t.AngryCount select t) .OrderByDescending(t => t.AngryCount) .Take(limit) .ToList()); } }
public static List <Tweet> GetTweets() { List <Tweet> stored_tweets = Scope.UnratedTweets(tweetlimit); if (stored_tweets.Count == tweetlimit) { return(stored_tweets); } var auth = new SingleUserAuthorizer { CredentialStore = new InMemoryCredentialStore() { ConsumerKey = ConfigurationManager.AppSettings["consumerKey"], ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"], OAuthToken = ConfigurationManager.AppSettings["accessToken"], OAuthTokenSecret = ConfigurationManager.AppSettings["accessTokenSecret"] } }; var twitterCtx = new TwitterContext(auth); var newTweets = new List <Tweet>(); int count = 0; var statusResponse = (from tweet in twitterCtx.Streaming where tweet.Type == StreamingType.Sample #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously select tweet).StartAsync(async strm => { /* * Synchronous call to simplify demo. Would never do this in a production environment * The better solution is to load all tweets, asyncronous to the view, to invisibly * grab more if needed. */ if (strm.EntityType.ToString() == "Status") { Status streamed_status = (Status)strm.Entity; if (streamed_status.Lang == "en") { newTweets.Add(new Tweet() { Text = streamed_status.Text }); } } if (count++ >= 100) { strm.CloseStream(); } }); #pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously statusResponse.Wait(); if (newTweets.Count > 0) { using (var ctx = new TwitterAPIContainer()) { var test = ctx.Tweets.AddRange(newTweets); ctx.SaveChanges(); } } else { throw new Exception("Communication or Rate Limit Error!"); } return(newTweets.Take(tweetlimit).ToList()); }