Bot.Plugins.Base.Commands.NowPlaying.FetchNowPlayingInfo C# (CSharp) Method

FetchNowPlayingInfo() protected method

Fetches and parses the Now Playing information from http://last.fm/user/
protected FetchNowPlayingInfo ( string lastfmUsername ) : string
lastfmUsername string Last.fm username to fetch from
return string
        protected string FetchNowPlayingInfo(string lastfmUsername)
        {
            string html;

            try
            {
                html = HttpHelper.GetFromUrl("http://last.fm/user/" + lastfmUsername);
            }
            catch (Exception e)
            {
                log.Warn("Could not get last.fm user page HTML.", e);
                throw;
            }

            var doc = new HtmlDocument();
            doc.LoadHtml(html);

            HtmlNode subjectNode = doc.DocumentNode.SelectSingleNode("//table [@id = 'recentTracks']/descendant::td [contains(@class, 'subjectCell') and contains(@class, 'highlight')]");

            if (subjectNode == null || string.IsNullOrWhiteSpace(subjectNode.InnerText))
            {
                log.Warn("Could not find Now Playing information in last.fm user page HTML.");
                throw new NullReferenceException();
            }

            string message = "np: " + subjectNode.InnerText.Trim();
            message = WebUtility.HtmlDecode(message);

            string[] trackInfo = subjectNode.InnerText.Trim().Split('–');
            html = HttpHelper.GetFromUrl("http://last.fm/music/" + trackInfo[0].Trim().Replace(' ', '+'));

            doc.LoadHtml(html);

            HtmlNode tagsNode = doc.DocumentNode.SelectSingleNode("//section [@class = 'global-tags']/ul/li/a");

            if (tagsNode != null && !string.IsNullOrWhiteSpace(tagsNode.InnerText))
            {
                message += " [" + tagsNode.InnerText.Trim() + "]";
            }

            return message;
        }