diff --git a/BufferAPI/BufferService.cs b/BufferAPI/BufferService.cs index 19923f5..1fc7ac6 100644 --- a/BufferAPI/BufferService.cs +++ b/BufferAPI/BufferService.cs @@ -4,6 +4,7 @@ using System.Threading.Tasks; using AncoraMVVM.Rest; using Newtonsoft.Json; +using System; namespace BufferAPI { @@ -101,6 +102,75 @@ public async Task> PostUpdate(string text, { return await PostUpdate(text, profiles.Select(p => p.Id)); } + + /// + /// Posts an update to the given social media profiles. + /// + /// BufferUpdateCreate object describing the update to create. + /// List with all the social media profile ids. + /// HttpResponse with a BufferUpdateCreating object describing the result of the operation. + public async Task> PostUpdate(BufferUpdateCreate update, IEnumerable profileIds) + { + ParameterCollection param = new ParameterCollection(); + + foreach (var id in profileIds) + param.Add("profile_ids[]", id); + + param.Add("text", update.Text); + param.Add("shorten", update.Shorten.ToString().ToLower()); + param.Add("now", update.Now.ToString().ToLower()); + param.Add("top", update.Top.ToString().ToLower()); + param.Add("attachment", update.Attachment.ToString().ToLower()); + + + // schedule + if (update.ScheduledAt != null && update.ScheduledAt != DateTime.MinValue ) + param.Add("scheduled_at", update.ScheduledAtSeconds); + + // media + if (update.Media.Link != null && update.Media.Link.Trim() != "") + param.Add("media[link]", update.Media.Link); + + if (update.Media.Description != null && update.Media.Description.Trim() != "") + param.Add("media[description]", update.Media.Description); + + if (update.Media.Title != null && update.Media.Title.Trim() != "") + param.Add("media[title]", update.Media.Title); + + if (update.Media.Picture != null && update.Media.Picture.Trim() != "") + param.Add("media[picture]", update.Media.Picture); + + if (update.Media.Photo != null && update.Media.Photo.Trim() != "") + param.Add("media[photo]", update.Media.Photo); + + if (update.Media.Thumbnail != null && update.Media.Thumbnail.Trim() != "") + param.Add("media[thumbnail]", update.Media.Thumbnail); + + + // retweet + if (update.Retweet.TweetId != null && update.Retweet.TweetId.Trim() != "") + param.Add("retweet[tweet_id]", update.Retweet.TweetId); + + if (update.Retweet.Comment != null && update.Retweet.Comment.Trim() != "") + param.Add("retweet[comment]", update.Retweet.Comment); + + + var req = CreateRequest("updates/create.json", HttpMethod.Post, param); + + return await Execute(req); + } + + /// + /// Posts an update to the given social media profiles. + /// + /// BufferUpdateCreate object describing the update to create. + /// List with all the social media profiles. + /// HttpResponse with a BufferUpdateCreating object describing the result of the operation. + public async Task> PostUpdate(BufferUpdateCreate update, IEnumerable profiles) + { + return await PostUpdate(update, profiles.Select(p => p.Id)); + } + #endregion } } diff --git a/BufferAPI/BufferUpdate.cs b/BufferAPI/BufferUpdate.cs index 4936d9b..3f59b93 100644 --- a/BufferAPI/BufferUpdate.cs +++ b/BufferAPI/BufferUpdate.cs @@ -81,4 +81,107 @@ public DateTime DueAt [JsonProperty("via")] public string Via { get; set; } } + + + + /// + /// This class represents the media section of a new update to send to buffer + /// + /// + [JsonObject(MemberSerialization.OptIn)] + public class BufferUpdateMedia + { + //[JsonProperty("link")] + public string Link { get; set; } + + //[JsonProperty("description")] + public string Description { get; set; } + + // [JsonProperty("title")] + public string Title { get; set; } + + //[JsonProperty("picture")] + public string Picture { get; set; } + + //[JsonProperty("photo")] + public string Photo { get; set; } + + //[JsonProperty("thumbnail")] + public string Thumbnail { get; set; } + + } + + /// + /// This class represents the retweet section of a new update to send to buffer + /// + /// + [JsonObject(MemberSerialization.OptIn)] + public class BufferUpdateRetweet + { + //[JsonProperty("tweet_id")] + public string TweetId { get; set; } + + //[JsonProperty("comment")] + public string Comment { get; set; } + + } + + + + + /// + /// This class represents a new update to send to buffer + /// + /// + [JsonObject(MemberSerialization.OptIn)] + public class BufferUpdateCreate + { + public BufferUpdateCreate() + { + Media = new BufferUpdateMedia(); + Retweet = new BufferUpdateRetweet(); + + Shorten = true; + Attachment = true; + } + + + // [JsonProperty("text")] + public string Text { get; set; } + + // [JsonProperty("shorten")] + public bool Shorten { get; set; } + + // [JsonProperty("now")] + public bool Now { get; set; } + + // [JsonProperty("top")] + public bool Top { get; set; } + + // [JsonProperty("media")] + public BufferUpdateMedia Media { get; set; } + + // [JsonProperty("attachment")] + public bool Attachment { get; set; } + + + public DateTime ScheduledAt { get; set; } + + // [JsonProperty("scheduled_at")] + public double ScheduledAtSeconds + { + get + { + return BufferAPI.TimeConversion.DateTimeToUnixTimestamp(ScheduledAt); + } + } + + // [JsonProperty("retweet")] + public BufferUpdateRetweet Retweet { get; set; } + + + } + + + } diff --git a/BufferAPI/TimeConversion.cs b/BufferAPI/TimeConversion.cs new file mode 100644 index 0000000..a6ef094 --- /dev/null +++ b/BufferAPI/TimeConversion.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace BufferAPI +{ + class TimeConversion + { + public static DateTime UnixTimeStampToDateTime(double? unixTimeStamp) + { + // Unix timestamp is seconds past epoch + System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); + dtDateTime = dtDateTime.AddSeconds(unixTimeStamp.Value).ToLocalTime(); + return dtDateTime; + } + + public static double DateTimeToUnixTimestamp(DateTime dateTime) + { + if (dateTime == null) + { + throw new Exception("Date Time is null, cannot convert."); + } + else + { + return (dateTime - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds; + } + } + } + +} \ No newline at end of file