From 9890a9c8b463a1eaf4f48b0be06ebd17da29b4e5 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Mon, 16 Jul 2018 01:34:15 +0200 Subject: [PATCH] use a new PocketExploreItem model for the explore results --- PocketSharp/Components/Explore.cs | 14 +- PocketSharp/IPocketClient.cs | 2 +- PocketSharp/Models/PocketExploreItem.cs | 203 ++++++++++++++++++++++++ 3 files changed, 214 insertions(+), 5 deletions(-) create mode 100644 PocketSharp/Models/PocketExploreItem.cs diff --git a/PocketSharp/Components/Explore.cs b/PocketSharp/Components/Explore.cs index 9cec9ee..3c18f0b 100644 --- a/PocketSharp/Components/Explore.cs +++ b/PocketSharp/Components/Explore.cs @@ -31,9 +31,9 @@ namespace PocketSharp /// Term or topic to get articles for /// /// - public async Task> Explore(string topic, CancellationToken cancellationToken = default(CancellationToken)) + public async Task> Explore(string topic, CancellationToken cancellationToken = default(CancellationToken)) { - List items = new List(); + List items = new List(); string html = await RequestAsString("https://getpocket.com/explore/" + HttpUtility.UrlEncode(topic) , cancellationToken); var document = new HtmlDocument(); @@ -49,7 +49,7 @@ namespace PocketSharp for (int i = 0; i < nodes.Count(); i++) { HtmlNode node = nodes.ElementAt(i); - PocketItem item = new PocketItem(); + PocketExploreItem item = new PocketExploreItem(); item.ID = node.Id; HtmlNode title = node.SelectNodeByClass("title")?.FirstChild; @@ -77,7 +77,13 @@ namespace PocketSharp // get basic infos item.Title = title.InnerText; item.Excerpt = node.SelectNodeByClass("excerpt")?.InnerText; - item.IsTrending = node.SelectNodeByClass("flag_trending") != null; + item.IsTrending = node.SelectNodeByClass("flag-trending") != null; + + // save count + string saveCountStr = node.SelectNodeByClass("save_count")?.InnerText?.Split(' ')?.FirstOrDefault(); + int saveCount = 0; + Int32.TryParse(saveCountStr, out saveCount); + item.SaveCount = saveCount; // add published date DateTime publishedDate = DateTime.Now; diff --git a/PocketSharp/IPocketClient.cs b/PocketSharp/IPocketClient.cs index 6845ad6..8850b7c 100644 --- a/PocketSharp/IPocketClient.cs +++ b/PocketSharp/IPocketClient.cs @@ -501,7 +501,7 @@ namespace PocketSharp /// Term or topic to get articles for /// /// - Task> Explore(string topic, CancellationToken cancellationToken = default(CancellationToken)); + Task> Explore(string topic, CancellationToken cancellationToken = default(CancellationToken)); #endregion /// diff --git a/PocketSharp/Models/PocketExploreItem.cs b/PocketSharp/Models/PocketExploreItem.cs new file mode 100644 index 0000000..7f6e86b --- /dev/null +++ b/PocketSharp/Models/PocketExploreItem.cs @@ -0,0 +1,203 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; + +namespace PocketSharp.Models +{ + /// + /// Explore item containing all available data + /// + [JsonObject] + [DebuggerDisplay("Uri = {Uri}, Title = {Title}")] + public class PocketExploreItem : IComparable + { + /// + /// Gets or sets the ID. + /// + /// + /// The ID. + /// + public string ID { get; set; } + + /// + /// Gets or sets the title. + /// + /// + /// The title. + /// + public string Title { get; set; } + + /// + /// Gets or sets the excerpt. + /// + /// + /// The excerpt. + /// + public string Excerpt { get; set; } + + /// + /// Gets or sets the published time. + /// + /// + /// The time when the article was published. + /// + public DateTime? PublishedTime { get; set; } + + /// + /// Gets or sets the URI. + /// + /// + /// The URI. + /// + [JsonIgnore] + public Uri Uri { get; set; } + + /// + /// Gets or sets the save count. + /// + /// + /// The save count. + /// + public int SaveCount { get; set; } + + /// + /// Gets or sets the images. + /// + /// + /// The images. + /// + [JsonConverter(typeof(ObjectToArrayConverter))] + public IEnumerable Images { get; set; } + + /// + /// Gets or sets a value indicating whether this instance is trending. + /// + /// + /// true if this instance is trending; otherwise, false. + /// + public bool IsTrending { get; set; } + + /// + /// Gets the lead image. + /// + /// + /// The lead image. + /// + [JsonIgnore] + public PocketImage LeadImage + { + get { return Images != null && Images.Count() > 0 ? Images.First() : null; } + } + + /// + /// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object. + /// + /// An object to compare with this instance. + /// + /// A value that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes in the sort order. Zero This instance occurs in the same position in the sort order as . Greater than zero This instance follows in the sort order. + /// + int IComparable.CompareTo(object obj) + { + PocketExploreItem item = (PocketExploreItem)obj; + + if (!PublishedTime.HasValue) + { + return 1; + } + if (!item.PublishedTime.HasValue) + { + return -1; + } + + return DateTime.Compare(PublishedTime.Value, item.PublishedTime.Value); + } + + /// + /// Determines whether the specified , is equal to this instance. + /// + /// The to compare with this instance. + /// + /// true if the specified is equal to this instance; otherwise, false. + /// + public override bool Equals(object obj) + { + if (obj == null) + { + return false; + } + + PocketExploreItem item = obj as PocketExploreItem; + + if (item == null) + { + return false; + } + + return ID == item.ID; + } + + + /// + /// Implements the operator ==. + /// + /// A. + /// The b. + /// + /// The result of the operator. + /// + public static bool operator ==(PocketExploreItem a, PocketExploreItem b) + { + if (Object.ReferenceEquals(a, b)) + { + return true; + } + + PocketExploreItem itemA = (PocketExploreItem)a; + PocketExploreItem itemB = (PocketExploreItem)b; + + if ((Object)itemA == null || (Object)itemB == null) + { + return false; + } + + return itemA.ID == itemB.ID; + } + + /// + /// Implements the operator !=. + /// + /// A. + /// The b. + /// + /// The result of the operator. + /// + public static bool operator !=(PocketExploreItem a, PocketExploreItem b) + { + return !(a == b); + } + + /// + /// Returns a hash code for this instance. + /// + /// + /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. + /// + public override int GetHashCode() + { + return ID.GetHashCode(); + } + + /// + /// Returns a that represents this instance. + /// + /// + /// A that represents this instance. + /// + public override string ToString() + { + return ID; + } + } +}