public IEnumerable <AltemaCharacterInfo> GetAltemaCharacterInfos()
        {
            _logger.LogInformation($"Logic Method invoked: {nameof(GetAltemaCharacterInfos)}");

            string cacheKey = nameof(GetAltemaCharacterInfos);
            IEnumerable <AltemaCharacterInfo> results = _cacheProvider.ObjectGet <IList <AltemaCharacterInfo> >(cacheKey);

            if (results == null)
            {
                IList <AltemaCharacterInfo> altemaCharacterInfos = new List <AltemaCharacterInfo>();

                //Stream
                //Stream altemaCharacterRatingStream = _altemaCharacterRatingRepository.GetAltemaCharacterRatingStream();
                ////don't fail if we can't contact the altema page
                //if (altemaCharacterRatingStream == null) { return altemaCharacterInfos; }

                //HtmlDocument htmlDoc = GetLoadedHtmlDocument(altemaCharacterRatingStream);

                //String
                string altemaCharacterRatingString = _altemaCharacterRatingRepository.GetAltemaCharacterRatingString();

                //don't fail if we can't contact the altema page
                if (altemaCharacterRatingString == null)
                {
                    return(altemaCharacterInfos);
                }

                HtmlDocument htmlDoc = GetLoadedHtmlDocument(altemaCharacterRatingString);

                IEnumerable <HtmlNode> characterNodes = GetCharacterNodes(htmlDoc);

                int altemaOrder = 1;
                foreach (HtmlNode node in characterNodes)
                {
                    AltemaCharacterNodeComponents characterNodeComponents = _altemaCharacterNodeParser.ParseCharacterNode(node);
                    AltemaCharacterInfo           characterInfo           = _altemaCharacterNodeInterpreter.ConvertCharacterNodeToCharacterInfo(characterNodeComponents);

                    if (characterInfo != null)
                    {
                        characterInfo.AltemaOrder = altemaOrder++;
                        altemaCharacterInfos.Add(characterInfo);
                    }
                }

                results = altemaCharacterInfos;

                _cacheProvider.ObjectSet(cacheKey, results);
            }

            return(results);
        }
Exemple #2
0
        public AltemaCharacterNodeComponents ParseCharacterNode(HtmlNode characterNode)
        {
            AltemaCharacterNodeComponents components = new AltemaCharacterNodeComponents();

            components.CharacterNode = characterNode;
            components.NameNode      = characterNode.SelectSingleNode(@"./td[1]/a[1]");
            components.ImageNode     = characterNode.SelectSingleNode(@"./td[1]/a[1]/img[1]");
            components.RoleNode      = characterNode.SelectSingleNode(@"./td[2]/span[@class='b']");
            components.RatingNode    = characterNode.SelectSingleNode(@"./td[3]/span[@class='redtxt']");

            components.CharacterIdAttribute     = components.NameNode.Attributes.FirstOrDefault();
            components.ImageLazyLoadedAttribute = components.ImageNode?.Attributes.FirstOrDefault(a => a.Name == ImageLazyLoadedAttributeName);
            components.ImageLazySourceAttribute = components.ImageNode?.Attributes.FirstOrDefault(a => a.Name == ImageLazySourceAttributeName);
            components.ImageSourceAttribute     = components.ImageNode?.Attributes.FirstOrDefault(a => a.Name == ImageSourceAttributeName);

            return(components);
        }
Exemple #3
0
        private string GetImageUrl(AltemaCharacterNodeComponents characterNodeComponents)
        {
            string imageUrl = String.Empty;

            if (characterNodeComponents.IsRatedCharacter)
            {
                if (characterNodeComponents.ImageLazyLoadedAttribute != null && characterNodeComponents.ImageLazyLoadedAttribute.Value == "true")
                {
                    imageUrl = characterNodeComponents.ImageSourceAttribute.Value;
                }
                else
                {
                    imageUrl = characterNodeComponents.ImageLazySourceAttribute.Value;
                }
            }


            return(imageUrl);
        }
Exemple #4
0
        public AltemaCharacterInfo ConvertCharacterNodeToCharacterInfo(AltemaCharacterNodeComponents characterNodeComponents)
        {
            AltemaCharacterInfo characterInfo = null;


            if (characterNodeComponents != null && characterNodeComponents.IsRatedCharacter) //IsRatedCharacter makes sure important fields are not null
            {
                characterInfo = new AltemaCharacterInfo()
                {
                    Id                  = characterNodeComponents.CharacterIdAttribute.Value,
                    ImageUrl            = GetImageUrl(characterNodeComponents),
                    JapaneseName        = characterNodeComponents.NameNode.InnerText.Trim(),
                    Rating              = System.Convert.ToInt32(characterNodeComponents.RatingNode.InnerText.Trim()),
                    JapaneseRoleSummary = characterNodeComponents.RoleNode.InnerText.Trim(),
                    Name                = _altemaCharacterJapaneseTextMapper.GetCharacterNameFromId(characterNodeComponents.CharacterIdAttribute.Value) ?? characterNodeComponents.NameNode.InnerText.Trim(),
                    RoleSummary         = _altemaCharacterJapaneseTextMapper.GetRoleSummaryFromJapaneseRoleSummary(characterNodeComponents.RoleNode.InnerText.Trim())
                };

                characterInfo.Roles = _altemaCharacterJapaneseTextMapper.GetRolesFromJapaneseRoleSummary(characterInfo.JapaneseRoleSummary);
            }

            return(characterInfo);
        }