From 899a2e1c4cedc7fea90ea66dc2cbc67197ebee88 Mon Sep 17 00:00:00 2001 From: ceee Date: Wed, 26 Jun 2013 12:17:01 +0200 Subject: [PATCH] complete PocketItem properties; add PocketImage, PocketVideo, PocketAuthor; --- PocketSharp/Components/Retrieve.cs | 2 +- PocketSharp/JsonDeserializer.cs | 14 ++++ PocketSharp/Models/PocketAuthor.cs | 18 +++++ PocketSharp/Models/PocketImage.cs | 21 +++++ PocketSharp/Models/PocketItem.cs | 81 +++++++++++++++++++ PocketSharp/Models/PocketTag.cs | 9 +-- PocketSharp/Models/PocketVideo.cs | 18 +++++ PocketSharp/Models/{ => Response}/Modify.cs | 0 .../Models/{ => Response}/ResponseBase.cs | 0 PocketSharp/Models/{ => Response}/Retrieve.cs | 16 +--- PocketSharp/PocketClient.cs | 25 ++++++ PocketSharp/PocketSharp.csproj | 9 ++- 12 files changed, 189 insertions(+), 24 deletions(-) create mode 100644 PocketSharp/Models/PocketAuthor.cs create mode 100644 PocketSharp/Models/PocketImage.cs create mode 100644 PocketSharp/Models/PocketVideo.cs rename PocketSharp/Models/{ => Response}/Modify.cs (100%) rename PocketSharp/Models/{ => Response}/ResponseBase.cs (100%) rename PocketSharp/Models/{ => Response}/Retrieve.cs (52%) diff --git a/PocketSharp/Components/Retrieve.cs b/PocketSharp/Components/Retrieve.cs index 014b256..053395d 100644 --- a/PocketSharp/Components/Retrieve.cs +++ b/PocketSharp/Components/Retrieve.cs @@ -53,7 +53,7 @@ namespace PocketSharp break; } - parameters.DetailType = DetailTypeEnum.complete; + //parameters.DetailType = DetailTypeEnum.complete; return Get("get", parameters.Convert()).Items; } diff --git a/PocketSharp/JsonDeserializer.cs b/PocketSharp/JsonDeserializer.cs index 8476623..8a60eb8 100644 --- a/PocketSharp/JsonDeserializer.cs +++ b/PocketSharp/JsonDeserializer.cs @@ -2,6 +2,7 @@ using RestSharp.Deserializers; using ServiceStack.Text; using System; +using System.Collections.Generic; namespace PocketSharp { @@ -23,6 +24,7 @@ namespace PocketSharp return JsonSerializer.DeserializeFromString(response.Content); } + /// /// Adds custom deserialization for specific types. /// @@ -30,8 +32,20 @@ namespace PocketSharp { // generate correct Uri format JsConfig.DeSerializeFn = value => new Uri(value); + + // create DateTime from UNIX timestamp input + JsConfig.DeSerializeFn = value => + { + if(value == "0") + { + return null; + } + System.DateTime dateTimeBeginUnixEpoch = new DateTime(1970,1,1,0,0,0,0); + return dateTimeBeginUnixEpoch.AddSeconds(Convert.ToDouble(value)).ToLocalTime(); + }; } + /// /// Gets or sets the date format. /// diff --git a/PocketSharp/Models/PocketAuthor.cs b/PocketSharp/Models/PocketAuthor.cs new file mode 100644 index 0000000..cc9106f --- /dev/null +++ b/PocketSharp/Models/PocketAuthor.cs @@ -0,0 +1,18 @@ +using System; +using System.Runtime.Serialization; + +namespace PocketSharp.Models +{ + [DataContract] + public class PocketAuthor + { + [DataMember(Name = "author_id")] + public string ID { get; set; } + + [DataMember] + public string Name { get; set; } + + [DataMember(Name = "url")] + public Uri Uri { get; set; } + } +} diff --git a/PocketSharp/Models/PocketImage.cs b/PocketSharp/Models/PocketImage.cs new file mode 100644 index 0000000..91815bd --- /dev/null +++ b/PocketSharp/Models/PocketImage.cs @@ -0,0 +1,21 @@ +using System; +using System.Runtime.Serialization; + +namespace PocketSharp.Models +{ + [DataContract] + public class PocketImage + { + [DataMember(Name = "image_id")] + public string ID { get; set; } + + [DataMember] + public string Caption { get; set; } + + [DataMember] + public string Credit { get; set; } + + [DataMember(Name = "src")] + public Uri Uri { get; set; } + } +} diff --git a/PocketSharp/Models/PocketItem.cs b/PocketSharp/Models/PocketItem.cs index f3e2648..fde2ca1 100644 --- a/PocketSharp/Models/PocketItem.cs +++ b/PocketSharp/Models/PocketItem.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Runtime.Serialization; namespace PocketSharp.Models @@ -21,8 +22,88 @@ namespace PocketSharp.Models [DataMember] public string Excerpt { get; set; } + [DataMember] + public int Status { get; set; } + [DataMember(Name = "favorite")] public bool IsFavorite { get; set; } + [IgnoreDataMember] + public bool IsArchive { get { return Status == 1; } } + + [IgnoreDataMember] + public bool IsDeleted { get { return Status == 2; } } + + [DataMember(Name = "is_article")] + public bool IsArticle { get; set; } + + [DataMember(Name = "has_image")] + public bool HasImage { get; set; } + + [DataMember(Name = "has_video")] + public bool HasVideo { get; set; } + + [DataMember(Name = "word_count")] + public int WordCount { get; set; } + + [DataMember(Name = "sort_id")] + public int Sort { get; set; } + + + [DataMember(Name = "time_added")] + public DateTime? AddTime { get; set; } + + [DataMember(Name = "time_updated")] + public DateTime? UpdateTime { get; set; } + + [DataMember(Name = "time_read")] + public DateTime? ReadTime { get; set; } + + [DataMember(Name = "time_favorited")] + public DateTime? FavoriteTime { get; set; } + + + [DataMember(Name = "tags")] + public Dictionary _TagDictionary { get; set; } + + [DataMember(Name = "images")] + public Dictionary _ImageDictionary { get; set; } + + [DataMember(Name = "videos")] + public Dictionary _VideoDictionary { get; set; } + + [DataMember(Name = "authors")] + public Dictionary _AuthorDictionary { get; set; } + + + [IgnoreDataMember] + public List Tags + { + get { return PocketClient.DictionaryToList(_TagDictionary); } + } + + [IgnoreDataMember] + public List Images + { + get { return PocketClient.DictionaryToList(_ImageDictionary); } + } + + [IgnoreDataMember] + public PocketImage LeadImage + { + get { return Images != null ? Images[0] : null; } + } + + [IgnoreDataMember] + public List Videos + { + get { return PocketClient.DictionaryToList(_VideoDictionary); } + } + + [IgnoreDataMember] + public List Authors + { + get { return PocketClient.DictionaryToList(_AuthorDictionary); } + } } } diff --git a/PocketSharp/Models/PocketTag.cs b/PocketSharp/Models/PocketTag.cs index 9d212a4..37bcc50 100644 --- a/PocketSharp/Models/PocketTag.cs +++ b/PocketSharp/Models/PocketTag.cs @@ -1,14 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.Serialization; -using System.Text; +using System.Runtime.Serialization; namespace PocketSharp.Models { [DataContract] public class PocketTag { - + [DataMember(Name = "tag")] + public string Name { get; set; } } } diff --git a/PocketSharp/Models/PocketVideo.cs b/PocketSharp/Models/PocketVideo.cs new file mode 100644 index 0000000..9b011ac --- /dev/null +++ b/PocketSharp/Models/PocketVideo.cs @@ -0,0 +1,18 @@ +using System; +using System.Runtime.Serialization; + +namespace PocketSharp.Models +{ + [DataContract] + public class PocketVideo + { + [DataMember(Name = "video_id")] + public string ID { get; set; } + + [DataMember(Name = "vid")] + public string ExternalID { get; set; } + + [DataMember(Name = "src")] + public Uri Uri { get; set; } + } +} diff --git a/PocketSharp/Models/Modify.cs b/PocketSharp/Models/Response/Modify.cs similarity index 100% rename from PocketSharp/Models/Modify.cs rename to PocketSharp/Models/Response/Modify.cs diff --git a/PocketSharp/Models/ResponseBase.cs b/PocketSharp/Models/Response/ResponseBase.cs similarity index 100% rename from PocketSharp/Models/ResponseBase.cs rename to PocketSharp/Models/Response/ResponseBase.cs diff --git a/PocketSharp/Models/Retrieve.cs b/PocketSharp/Models/Response/Retrieve.cs similarity index 52% rename from PocketSharp/Models/Retrieve.cs rename to PocketSharp/Models/Response/Retrieve.cs index 4d0a088..1580d28 100644 --- a/PocketSharp/Models/Retrieve.cs +++ b/PocketSharp/Models/Response/Retrieve.cs @@ -14,24 +14,12 @@ namespace PocketSharp.Models public int Since { get; set; } [DataMember(Name = "list")] - public Dictionary ItemDictionary { get; set; } + public Dictionary _ItemDictionary { get; set; } [IgnoreDataMember] public List Items { - get - { - var itemEnumerator = ItemDictionary.GetEnumerator(); - List result = new List(); - - while (itemEnumerator.MoveNext()) - { - result.Add(itemEnumerator.Current.Value); - } - - return result; - } - private set {} + get { return PocketClient.DictionaryToList(_ItemDictionary); } } } } diff --git a/PocketSharp/PocketClient.cs b/PocketSharp/PocketClient.cs index 009c7d5..8790123 100644 --- a/PocketSharp/PocketClient.cs +++ b/PocketSharp/PocketClient.cs @@ -266,5 +266,30 @@ namespace PocketSharp throw new APIException("No access token available. Use authentification first."); } } + + + /// + /// Convert a dictionary to a list + /// + /// + /// The items. + /// + internal static List DictionaryToList(Dictionary items) where T : new() + { + if(items == null) + { + return null; + } + + var itemEnumerator = items.GetEnumerator(); + List list = new List(); + + while (itemEnumerator.MoveNext()) + { + list.Add(itemEnumerator.Current.Value); + } + + return list; + } } } diff --git a/PocketSharp/PocketSharp.csproj b/PocketSharp/PocketSharp.csproj index 4e9aab4..4458ea9 100644 --- a/PocketSharp/PocketSharp.csproj +++ b/PocketSharp/PocketSharp.csproj @@ -57,15 +57,18 @@ - + + + + - - + +