use a new PocketExploreItem model for the explore results
This commit is contained in:
@@ -31,9 +31,9 @@ namespace PocketSharp
|
||||
/// <param name="topic">Term or topic to get articles for</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<IEnumerable<PocketItem>> Explore(string topic, CancellationToken cancellationToken = default(CancellationToken))
|
||||
public async Task<IEnumerable<PocketExploreItem>> Explore(string topic, CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
List<PocketItem> items = new List<PocketItem>();
|
||||
List<PocketExploreItem> items = new List<PocketExploreItem>();
|
||||
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;
|
||||
|
||||
@@ -501,7 +501,7 @@ namespace PocketSharp
|
||||
/// <param name="topic">Term or topic to get articles for</param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
Task<IEnumerable<PocketItem>> Explore(string topic, CancellationToken cancellationToken = default(CancellationToken));
|
||||
Task<IEnumerable<PocketExploreItem>> Explore(string topic, CancellationToken cancellationToken = default(CancellationToken));
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -0,0 +1,203 @@
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
|
||||
namespace PocketSharp.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Explore item containing all available data
|
||||
/// </summary>
|
||||
[JsonObject]
|
||||
[DebuggerDisplay("Uri = {Uri}, Title = {Title}")]
|
||||
public class PocketExploreItem : IComparable
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the ID.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The ID.
|
||||
/// </value>
|
||||
public string ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the title.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The title.
|
||||
/// </value>
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the excerpt.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The excerpt.
|
||||
/// </value>
|
||||
public string Excerpt { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the published time.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The time when the article was published.
|
||||
/// </value>
|
||||
public DateTime? PublishedTime { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the URI.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The URI.
|
||||
/// </value>
|
||||
[JsonIgnore]
|
||||
public Uri Uri { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the save count.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The save count.
|
||||
/// </value>
|
||||
public int SaveCount { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the images.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The images.
|
||||
/// </value>
|
||||
[JsonConverter(typeof(ObjectToArrayConverter<PocketImage>))]
|
||||
public IEnumerable<PocketImage> Images { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether this instance is trending.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if this instance is trending; otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public bool IsTrending { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the lead image.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The lead image.
|
||||
/// </value>
|
||||
[JsonIgnore]
|
||||
public PocketImage LeadImage
|
||||
{
|
||||
get { return Images != null && Images.Count() > 0 ? Images.First() : null; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
/// <param name="obj">An object to compare with this instance.</param>
|
||||
/// <returns>
|
||||
/// 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 <paramref name="obj" /> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj" />. Greater than zero This instance follows <paramref name="obj" /> in the sort order.
|
||||
/// </returns>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the specified <see cref="System.Object" />, is equal to this instance.
|
||||
/// </summary>
|
||||
/// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param>
|
||||
/// <returns>
|
||||
/// <c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.
|
||||
/// </returns>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator ==.
|
||||
/// </summary>
|
||||
/// <param name="a">A.</param>
|
||||
/// <param name="b">The b.</param>
|
||||
/// <returns>
|
||||
/// The result of the operator.
|
||||
/// </returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Implements the operator !=.
|
||||
/// </summary>
|
||||
/// <param name="a">A.</param>
|
||||
/// <param name="b">The b.</param>
|
||||
/// <returns>
|
||||
/// The result of the operator.
|
||||
/// </returns>
|
||||
public static bool operator !=(PocketExploreItem a, PocketExploreItem b)
|
||||
{
|
||||
return !(a == b);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a hash code for this instance.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
|
||||
/// </returns>
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return ID.GetHashCode();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="System.String" /> that represents this instance.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A <see cref="System.String" /> that represents this instance.
|
||||
/// </returns>
|
||||
public override string ToString()
|
||||
{
|
||||
return ID;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user