public async Task <List <TopAnimeData> > GetTopAnimeData(bool force = false)
        {
            if (!force)
            {
                if (_prevQueriesCache.ContainsKey(_type))
                {
                    return(_prevQueriesCache[_type]);
                }
            }

            var output = force ? new List <TopAnimeData>() : (await DataCache.RetrieveTopAnimeData(_type) ?? new List <TopAnimeData>());

            if (output.Count > 0)
            {
                return(output);
            }
            var raw = await GetRequestResponse();

            if (string.IsNullOrEmpty(raw))
            {
                return(new List <TopAnimeData>());
            }


            var doc = new HtmlDocument();

            doc.LoadHtml(raw);
            var topNodes = doc.DocumentNode.Descendants("table").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs["#Top:mainNode:class"]);
            var i        = 0;

            foreach (var item in topNodes.Descendants("tr").Where(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs["#Top:topNode:class"]))
            {
                try
                {
                    var current = new TopAnimeData();
                    var epsText = item.Descendants("div").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs["#Top:topNode:eps:class"]).ChildNodes[0].InnerText;
                    epsText          = epsText.Substring(epsText.IndexOf('(') + 1);
                    epsText          = epsText.Substring(0, epsText.IndexOf(' '));
                    current.Episodes = epsText;
                    var img = item.Descendants("img").First().Attributes["data-src"].Value;
                    var pos = img.LastIndexOf('t');
                    // we want to remove last "t" from url as this is much smaller image than we would want
                    current.ImgUrl = pos != -1 ? img.Remove(pos, 1) : img;
                    var titleNode = item.Descendants("a").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs[_type != TopAnimeType.Manga  ? "#Top:topNode:titleNode:class" : "#Top:topMangaNode:titleNode:class"]);
                    current.Title = WebUtility.HtmlDecode(titleNode.InnerText).Trim();
                    current.Id    = Convert.ToInt32(titleNode.Attributes["href"].Value.Substring(7).Split('/')[2]);
                    try
                    {
                        current.Score = float.Parse(item.Descendants("span").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs["#Top:topNode:score:class"]).InnerText.Trim());
                    }
                    catch (Exception)
                    {
                        current.Score = 0; //sometimes score in unavailable -> upcoming for example
                    }

                    current.Index = ++i;


                    output.Add(current);
                }
                catch (Exception)
                {
                    //
                }
            }
            DataCache.SaveTopAnimeData(output, _type);
            _prevQueriesCache[_type] = output;
            return(output);
        }
Example #2
0
        public async Task <List <TopAnimeData> > GetTopAnimeData(bool force = false)
        {
            if (!force)
            {
                if (_prevQueriesCache.ContainsKey(_type))
                {
                    return(_prevQueriesCache[_type]);
                }
            }

            var output = force ? new List <TopAnimeData>() : (await DataCache.RetrieveTopAnimeData(_type) ?? new List <TopAnimeData>());

            if (output.Count > 0)
            {
                _prevQueriesCache[_type] = output;
                return(output);
            }
            var raw = await GetRequestResponse();

            if (string.IsNullOrEmpty(raw))
            {
                return(new List <TopAnimeData>());
            }

            var doc = new HtmlDocument();

            doc.LoadHtml(raw);
            var    topNodes   = doc.DocumentNode.Descendants("table").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == "top-ranking-table");
            var    i          = 50 * _page;
            string imgUrlType = _type == TopAnimeType.Manga ? "manga/" : "anime/";

            foreach (var item in topNodes.Descendants("tr").Where(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == "ranking-list"))
            {
                try
                {
                    var current = new TopAnimeData();
                    var epsText = item.Descendants("div").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == "information di-ib mt4").ChildNodes[0].InnerText;
                    epsText          = epsText.Substring(epsText.IndexOf('(') + 1);
                    epsText          = epsText.Substring(0, epsText.IndexOf(' '));
                    current.Episodes = epsText;
                    //var img = item.Descendants("img").First().Attributes["data-src"].Value.Split('/');
                    var img = item.Descendants("img").First().Attributes["data-srcset"].Value;
                    img = img.Split(',').Last();
                    img = img.Substring(0, img.Length - 3);
                    var imgParts = img.Split('/');
                    int imgCount = imgParts.Length;
                    var imgurl   = imgParts[imgCount - 2] + "/" + imgParts[imgCount - 1];
                    var pos      = imgurl.IndexOf('?');
                    if (pos != -1)
                    {
                        imgurl = imgurl.Substring(0, pos);
                    }
                    current.ImgUrl = "https://myanimelist.cdn-dena.com/images/" + imgUrlType + imgurl;
                    var titleNode = item.Descendants("a").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == (_type != TopAnimeType.Manga  ? "hoverinfo_trigger fl-l fs14 fw-b" : "hoverinfo_trigger fs14 fw-b"));
                    current.Title = WebUtility.HtmlDecode(titleNode.InnerText).Trim();
                    current.Id    = Convert.ToInt32(titleNode.Attributes["href"].Value.Substring(8).Split('/')[2]);
                    try
                    {
                        current.Score = float.Parse(item.Descendants("span").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == "text on").InnerText.Trim());
                    }
                    catch (Exception)
                    {
                        current.Score = 0; //sometimes score in unavailable -> upcoming for example
                    }

                    current.Index = ++i;


                    output.Add(current);
                }
                catch (Exception)
                {
                    //
                }
            }
            if (_page != 0) //merge data
            {
                output = _prevQueriesCache[_type].Union(output).Distinct().ToList();
            }

            DataCache.SaveTopAnimeData(output, _type);
            _prevQueriesCache[_type] = output;
            return(output);
        }
