public static async Task<TwitterConfiguration> GetConfigurationAsync(this IOAuthCredential credential) { if (credential == null) throw new ArgumentNullException("credential"); var client = credential.CreateOAuthClient(); var json = await client.GetStringAsync(new ApiAccess("help/configuration.json")); return new TwitterConfiguration(DynamicJson.Parse(json)); }
public static async Task<IEnumerable<TwitterStatus>> SearchAsync( this IOAuthCredential credential, string query, string geoCode = null, string lang = null, string locale = null, SearchResultType resultType = SearchResultType.Mixed, int? count = null, DateTime? untilDate = null, long? sinceId = null, long? maxId = null) { if (credential == null) throw new ArgumentNullException("credential"); if (query == null) throw new ArgumentNullException("query"); var param = new Dictionary<string, object> { {"q", query}, {"geocode", geoCode}, {"lang", lang}, {"locale", locale}, {"result_type", resultType.ToString().ToLower()}, {"count", count}, {"until", untilDate != null ? untilDate.Value.ToString("yyyy-MM-dd") : null}, {"since_id", sinceId}, {"max_id", maxId}, }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = await client.GetAsync(new ApiAccess("search/tweets.json", param)); return await response.ReadAsStatusCollectionAsync(); }
public static async Task<IEnumerable<long>> GetNoRetweetsIdsAsync( this IOAuthCredential credential) { if (credential == null) throw new ArgumentNullException("credential"); var client = credential.CreateOAuthClient(); var respStr = await client.GetStringAsync(new ApiAccess("friendships/no_retweets/ids.json")); return await Task.Run(() => ((dynamic[])DynamicJson.Parse(respStr)).Select(d => (long)d)); }
public static async Task<ICursorResult<IEnumerable<long>>> GetBlockingsIdsAsync( this IOAuthCredential credential, long cursor = -1) { var param = new Dictionary<string, object> { {"cursor", cursor}, }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = await client.GetAsync(new ApiAccess("blocks/ids.json", param)); return await response.ReadAsCursoredIdsAsync(); }
public static async Task<TwitterStatus> ShowTweetAsync( this IOAuthCredential credential, long id) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object> { {"id", id}, }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = await client.GetAsync(new ApiAccess("statuses/show.json", param)); return await response.ReadAsStatusAsync(); }
public static async Task<TwitterUser> VerifyCredentialAsync( this IOAuthCredential credential) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object> { {"skip_status", true} }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = await client.GetAsync(new ApiAccess("account/verify_credentials.json", param)); return await response.ReadAsUserAsync(); }
public static async Task<TwitterStatus> DestroyFavoriteAsync( this IOAuthCredential credential, long id) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object> { {"id", id} }.ParametalizeForPost(); var client = credential.CreateOAuthClient(); var response = await client.PostAsync(new ApiAccess("favorites/destroy.json"), param); return await response.ReadAsStatusAsync(); }
public static async Task<ICursorResult<IEnumerable<long>>> GetRetweeterIdsAsync( this IOAuthCredential credential, long id, long cursor = -1) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object> { {"id", id}, {"cursor", cursor} }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = await client.GetAsync(new ApiAccess("retweeters/ids.json", param)); return await response.ReadAsCursoredIdsAsync(); }
public static async Task<IEnumerable<Tuple<long, string>>> GetSavedSearchesAsync( this IOAuthCredential credential) { if (credential == null) throw new ArgumentNullException("credential"); var client = credential.CreateOAuthClient(); var respStr = await client.GetStringAsync(new ApiAccess("saved_searches/list.json")); return await Task.Run(() => { var parsed = DynamicJson.Parse(respStr); return (((dynamic[])parsed).Select( item => Tuple.Create(Int64.Parse((string)item.id_str), (string)item.query))); }); }
public static async Task<Tuple<long, string>> DestroySavedSearchAsync( this IOAuthCredential credential, long id) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object>().ParametalizeForPost(); var client = credential.CreateOAuthClient(); var response = await client.PostAsync(new ApiAccess("saved_searches/destroy/" + id + ".json"), param); var respStr = await response.ReadAsStringAsync(); return await Task.Run(() => { var json = DynamicJson.Parse(respStr); return Tuple.Create(Int64.Parse(json.id_str), json.query); }); }
public static async Task<IEnumerable<TwitterStatus>> GetHomeTimelineAsync( this IOAuthCredential credential, int? count = null, long? sinceId = null, long? maxId = null) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object> { {"count", count}, {"since_id", sinceId}, {"max_id", maxId} }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = await client.GetAsync(new ApiAccess("statuses/home_timeline.json", param)); return await response.ReadAsStatusCollectionAsync(); }
public static async Task<TwitterUser> UpdateProfileImageAsync( this IOAuthCredential credential, byte[] image) { if (credential == null) throw new ArgumentNullException("credential"); if (image == null) throw new ArgumentNullException("image"); var content = new MultipartFormDataContent { {new StringContent("true"), "skip_status"}, {new ByteArrayContent(image), "image", "image.png"} }; var client = credential.CreateOAuthClient(); var response = await client.PostAsync(new ApiAccess("account/update_profile_image.json"), content); return await response.ReadAsUserAsync(); }
public static async Task<IEnumerable<TwitterUser>> SearchUserAsync( this IOAuthCredential credential, string query, int? page = null, int? count = null) { if (credential == null) throw new ArgumentNullException("credential"); if (query == null) throw new ArgumentNullException("query"); var param = new Dictionary<string, object> { {"q", query}, {"page", page}, {"count", count}, }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = await client.GetAsync(new ApiAccess("users/search.json", param)); return await response.ReadAsUserCollectionAsync(); }
public static IObservable<string> ConnectUserStreams( this IOAuthCredential credential, IEnumerable<string> tracks, bool repliesAll = false, bool followingsActivity = false) { var filteredTracks = tracks != null ? tracks.Where(t => !String.IsNullOrEmpty(t)) .Distinct() .JoinString(",") : null; var param = new Dictionary<string, object> { {"track", String.IsNullOrEmpty(filteredTracks) ? null : filteredTracks }, {"replies", repliesAll ? "all" : null}, {"include_followings_activity", followingsActivity ? "true" : null} }.ParametalizeForGet(); return Observable.Create<string>((observer, cancel) => Task.Run(async () => { try { // using GZip cause receiving elements delayed. var client = credential.CreateOAuthClient(useGZip: false); client.Timeout = System.Threading.Timeout.InfiniteTimeSpan; var endpoint = EndpointUserStreams; if (!String.IsNullOrEmpty(param)) { endpoint += "?" + param; } using (var stream = await client.GetStreamAsync(endpoint)) using (var reader = new StreamReader(stream)) { while (!reader.EndOfStream && !cancel.IsCancellationRequested) { var line = reader.ReadLine(); observer.OnNext(line); } } if (!cancel.IsCancellationRequested) { observer.OnCompleted(); } } catch (Exception ex) { observer.OnError(ex); } })); }
public static async Task<IEnumerable<TwitterStatus>> GetSentDirectMessagesAsync( this IOAuthCredential credential, int? count = null, long? sinceId = null, long? maxId = null, int? page = null) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object> { {"count", count}, {"since_id", sinceId}, {"max_id", maxId}, {"page", page}, {"full_text", true}, }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = await client.GetAsync(new ApiAccess("direct_messages/sent.json", param)); return await response.ReadAsStatusCollectionAsync(); }
public static async Task<TwitterUser> UpdateProfileAsync( this IOAuthCredential credential, string name = null, string url = null, string location = null, string description = null) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object> { {"name", name}, {"url", url}, {"location", location}, {"description", description}, {"skip_status", true}, }.ParametalizeForPost(); var client = credential.CreateOAuthClient(); var response = await client.PostAsync(new ApiAccess("account/update_profile.json"), param); return await response.ReadAsUserAsync(); }
public static async Task<long?> GetMyRetweetIdOfStatusAsync( this IOAuthCredential credential, long id) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object> { {"id", id}, {"include_my_retweet", true} }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var respStr = await client.GetStringAsync(new ApiAccess("statuses/show.json", param)); return await Task.Run(() => { var graph = DynamicJson.Parse(respStr); return ((bool)graph.current_user_retweet()) ? Int64.Parse(graph.current_user_retweet.id_str) : null; }); }
public static async Task<IEnumerable<TwitterUser>> GetRetweetsAsync( this IOAuthCredential credential, long id, int? count = null) { if (credential == null) throw new ArgumentNullException("credential"); /* for future compatibility var param = new Dictionary<string, object> { {"id", id}, {"count", count}, }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = client.GetAsync(new ApiAccess("statuses/retweets.json", param)) */ var param = new Dictionary<string, object> { {"count", count}, }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); var response = await client.GetAsync(new ApiAccess("statuses/retweets/" + id + ".json", param)); return await response.ReadAsUserCollectionAsync(); }
public static async Task<TwitterStatus> RetweetAsync( this IOAuthCredential credential, long id) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object>().ParametalizeForPost(); var client = credential.CreateOAuthClient(); var response = await client.PostAsync(new ApiAccess("statuses/retweet/" + id + ".json"), param); return await response.ReadAsStatusAsync(); }
public static async Task<TwitterStatus> UpdateWithMediaAsync( this IOAuthCredential credential, string status, IEnumerable<byte[]> images, bool? possiblySensitive = false, long? inReplyToStatusId = null, Tuple<double, double> geoLatLong = null, string placeId = null, bool? displayCoordinates = null) { if (credential == null) throw new ArgumentNullException("credential"); if (status == null) throw new ArgumentNullException("status"); var param = new Dictionary<string, object> { {"status", status}, {"possibly_sensitive", possiblySensitive}, {"in_reply_to_status_id", inReplyToStatusId}, {"lat", geoLatLong != null ? geoLatLong.Item1 : (double?) null}, {"long", geoLatLong != null ? geoLatLong.Item2 : (double?) null}, {"place_id", placeId}, {"display_coordinates", displayCoordinates} }.Where(kvp => kvp.Value != null); var content = new MultipartFormDataContent(); param.ForEach(kvp => content.Add(new StringContent(kvp.Value.ToString()), kvp.Key)); images.ForEach((b, i) => content.Add(new ByteArrayContent(b), "media[]", "image_" + i + ".png")); var client = credential.CreateOAuthClient(); var response = await client.PostAsync(new ApiAccess("statuses/update_with_media.json"), content); return await response.ReadAsStatusAsync(); }
public static IObservable<string> ConnectUserStreams( this IOAuthCredential credential, IEnumerable<string> tracks, bool repliesAll = false, bool followingsActivity = false) { var filteredTracks = tracks != null ? tracks.Where(t => !String.IsNullOrEmpty(t)) .Distinct() .JoinString(",") : null; var param = new Dictionary<string, object> { {"track", String.IsNullOrEmpty(filteredTracks) ? null : filteredTracks }, {"replies", repliesAll ? "all" : null}, {"include_followings_activity", followingsActivity ? "true" : null} }.ParametalizeForGet(); return Observable.Create<string>((observer, cancel) => Task.Run(async () => { try { // using GZip cause receiving elements delayed. var client = credential.CreateOAuthClient(useGZip: false); // disable connection timeout due to streaming specification client.Timeout = System.Threading.Timeout.InfiniteTimeSpan; var endpoint = EndpointUserStreams; if (!String.IsNullOrEmpty(param)) { endpoint += "?" + param; } using (var stream = await client.GetStreamAsync(endpoint)) using (var reader = new StreamReader(stream)) { // reader.EndOfStream while (!cancel.IsCancellationRequested) { var readLine = reader.ReadLineAsync(); var delay = Task.Delay(TimeSpan.FromSeconds(ApiAccessProperties.StreamingTimeoutSec), cancel); if (await Task.WhenAny(readLine, delay) == delay) { // timeout System.Diagnostics.Debug.WriteLine("#USERSTREAM# TIMEOUT."); break; } var line = readLine.Result; if (line == null) { // connection closed System.Diagnostics.Debug.WriteLine("#USERSTREAM# CONNECTION CLOSED."); break; } if (!String.IsNullOrEmpty(line)) { // successfully completed observer.OnNext(line); } } } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("#USERSTREAM# error detected: " + ex.Message); observer.OnError(ex); return; } System.Diagnostics.Debug.WriteLine("#USERSTREAM# disconnection detected. (CANCELLATION REQUEST? " + cancel.IsCancellationRequested + ")"); if (!cancel.IsCancellationRequested) { System.Diagnostics.Debug.WriteLine("#USERSTREAM# notify disconnection to upper layer."); observer.OnCompleted(); } }, cancel)); }
public static async Task<long> UploadMediaAsync(this IOAuthCredential credential, byte[] image) { if (credential == null) throw new ArgumentNullException("credential"); if (image == null) throw new ArgumentNullException("image"); var content = new MultipartFormDataContent { {new ByteArrayContent(image), "media", System.IO.Path.GetRandomFileName() + ".png"} }; var client = credential.CreateOAuthClient(); var response = await client.PostAsync("https://upload.twitter.com/1.1/media/upload.json", content); var json = await response.ReadAsStringAsync(); return long.Parse(DynamicJson.Parse(json).media_id_string); }
public static async Task<TwitterStatus> UpdateAsync( this IOAuthCredential credential, string status, long? inReplyToStatusId = null, Tuple<double, double> geoLatLong = null, string placeId = null, bool? displayCoordinates = null) { if (credential == null) throw new ArgumentNullException("credential"); if (status == null) throw new ArgumentNullException("status"); var param = new Dictionary<string, object> { {"status", status}, {"in_reply_to_status_id", inReplyToStatusId}, {"lat", geoLatLong != null ? geoLatLong.Item1 : (double?) null}, {"long", geoLatLong != null ? geoLatLong.Item2 : (double?) null}, {"place_id", placeId}, {"display_coordinates", displayCoordinates} }.ParametalizeForPost(); var client = credential.CreateOAuthClient(useGZip: false); var response = await client.PostAsync(new ApiAccess("statuses/update.json"), param); return await response.ReadAsStatusAsync(); }
public static async Task<ICursorResult<IEnumerable<long>>> GetMuteIdsAsync( this IOAuthCredential credential, long cursor = -1) { if (credential == null) throw new ArgumentNullException("credential"); var param = new Dictionary<string, object> { {"cursor", cursor} }.ParametalizeForGet(); var client = credential.CreateOAuthClient(); return await Task.Run(async () => { var respStr = await client.GetStringAsync(new ApiAccess("mutes/users/ids.json", param)); var parsed = DynamicJson.Parse(respStr); var ids = ((dynamic[])parsed.ids).Select(d => (long)d); return new CursorResult<IEnumerable<long>>(ids, parsed.previous_cursor_str, parsed.next_cursor_str); }); }