From 6f305abe0c16cf3de72759661a007abf8f1316d7 Mon Sep 17 00:00:00 2001 From: Tobias Klika Date: Thu, 2 Apr 2015 22:50:40 +0200 Subject: [PATCH] complete ratings/reviews --- src/OnePeek.Api/AppRatingEndpoint.cs | 45 ++++++++++++++++++---------- src/OnePeek.Api/OnePeek.Api.csproj | 1 + src/OnePeek.Api/Utils.cs | 26 ++++++++++++++++ src/OnePeek.Entities/AppReviews.cs | 29 ++++++++++-------- src/OnePeek.WebConsole/ApiModule.cs | 2 ++ 5 files changed, 76 insertions(+), 27 deletions(-) create mode 100644 src/OnePeek.Api/Utils.cs diff --git a/src/OnePeek.Api/AppRatingEndpoint.cs b/src/OnePeek.Api/AppRatingEndpoint.cs index ad292dc..de807d6 100644 --- a/src/OnePeek.Api/AppRatingEndpoint.cs +++ b/src/OnePeek.Api/AppRatingEndpoint.cs @@ -7,6 +7,7 @@ using System.IO; using System.Threading.Tasks; using System.Xml.Linq; using System.Linq; +using System.Collections.Generic; namespace OnePeek.Api { @@ -23,23 +24,37 @@ namespace OnePeek.Api EndpointUris.GetWindowsPhoneReviewsUri(appId, storeCulture.ToString(), sorting.ToString()) ); - try - { - //string entryXml = XDocument.Parse(xml).Descendants().FirstOrDefault(x => x.Name.LocalName == "entry").ToString(); + IEnumerable xel = XDocument.Parse(xml).Elements().First().Descendants(); - AppReviews result = Deserialize.Xml(xml); - result.Id = appId; - result.StoreType = store; - result.StoreCultureType = storeCulture; - result.Sorting = sorting; - return result; - } - catch (Exception exc) - { - Debug.WriteLine(exc.Message); - } + AppReviews result = Deserialize.Xml(xml); + result.Id = appId; + result.StoreType = store; + result.StoreCultureType = storeCulture; + result.Sorting = sorting; - return null; + // parse markers + result.PrevPageMarkerId = Utils.GetQueryPart(xel.FirstOrDefault(x => x.Name.LocalName == "link" && x.Attribute("rel").Value == "prev"), "href", "beforeMarker"); + result.NextPageMarkerId = Utils.GetQueryPart(xel.FirstOrDefault(x => x.Name.LocalName == "link" && x.Attribute("rel").Value == "next"), "href", "afterMarker"); + + // create images + result.Reviews = xel.Where(x => x.Name.LocalName == "entry").Select(x => + { + IEnumerable childs = x.Descendants(); + byte rating = (byte)childs.GetFloat("userRating"); + + return new AppReview() + { + Id = childs.Get("reviewId"), + CreatedDate = DateTime.Parse(childs.Get("updated")), + Author = childs.FirstOrDefault(c => c.Name.LocalName == "author").Descendants().Get("name"), + Text = childs.Get("content"), + Rating = Configuration.UseFiveStarSystem ? (byte)(rating * 0.5) : rating, + Device = childs.Get("device"), + AppVersion = childs.Get("productVersion") + }; + }); + + return result; } } } \ No newline at end of file diff --git a/src/OnePeek.Api/OnePeek.Api.csproj b/src/OnePeek.Api/OnePeek.Api.csproj index d3b25ec..4a10474 100644 --- a/src/OnePeek.Api/OnePeek.Api.csproj +++ b/src/OnePeek.Api/OnePeek.Api.csproj @@ -50,6 +50,7 @@ + diff --git a/src/OnePeek.Api/Utils.cs b/src/OnePeek.Api/Utils.cs new file mode 100644 index 0000000..4d42158 --- /dev/null +++ b/src/OnePeek.Api/Utils.cs @@ -0,0 +1,26 @@ +using System.Text.RegularExpressions; +using System.Xml.Linq; + +namespace OnePeek.Api +{ + internal static class Utils + { + public static string GetQueryPart(XElement element, string attribute, string queryKey) + { + if (element == null) + { + return null; + } + + XAttribute attr = element.Attribute(attribute); + + if (attr == null) + { + return null; + } + + Match match = new Regex(queryKey + @"=([A-Za-z0-9\-=]+)").Match(attr.Value); + return match.Success ? match.Groups[1].Value + "=" : null; + } + } +} diff --git a/src/OnePeek.Entities/AppReviews.cs b/src/OnePeek.Entities/AppReviews.cs index 9e7a36d..67c172e 100644 --- a/src/OnePeek.Entities/AppReviews.cs +++ b/src/OnePeek.Entities/AppReviews.cs @@ -1,8 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Xml.Serialization; namespace OnePeek.Entities @@ -25,24 +22,32 @@ namespace OnePeek.Entities public string NextPageMarkerId { get; set; } - public List Reviews { get; set; } + public IEnumerable Reviews { get; set; } } + [XmlRoot("entry")] public partial class AppReview { - public DateTime CreatedDate { get; set; } - - public string Author { get; set; } - - public string Text { get; set; } - - public float Rating { get; set; } - + [XmlElement("reviewId")] public string Id { get; set; } + [XmlElement("updated")] + public DateTime CreatedDate { get; set; } + + [XmlElement("name")] + public string Author { get; set; } + + [XmlElement("content")] + public string Text { get; set; } + + [XmlElement("userRating")] + public byte Rating { get; set; } + + [XmlElement("device")] public string Device { get; set; } + [XmlElement("productVersion")] public string AppVersion { get; set; } } } diff --git a/src/OnePeek.WebConsole/ApiModule.cs b/src/OnePeek.WebConsole/ApiModule.cs index 3fccc74..ce5c82d 100644 --- a/src/OnePeek.WebConsole/ApiModule.cs +++ b/src/OnePeek.WebConsole/ApiModule.cs @@ -1,4 +1,5 @@ using Nancy; +using Nancy.Responses; using OnePeek.Api; using OnePeek.Entities; using System.IO; @@ -17,6 +18,7 @@ namespace OnePeek.WebConsole { AppMetadata meta = await metaEndpoint.GetMetadata(ctx.id, StoreType.WindowsPhone8, StoreCultureType.EN_US); return Response.AsJson(meta); + //return new JsonResponse(ctx) };