// All timestamps are in UTC time. So UTC is used throughout the app public HomeController(TweetDbContext dbContext) { context = dbContext; }
public HashtagController(TweetDbContext dbContext) { context = dbContext; }
public UsersController(TweetDbContext context) { _context = context; }
// Method to retrun Top Ten hashtags of the past hour public static List <HashtagCount> GetTopTenHour(IEnumerable <Hashtag> hashtagsAll, DateTime dateTimeStart, TweetDbContext context) { // Create variable to hold unix time minus one hour // Time stamps in DB are in unix time Int32 unixTimestampStart = (Int32)(dateTimeStart.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; DateTime dateTimeMinusHour = dateTimeStart.AddHours(-1); Int32 unixTimestampMinusHour = (Int32)(dateTimeMinusHour.Subtract(new DateTime(1970, 1, 1))).TotalSeconds; // Contact DB and retrieve data needed var tweetHashtags = context.TweetHashtags.FromSql("SELECT * FROM TweetHashtags WHERE UnixTimeStamp BETWEEN {0} AND {1}", unixTimestampMinusHour, unixTimestampStart).ToList(); var tweets = context.Tweets.FromSql("SELECT * FROM Tweets WHERE UnixTimeStamp BETWEEN {0} AND {1}", unixTimestampMinusHour, unixTimestampStart).ToList(); IEnumerable <int> tweetHashtagIds = (tweetHashtags.Select(tht => tht.HashtagID)).ToList(); IEnumerable <Hashtag> hashtags = hashtagsAll.Where(ht => tweetHashtagIds.Contains(ht.ID)).ToList();; // HashtagIdCount save the ID of the hashtag, keep track of the number of times it was used, and save a list of the TweetHashtags List <HashtagIdCount> hashtagIdCounts = new List <HashtagIdCount>(); // listof hashtagIds will be used to check if a hashtagIdCount has already been made for that hashtag List <int> hashtagIdList = new List <int>(); // For each unique hashtag create HashtagIdCount object and keep track of how many times it was used foreach (TweetHashtag tweetHashtag in tweetHashtags) { if (hashtagIdList != null && hashtagIdList.Contains(tweetHashtag.HashtagID)) { HashtagIdCount hashtagIdCount = hashtagIdCounts.Single(hid => hid.HashtagId == tweetHashtag.HashtagID); hashtagIdCount.TimesUsed += 1; hashtagIdCount.TweetHashtags.Add(tweetHashtag); } else { HashtagIdCount newHashtagIdCount = new HashtagIdCount { HashtagId = tweetHashtag.HashtagID, TimesUsed = 1, TweetHashtags = new List <TweetHashtag>() }; newHashtagIdCount.TweetHashtags.Add(tweetHashtag); hashtagIdCounts.Add(newHashtagIdCount); hashtagIdList.Add(newHashtagIdCount.HashtagId); } } // Sort HashtagIdCounts by TimesUsed hashtagIdCounts.Sort(new HashtagIdCountComparer()); if (hashtagIdCounts.Count > 10) { // Drop all but Top 10 Hashtags hashtagIdCounts.RemoveRange(10, hashtagIdCounts.Count - 10); } // HastagCount objects contain a Hashtag object, TimesUsed int, and an int[] TweetsPer // HashtagCounts will be used to hold information about how many times the hashtag was used in 5 minute intervals List <HashtagCount> hashtagCounts = new List <HashtagCount>(); // List to keep track of which hashtags already have a HashtagCount object // Will not be passed to ViewModel List <string> hashtagStringList = new List <string>(); foreach (HashtagIdCount hashtagIdCount in hashtagIdCounts) { foreach (TweetHashtag tweetHashtag in hashtagIdCount.TweetHashtags) { Hashtag hashTag = hashtags.Single(h => h.ID == tweetHashtag.HashtagID); if (hashtagStringList != null && hashtagStringList.Contains(hashTag.Name.ToString())) { HashtagCount hashtagCount = hashtagCounts.Single(hc => hc.Hashtag == hashTag); // Twitter API stream only gives 1% of all tweets // So each hashtag collected will be counted 100 times hashtagCount.TimesUsed += 100; AddTweetPerMinute(tweetHashtag, hashtagCount, tweets, dateTimeStart); } else { byte[] bytes = Encoding.Default.GetBytes(hashTag.Name); string hashtagName = (Encoding.UTF8.GetString(bytes)); HashtagCount newHashtagCount = new HashtagCount { Hashtag = hashTag, // Twitter API stream only gives 1% of all tweets // So each hashtag collected will be counted 100 times TimesUsed = 100, HashtagName = hashtagName, // int[] that stores data to be used in line graph // period is intervals for the X axis TweetsPer = new int[12] }; // TODO- tweet is sometimes returning as null, not sure how this is happening // using if to catch null instances Tweet tweet = tweets.SingleOrDefault(t => t.ID == tweetHashtag.TweetID); if (tweet != null) { // adds 100 to the correct 5 minute interval 0-11 newHashtagCount.TweetsPer[Convert.ToInt32(Math.Floor((dateTimeStart - UnixTimeStampToDateTime(tweet.UnixTimeStamp)).Minutes / 5.0))] += 100; } hashtagCounts.Add(newHashtagCount); hashtagStringList.Add(hashTag.Name); } } } // Sort HashtagCounts by TimesUsed hashtagCounts.Sort(new HashtagCountComparer()); if (hashtagCounts.Count > 10) { // Drop all but Top 10 Hashtags hashtagCounts.RemoveRange(10, hashtagCounts.Count - 10); } return(hashtagCounts); }
public PostService(TweetDbContext dbContext) { _dbContext = dbContext; }
public TBTController(TweetDbContext dbContext) { context = dbContext; }
public TweetsController(TweetDbContext context) { _context = context; }
public static void Main(string[] args) { // Wait to call .Run() on BuildWebHost until after stream is setup // or stream logic will not be called var host = BuildWebHost(args); // Getting scope to allow retrieval of DbContext var scope = host.Services.GetService <IServiceScopeFactory>().CreateScope(); context = scope.ServiceProvider.GetRequiredService <TweetDbContext>(); // Set up your credentials (https://apps.twitter.com) // Applies credentials for the current thread.If used for the first time, set up the ApplicationCredentials Auth.SetUserCredentials("5Za8wv3dg9cL1JpMkZ69xHiYR", "q3ieBRSIQGJOXzXjtAuSySHcFQwiKHgKCEccDSbyMYxGc5GbbT", "858816969759444992-ZOBHLzMEq4V9TV6XbYVFk9z9auNRt8v", "gEUnPo24s7dMUbvr0QyfHCbFKbWbzF8XV6F9WXtaudRYc"); var user = User.GetAuthenticatedUser(); // Enable Automatic RateLimit handling RateLimit.RateLimitTrackerMode = RateLimitTrackerMode.TrackAndAwait; var stream = Tweetinvi.Stream.CreateFilteredStream(); /* Filter for API stream * Max 400 char per track but can add multiple tracks * example: stream.AddTrack("#TBT #tbt #TbT") */ stream.AddTrack("#TBT"); stream.MatchingTweetReceived += (sender, recievedTweet) => { Language tweetLanguage; // if language is in DB retrieve it else create new langauge object and save to DB if (GetLanguage(recievedTweet.Tweet.Language.ToString()) != null) { tweetLanguage = GetLanguage(recievedTweet.Tweet.Language.ToString()); tweetLanguage.TimesUsed += 1; } else { Language newLanguage = new Language { Name = recievedTweet.Tweet.Language.ToString() }; context.Languages.Add(newLanguage); context.SaveChanges(); tweetLanguage = newLanguage; } // Create new tweet object and add to db Models.Tweet newTweet = new Models.Tweet { Language = tweetLanguage, DateTime = recievedTweet.Tweet.CreatedAt }; context.Tweets.Add(newTweet); context.SaveChanges(); // if hashtag is in DB retrieve it else create new hashtag object and save to DB List <Hashtag> hashtagList = new List <Hashtag>(); foreach (var hashtag in recievedTweet.Tweet.Hashtags) { if (GetHashtag(hashtag.ToString()) != null) { Hashtag tweetHashtag = GetHashtag(hashtag.ToString()); hashtagList.Add(tweetHashtag); tweetHashtag.TimesUsed += 1; } else { Hashtag newHashtag = new Hashtag { Name = hashtag.ToString() }; context.Hashtags.Add(newHashtag); context.SaveChanges(); hashtagList.Add(newHashtag); } } //TODO - TweetHashtag is not connecting Tweet and Hashtag classes and ID is not auto - incrementing // Create TweetHashtag object for each hashtag //foreach (var hashtag in hashtagList) //{ // TweetHashtag tweetHashtag = new TweetHashtag // { // Tweet = newTweet, // TweetID = newTweet.ID, // Hashtag = hashtag, // HashtagID = hashtag.ID // }; // context.TweetHashtags.Add(tweetHashtag); // context.SaveChanges(); //} }; /* Using Async version of StartStreamMatchingAnyCondition method * without Async the API stream will hold up the stack * shifting it onto another thread allows host.run() to be called * and the web app to run normally */ stream.StartStreamMatchingAnyConditionAsync(); // host.Run() must be called after creation of stream or stream will not be set up host.Run(); // Checks DB for existing hashtag with same name Hashtag GetHashtag(string hashtag) { // FirstorDefault returns null if nothing is found Hashtag existingHashtag = context.Hashtags.FirstOrDefault(h => h.Name == hashtag); return(existingHashtag); } // Checks DB for existing language with same name Language GetLanguage(string language) { // FirstOrDefault returns null if nothing is found Language existingLangauge = context.Languages.FirstOrDefault(l => l.Name == language); return(existingLangauge); } }
public AbstractBaseRepository() { context = new TweetDbContext(); }