public void Start(string track, MatchCondition condition, params Model.Location[] locations) { // Filters if (!string.IsNullOrEmpty(track)) { _stream.AddTrack(track); } if (null != locations && locations.Length > 0) { foreach (var location in locations) { _stream.AddLocation( new Coordinates(location.Latitude1, location.Longitude1), new Coordinates(location.Latitude2, location.Longitude2) ); } } // Start if (condition == MatchCondition.AnyCondition) { _stream.StartStreamMatchingAnyCondition(); } else { _stream.StartStreamMatchingAllConditions(); } }
private void ListenerThreadFunction() { TwitterCredentials.SetCredentials( ConfigurationManager.AppSettings["token_AccessToken"], ConfigurationManager.AppSettings["token_AccessTokenSecret"], ConfigurationManager.AppSettings["token_ConsumerKey"], ConfigurationManager.AppSettings["token_ConsumerSecret"]); while (threadRunning) { try { //var hbase = new HBaseWriter(); stream = Stream.CreateFilteredStream(); var location = Geo.GenerateLocation(-180, -90, 180, 90); stream.AddLocation(location); var tweetCount = 0; var timer = Stopwatch.StartNew(); stream.MatchingTweetReceived += (sender, args) => { tweetCount++; var tweet = args.Tweet; // Store indexItem in the buffer lock (queue) { queue.Enqueue(tweet); } if (timer.ElapsedMilliseconds > 1000) { if (tweet.Coordinates != null) { Context.Logger.Info("{0}: {1} {2}", tweet.Id, tweet.Language.ToString(), tweet.Text); Context.Logger.Info("\tLocation: {0}, {1}", tweet.Coordinates.Longitude, tweet.Coordinates.Latitude); } timer.Restart(); Context.Logger.Info("===== Tweets/sec: {0} =====", tweetCount); tweetCount = 0; } }; stream.StartStreamMatchingAllConditions(); } catch (Exception ex) { Context.Logger.Fatal("Exception: {0}", ex.Message); } } }
/// <summary> /// Starts a Twitter Stream based on the specified geographic coordinates and the now playing hash tag /// </summary> /// <param name="latitude1">Latitude of user location (bottom_left)</param> /// <param name="longitude1">Longitude of user location (bottom_left)</param> /// <param name="latitude2">Latitude of user location (top_right)</param> /// <param name="longitude2">Longitude of user location (top_right)</param> public static async Task StartStream(double latitude1, double longitude1, double latitude2, double longitude2) { // Setup Twitter credentials TweetinviUtilities.SetTwitterCredentials(); // If the stream does not exists... if (_stream == null) { //...then it is started // Create a filtered stream _stream = Stream.CreateFilteredStream(); _stream.AddTrack(Constants.NOWPLAYING_HASHTAG); // Lookup for nowplaying hashtag // OPTIONAL: if you want to see how the feed is updated really quick, just comment the following line of code. // You will see the effect of "infinite scroll" in the client _stream.AddLocation( new Coordinates(latitude1, longitude1), new Coordinates(latitude2, longitude2)); // Lookup in the specific geographic coordinates // OPTIONAL: if you want to filter the stream just for a specific user, uncomment the following line of code //_stream.AddFollow(2834545563); // Event that handles a matching tweet _stream.MatchingTweetReceived += async(sender, args) => { // A OEmbed tweet is sent to the client IOEmbedTweet embedTweet = Tweet.GetOEmbedTweet(args.Tweet); await _context.Clients.All.updateFeed(embedTweet); }; // Start the stream matching all conditions await _stream.StartStreamMatchingAllConditionsAsync(); } else { //... otherwise resume it _stream.ResumeStream(); } }
public void StreamTwitter(Action <TweetDto, string> _Update, string _Connection, List <string> tracks, bool enableLocation) { cancellationToken = new CancellationTokenSource(); Connection = _Connection; regex = new Regex(@"(?<=#)\w+"); Login(); Update = _Update; GetLocation = new OpenStreetMapHelper(); stream = Stream.CreateFilteredStream(); tracks.ForEach(x => stream.AddTrack(x)); if (enableLocation) { stream.AddLocation(new Tweetinvi.Models.Coordinates(49.246292, -123.116226), new Tweetinvi.Models.Coordinates(-33.865143, 151.209900)); } Task.Factory.StartNew(async() => await TweetAnalysis(), cancellationToken.Token); stream.MatchingTweetReceived += (sender, args) => { Tweets.Push(args.Tweet); }; stream.StartStreamMatchingAllConditions(); }