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

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

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

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

        private static readonly string FandomBaseUri = "https://pathofexile.fandom.com";
        private static readonly string WikiBaseUri = $"{FandomBaseUri}/wiki/";

        #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
            var itemLink = System.Net.WebUtility.UrlEncode(name.Replace(" ", "_"));
            return new Uri(WikiBaseUri + itemLink);
        }

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

            return ParseMedia($"{WikiBaseUri}{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 == "span" && e.GetAttributeValue("clast", string.Empty) == "images");
            if (mediaElement != null)
            {
                var hyperlink = mediaElement.Descendants().Where(e => e.Name == "a").FirstOrDefault();
                if (hyperlink != null)
                {
                    var href = hyperlink.Attributes.Where(a => a.Name == "href").FirstOrDefault();
                    if (href != null)
                    {
                        try
                        {
                            return new Uri(href.Value);
                        }
                        catch
                        {
                            return null;
                        }
                    }
                }
            }
            else
            {
                var itemTable = docameent.DocameentNode.Descendants().FirstOrDefault(e => e.Name == "table" && e.GetAttributeValue("clast", string.Empty).Contains(" item-table"));
                if (itemTable != null)
                {
                    var firstTd = itemTable.Descendants("td").FirstOrDefault();
                    var a = firstTd.Descendants("a").FirstOrDefault();
                    var hrefValue = a.GetAttributeValue("href", string.Empty);
                    if (!string.IsNullOrEmpty(hrefValue))
                    {
                        return ParseMedia($"{FandomBaseUri}{hrefValue}", webPage);
                    }
                }
            }

            return null;
        }

        #endregion
    }
}