コード例 #1
0
        private IComparable FindDataFromXML(SyncTemplateItem sti, XmlNode namedChild)
        {
            string content = namedChild.InnerText;

            switch (sti.Type)
            {
            case FieldType.BASIC:
                return(content);

            case FieldType.DATE:
                DateTime dateTime;
                bool     success = DateTime.TryParseExact(content, "yyyy-MM-dd",
                                                          System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out dateTime);
                if (success)
                {
                    return(new XDate(dateTime));
                }
                else
                {
                    int?year  = ParseNullable(content.Substring(0, 4), "0000");
                    int?month = ParseNullable(content.Substring(5, 2), "00");
                    int?day   = ParseNullable(content.Substring(8, 2), "00");
                    return(new XDate(year, month, day, 0));
                }

            case FieldType.ENUM:
                EnumMetadata data = sti.Metadata as EnumMetadata;
                for (int j = 0; j < data.Entries.Length; j++)
                {
                    if (data.Entries[j].Equals(content))
                    {
                        return(j);
                    }
                }
                int Itry = 0;
                success = int.TryParse(content, out Itry);
                return(success ? Itry : 0);

            case FieldType.IMAGE:
                return(new XImage(content, true));

            case FieldType.NUMBER:
                return(int.Parse(content));

            case FieldType.DECIMAL:
                return(float.Parse(content));

            default:
                throw new NotImplementedException();
            }
        }
コード例 #2
0
 private IComparable FindDataFromHTML(HtmlDocument htmlDoc, SyncTemplateItem sti)
 {
     //IEnumerable<HtmlNode> sideBarNodes = htmlDoc.DocumentNode.Descendants().
     //	Where(n => n.Attributes.Contains("class") && n.Attributes["class"].Value.Contains("js-scrollfix-bottom"));
     //foreach(HtmlNode n in sideBarNodes) {
     //	Console.WriteLine(n.InnerText);
     //}
     if (sti.Id.Equals("pub_rating"))
     {
         return(float.Parse(FindNodesWithAttribute(htmlDoc, "itemprop", "ratingValue").FirstOrDefault().InnerHtml));
     }
     else if (sti.Id.Equals("pub_rater_count"))
     {
         return(int.Parse(FindNodesWithAttribute(htmlDoc, "itemprop", "ratingCount").FirstOrDefault().InnerHtml,
                          System.Globalization.NumberStyles.AllowThousands));
     }
     throw new InvalidOperationException();
     //TODO
 }
コード例 #3
0
        private void FillItem(SyncItem si, Dictionary <string, FieldTemplateItem> template, XmlNode animeNode)
        {
            //Console.WriteLine("anime node id: " + si.Id);
            HtmlDocument htmlDoc = new HtmlDocument();

            System.Diagnostics.Stopwatch s = System.Diagnostics.Stopwatch.StartNew();
            string htmlStr = webClient.DownloadString("http://myanimelist.net/anime/" + si.Id + "/");

            htmlDoc.LoadHtml(htmlStr);
            foreach (string fieldName in template.Keys)
            {
                FieldTemplateItem fti = template[fieldName];
                if (fti is SyncTemplateItem)
                {
                    SyncTemplateItem sti = fti as SyncTemplateItem;
                    si[fieldName].Value = sti.ParamList[0].Equals("XML") ?
                                          FindDataFromXML(sti, animeNode.FindChild(sti.Id)) : FindDataFromHTML(htmlDoc, sti);
                }
            }
            //Console.WriteLine("time: " + s.ElapsedMilliseconds);
        }