public static void ProcessTweet(Tweet t) { ConsoleOutput.PrintTweet(t); var file = Path.Combine("WorkingDir",String.Format("{0}.json", t.Id)); var serializedTweet = Newtonsoft.Json.JsonConvert.SerializeObject(t, Formatting.Indented); File.WriteAllText(file, serializedTweet, Encoding.UTF8); }
/// <summary> /// https://dev.twitter.com/docs/api/1.1/post/favorites/destroy /// Un-favourites a given tweet /// </summary> /// <param name="tweet">Tweet for to favourite</param> /// <returns></returns> public async static Task<Tweet> DeleteFavourite(this IUserSession session, Tweet tweet) { var parameters = new TwitterParametersCollection(); parameters.Create(id: tweet.Id); var url = TwitterApi.Resolve("/1.1/favorites/destroy.json"); return await session.PostAsync(url, parameters) .ContinueWith(c => c.MapToSingle<Tweet>()); }
public async Task Delete_Favourite_For_CurrentUser_ReturnsResult() { // arrange session.Returns(await Json.FromFile("data\\favorites\\unfavorited.txt")); session.ExpectPost("https://api.twitter.com/1.1/favorites/destroy.json"); var tweet = new Tweet { Id = 1234 }; var favourite = await session.DeleteFavourite(tweet); Assert.IsNotNull(favourite); Assert.IsFalse(favourite.Favourited); }
public static void PrintTweet(Tweet t, ConsoleColor fgColour = ConsoleColor.Gray, ConsoleColor bgColour = ConsoleColor.Black) { System.Console.BackgroundColor = bgColour; if (t.OK) { PrintLineHeader(t.User.ScreenName, ConsoleColor.Gray); System.Console.ForegroundColor = fgColour; System.Console.WriteLine("{0}", t.Text); } else PrintError(t.twitterControlMessage.twitter_error_message, fgColour, bgColour); System.Console.ResetColor(); }
public async Task Get_Rate_LimitExceeded_Error_OnSingle() { // Ref: https://dev.twitter.com/docs/rate-limiting/1.1 // arrange errorsession.simulatingError = true; errorsession.httpStatusCode = HttpStatusCode.Gone; // Twitter : Rate Limit exceeded, RFC6585 Too Many Requests errorsession.Returns(await Json.FromFile("data\\errors\\ratelimitexceedederror.txt")); errorsession.ExpectGet("/1.1/favorites/create.json"); var twt = new Tweet(); var favourites = await errorsession.CreateFavourite(twt); Assert.IsNotNull(favourites); favourites.twitterFaulted.Should().BeTrue(); favourites.twitterControlMessage.Should().NotBeNull(); favourites.twitterControlMessage.http_status_code.ShouldBeEquivalentTo(410); // Note: testing 410 as 429 is not an enum }
public async Task Create_Reply_To_Tweet() { // arrange session.Returns(await Json.FromFile("data\\tweets\\singletweetreply.txt")); session.ExpectPost("https://api.twitter.com/1.1/statuses/update.json"); var replytweet = new Tweet { Id = 193577612956811264, User = new User {UserId = 823083}, RawText = "Replying to this faked tweet" }; var tweetreply = await session.ReplyToTweet(replytweet, "@migueldeicaza my issue is if an assembly is compiled against System.Json.dll http://t.co/CByp6ds6 it wont work on the iphone"); Assert.IsNotNull(tweetreply); Assert.IsTrue(tweetreply.Id == 193579947615453184); Assert.IsTrue(tweetreply.InReplyToId == 193577612956811264); Assert.IsTrue(tweetreply.InReplyToUserId == 823083); }
/// <summary> /// Sends a Tweet in reply to another tweet /// </summary> /// <param name="tweet">Tweet replying to</param> /// <param name="text">Text of the reply</param> /// <param name="latitude">Latitude</param> /// <param name="longitude">Longitude</param> /// <param name="placeId">A place in the world identified by a Twitter place ID. Place IDs can be retrieved from geo/reverse_geocode.</param> /// <returns></returns> /// <remarks> ref: https://dev.twitter.com/docs/api/1.1/post/statuses/update </remarks> public async static Task<Tweet> ReplyToTweet(this IUserSession session, Tweet tweet, string text, double latitude=0.0, double longitude = 0.0, string placeId="") { var parameters = new TwitterParametersCollection { {"status", text }, {"in_reply_to_status_id", tweet.Id.ToString()} }; parameters.Create(place_id:placeId); if (Math.Abs(latitude) > 0.0 && Math.Abs(longitude) > 0.0) { parameters.Add("lat", latitude.ToString()); parameters.Add("long", longitude.ToString()); } return await session.PostAsync(TwitterApi.Resolve("/1.1/statuses/update.json"), parameters) .ContinueWith(c => c.MapToSingle<Tweet>()); }
public async Task<bool> DoTweetTest(IUserSession session, List<int> testSeq) { var successStatus = true; try { // 1 long tweetid = 0; if (testSeq.Contains(1)) { ConsoleOutput.PrintMessage("4.1 Tweets\\SendTweet", ConsoleColor.Gray); var tweets1 = await session.SendTweet("Live Fire Test only, please ignore"); if (tweets1.OK) { tweetid = tweets1.Id; ConsoleOutput.PrintMessage( String.Format("From: {0} // Message: {1}", tweets1.User.ScreenName, tweets1.Text )); } else successStatus = false; } var tweets2 = new Tweet(); // 2 if (testSeq.Contains(2)) { ConsoleOutput.PrintMessage("4.2 Tweets\\GetTweet", ConsoleColor.Gray); tweets2 = await session.GetTweet(336377569098207233); if (tweets2.OK) { ConsoleOutput.PrintMessage( String.Format("From: {0} // Message: {1}", tweets2.User.ScreenName, tweets2.Text)); } else successStatus = false; } // 3 if (testSeq.Contains(3)) { ConsoleOutput.PrintMessage("4.3 Tweets\\GetRetweets", ConsoleColor.Gray); var tweets3 = await session.GetRetweets(tweets2); if (tweets3.OK) { foreach (var t in tweets3) { ConsoleOutput.PrintMessage( String.Format("From: {0} // Message: {1}", t.User.ScreenName, t.Text)); } } else successStatus = false; } // 4 if (testSeq.Contains(4)) { ConsoleOutput.PrintMessage("4.4 Tweets\\SendTweetWithImage", ConsoleColor.Gray); var sr = FilesHelper.FromFile("sampleimage\\Boxkite-Logo-github.jpg"); using (var fs = new FileStream(sr, FileMode.Open, FileAccess.Read)) { var tweets4 = await session.SendTweetWithImage("Live Fire Test only, please ignore", Path.GetFileName(sr), fs); if (tweets4.OK) { tweetid = tweets4.Id; ConsoleOutput.PrintMessage( String.Format("From: {0} // Message: {1}", tweets4.User.ScreenName, tweets4.Text)); } else { TwitterLiveFireUserAuth.PrintTwitterErrors(tweets4.twitterControlMessage); successStatus = false; } } } // 5 if (testSeq.Contains(5)) { ConsoleOutput.PrintMessage("4.5 Tweets\\GetTweet - with Extended Entities", ConsoleColor.Gray); var tweets5 = await session.GetTweet(560049149836808192); if (tweets5.OK) { ConsoleOutput.PrintMessage( String.Format("From: {0} // Message: {1}", tweets5.User.ScreenName, tweets5.Text)); ConsoleOutput.PrintMessage( String.Format("Extended Entities Count: {0}", tweets5.ExtendedEntities.Urls.Count())); } else successStatus = false; } } catch (Exception e) { ConsoleOutput.PrintError(e.ToString()); return false; } return successStatus; }
public async Task Create_ReTweet() { // arrange session.Returns(await Json.FromFile("data\\tweets\\retweet.txt")); session.ExpectPost("https://api.twitter.com/1.1/statuses/retweet/243149503589400576.json"); var otweet = new Tweet { Id = 243149503589400576, RawText = "Replying to this faked tweet" }; var retweet = await session.Retweet(otweet); Assert.IsNotNull(retweet); Assert.IsTrue(retweet.Id == 243149503589400576); // id of the retweet itself retweet.Text.ShouldBeEquivalentTo("RT @kurrik: tcptrace and imagemagick - two command line tools TOTALLY worth learning"); }
/// <summary> /// Retweets a tweet /// </summary> /// <param name="tweet">The tweet to retweet</param> /// <returns></returns> /// <remarks> ref: https://dev.twitter.com/docs/api/1.1/post/statuses/retweet/%3Aid </remarks> public async static Task<Tweet> Retweet(this IUserSession session, Tweet tweet) { var parameters = new TwitterParametersCollection(); var path = TwitterApi.Resolve("/1.1/statuses/retweet/{0}.json", tweet.Id); return await session.PostAsync(path, parameters) .ContinueWith(c => c.MapToSingle<Tweet>()); }
public static bool IsARetweet(this Tweet tweet) { return(tweet.RetweetedStatus != null); }
/// <summary> /// Gets the Retweets of a particular tweet /// </summary> /// <param name="tweet"></param> /// <param name="count"></param> /// <returns></returns> /// <remarks> ref: https://dev.twitter.com/docs/api/1.1/get/statuses/retweets/%3Aid </remarks> public async static Task<TwitterResponseCollection<Tweet>> GetRetweets(this ITwitterSession session, Tweet tweet, int count = 20) { var parameters = new TwitterParametersCollection { {"count", count.ToString()}, }; var path = TwitterApi.Resolve("/1.1/statuses/retweets/{0}.json", tweet.Id); return await session.GetAsync(path, parameters) .ContinueWith(c => c.MapToMany<Tweet>()); }
public async Task<bool> DoApplicationOnlyAuthFireTests(TwitterConnection twitterConnection, List<int> testSeq) { var successStatus = true; try { // 1 if (testSeq.Contains(1)) { ConsoleOutput.PrintMessage("12.1 User Time Line//Application Auth Only", ConsoleColor.Gray); var combo1 = await twitterConnection.TwitterSession.GetUserTimeline("KatyPerry"); if (combo1.OK) { foreach (var trnd in combo1) { ConsoleOutput.PrintMessage( String.Format("App Auth Timeline Test: {0}", trnd.Text)); } } else { successStatus = false; throw new Exception("cannot user time line (app only auth)"); } } // end test 1 // 2 if (testSeq.Contains(2)) { ConsoleOutput.PrintMessage("12.2 API Management//Rate limits", ConsoleColor.Gray); var combo2 = await twitterConnection.TwitterSession.GetCurrentApiStatus(); if (combo2.OK) { foreach (var apir in combo2.APIRateStatuses) { ConsoleOutput.PrintMessage( String.Format("API: {0} Limit: {1} Remaining: {2}", apir.Value.APIPath, apir.Value.Limit, apir.Value.Remaining)); } } else { successStatus = false; throw new Exception("cannot api management (app only auth)"); } } // end test 2 // 3 if (testSeq.Contains(3)) { ConsoleOutput.PrintMessage("12.3 Get a Tweet//", ConsoleColor.Gray); var combo3 = await twitterConnection.TwitterSession.GetTweet(464717579861258242); if (combo3.OK) { ConsoleOutput.PrintMessage(combo3.Text); } else { successStatus = false; throw new Exception("cannot get tweet (app only auth)"); } } // end test 3 // 4 if (testSeq.Contains(4)) { ConsoleOutput.PrintMessage("12.4 Get a Retweet//", ConsoleColor.Gray); var oldtweet = new Tweet() {Id = 453310114796412928}; var combo4 = await twitterConnection.TwitterSession.GetRetweets(oldtweet); if (combo4.OK) { ConsoleOutput.PrintMessage(String.Format("Retweeted {0} times",combo4.Count)); } else { successStatus = false; throw new Exception("cannot get retweet (app only auth)"); } } // end test 4 // 5 if (testSeq.Contains(5)) { ConsoleOutput.PrintMessage("12.5 Get friendships//", ConsoleColor.Gray); var combo5 = await twitterConnection.TwitterSession.GetFriendship(sourceScreenName:"shiftkey",targetScreenName:"nickhodgemsft"); if (combo5.OK) { } else { successStatus = false; throw new Exception("cannot get friendships (app only auth)"); } } // end test 5 } catch (Exception e) { ConsoleOutput.PrintError(e.ToString()); return false; } return successStatus; }
/* * NOTE: scrubgeo, statuswithheld, limitnotices, userwithheld and friends stream currently * not .Subscribed to from the UserStream */ private bool IsUnique(Tweet t) { if (_tweetIdsRegister.Contains(t.Id)) return false; _tweetIdsRegister.Add(t.Id); return true; }
/// <summary> /// Sends a Tweet in reply to another tweet /// </summary> /// <param name="tweet">Text to send</param> /// <param name="text">Text of tweet to send</param> /// <param name="fileName">Name of the file, including extension</param> /// <param name="imageDataStream">Stream containing the image</param> /// <param name="latitude">Latitude</param> /// <param name="longitude">Longitude</param> /// <param name="placeId">A place in the world identified by a Twitter place ID. Place IDs can be retrieved from geo/reverse_geocode.</param> /// <returns></returns> /// <remarks> ref: https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media </remarks> public async static Task<Tweet> ReplyToTweetWithImage(this IUserSession session, Tweet tweet, string text, string fileName, Stream imageDataStream, double latitude = 0.0, double longitude = 0.0, string placeId = "") { var parameters = new TwitterParametersCollection { {"status", text}, {"in_reply_to_status_id", tweet.Id.ToString()} }; parameters.Create(place_id: placeId); if (Math.Abs(latitude) > 0.0 && Math.Abs(longitude) > 0.0) { parameters.Add("lat", latitude.ToString()); parameters.Add("long", longitude.ToString()); } return await session.PostFileAsync(TwitterApi.Upload("/1.1/statuses/update_with_media.json"), parameters, fileName, "media[]", srImage: imageDataStream) .ContinueWith(c => c.MapToSingle<Tweet>()); }
private void AddToHomeTimeLine(Tweet t) { // only Publish if unique if (!IsUnique(t)) return; _tweetsseen.OnNext(t.Id); _timeline.OnNext(t); _usersseen.OnNext(t.User); }
public static bool IsAReply(this Tweet tweet) { return(tweet.InReplyToId != null); }