public SentimentAnalyser() { NameValueCollection configSettings = ConfigurationManager.AppSettings; _sentimentUrl = configSettings["SENTIMENT_URL"]; _sentimentApi = configSettings["SENTIMENT_API"]; Receive <SocialMediaMaster.MessageToBeAnalysedAndHashtagged>(msg => { string uriString = String.Format("{0}={1}{2}", "text", HttpUtility.UrlEncode(msg.Tweet.Text), _sentimentApi); var wc = new WebClient(); wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded"; wc.Headers[HttpRequestHeader.Accept] = "application/json"; wc.UploadStringTaskAsync(new Uri(_sentimentUrl), uriString.ToString(CultureInfo.InvariantCulture)) .ContinueWith(task => { #region getScoreFromTaskResultJSON string html = null; float score = 0; try { var json = JsonConvert.DeserializeObject <dynamic>(task.Result); if (!float.TryParse(Convert.ToString(json.aggregate.score), out score)) { score = 0; } } catch (AggregateException) { // TODO ; } #endregion #region getTweetDetails string message = msg.Tweet.Text; string hashtag = msg.Hashtag; ITweet tweet = msg.Tweet; IOEmbedTweet embededtweet = tweet.GenerateOEmbedTweet(); if (embededtweet != null) { html = embededtweet.HTML; } #endregion return(new MessageAlreadyAnalysed(message, hashtag, score, html)); }, TaskContinuationOptions.AttachedToParent & TaskContinuationOptions.ExecuteSynchronously) .PipeTo(Sender); }); }
public static string ReadHtmlContentFromIOembededTweet(ITweet tweet) { string htmlContent = null; IOEmbedTweet aux = Tweet.GetOEmbedTweet(tweet.Id); if (aux != null) { string htmlCode = aux.HTML; if (htmlCode != null) { TwitterSearcher.htmlDocument.LoadHtml(htmlCode); htmlContent = TwitterSearcher.htmlDocument.DocumentNode.InnerText; } } return(htmlContent); }
/// <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(); } }