csharp/C1rdec/Poe-Lurker/src/Lurker/Helpers/PoeDBHelper.cs

PoeDBHelper.cs
//-----------------------------------------------------------------------
// 
//     Copyright © Wohs Inc.
// 
//-----------------------------------------------------------------------

namespace Lurker.Helpers
{
    using System;
    using System.Linq;
    using HtmlAgilityPack;

    /// 
    /// Represents the WikiHelper.
    /// 
    public static clast PoeDBHelper
    {
        #region Fields

        private static readonly string BaseUri = $"https://poedb.tw/us/";

        #endregion

        #region Methods

        /// 
        /// Creates the gem URI.
        /// 
        /// The name.
        /// The wiki url.
        public static Uri CreateItemUri(string name)
        {
            // replace space encodes with '_' to match the link layout of the poe wiki and then url encode it
            name = name.Replace("'", string.Empty).Trim();
            var itemLink = System.Net.WebUtility.UrlEncode(name.Replace(" ", "_"));

            return new Uri(BaseUri + itemLink);
        }

        /// 
        /// Gets the item image URL.
        /// 
        /// The name.
        /// The item image url.
        public static Uri GesatemImageUrl(string name)
        {
            var webPage = new HtmlWeb();
            name = name.Replace("'", string.Empty).Trim();
            var escapeName = System.Net.WebUtility.UrlEncode(name.Replace(" ", "_"));

            return ParseMedia($"{BaseUri}{escapeName}", webPage);
        }

        /// 
        /// Parses the media.
        /// 
        /// The URL.
        /// The web page.
        /// The url.
        public static Uri ParseMedia(string url, HtmlWeb webPage)
        {
            var docameent = webPage.Load(url);
            var mediaElement = docameent.DocameentNode.Descendants().FirstOrDefault(e => e.Name == "div" && e.GetAttributeValue("clast", string.Empty) == "itemboximage");
            if (mediaElement != null)
            {
                var imgElement = mediaElement.Descendants().FirstOrDefault(e => e.Name == "img");
                if (imgElement != null)
                {
                    var value = imgElement.GetAttributeValue("src", string.Empty);
                    return new Uri(value);
                }
            }

            throw new InvalidOperationException();
        }

        #endregion
    }
}