Example #3
0
        public async Task<List<TopAnimeData>> GetTopAnimeData(bool force = false)
        {
            if (!force)
                if (_prevQueriesCache.ContainsKey(_type))
                    return _prevQueriesCache[_type];

            var output = force ? new List<TopAnimeData>() : (await DataCache.RetrieveTopAnimeData(_type) ?? new List<TopAnimeData>());
            if (output.Count > 0)
            {
                _prevQueriesCache[_type] = output;
                return output;
            }
            var raw = await GetRequestResponse();
            if (string.IsNullOrEmpty(raw))
                return new List<TopAnimeData>();

            var doc = new HtmlDocument();
            doc.LoadHtml(raw);
            var topNodes = doc.DocumentNode.Descendants("table").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs["#Top:mainNode:class"]);
            var i = 50*_page;
            string imgUrlType = _type == TopAnimeType.Manga ? "manga/" : "anime/";
            foreach (var item in topNodes.Descendants("tr").Where(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs["#Top:topNode:class"]))
            {
                try
                {
                    var current = new TopAnimeData();
                    var epsText = item.Descendants("div").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs["#Top:topNode:eps:class"]).ChildNodes[0].InnerText;
                    epsText = epsText.Substring(epsText.IndexOf('(') + 1);
                    epsText = epsText.Substring(0, epsText.IndexOf(' '));
                    current.Episodes = epsText;
                    //var img = item.Descendants("img").First().Attributes["data-src"].Value.Split('/');
                    var img = item.Descendants("img").First().Attributes["srcset"].Value;
                    img = img.Split(',').Last();
                    img = img.Substring(0, img.Length - 3);
                    var imgParts = img.Split('/');
                    int imgCount = imgParts.Length;
                    var imgurl = imgParts[imgCount - 2] + "/" + imgParts[imgCount - 1];
                    var pos = imgurl.IndexOf('?');
                    if (pos != -1)
                        imgurl = imgurl.Substring(0, pos);
                    current.ImgUrl = "https://myanimelist.cdn-dena.com/images/" + imgUrlType + imgurl;
                    var titleNode = item.Descendants("a").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs[_type != TopAnimeType.Manga  ? "#Top:topNode:titleNode:class" : "#Top:topMangaNode:titleNode:class"]);
                    current.Title = WebUtility.HtmlDecode(titleNode.InnerText).Trim();
                    current.Id = Convert.ToInt32(titleNode.Attributes["href"].Value.Substring(8).Split('/')[2]);
                    try
                    {
                        current.Score = float.Parse(item.Descendants("span").First(node => node.Attributes.Contains("class") && node.Attributes["class"].Value == HtmlClassMgr.ClassDefs["#Top:topNode:score:class"]).InnerText.Trim());
                    }
                    catch (Exception)
                    {
                        current.Score = 0; //sometimes score in unavailable -> upcoming for example
                    }
                    
                    current.Index = ++i;


                    output.Add(current);
                }
                catch (Exception)
                {
                    //
                }
            }
            if (_page != 0) //merge data
                output = _prevQueriesCache[_type].Union(output).Distinct().ToList();

            DataCache.SaveTopAnimeData(output, _type);
            _prevQueriesCache[_type] = output;
            return output;
        }