Пример #1
0
        private async Task DownloadArticals(XmlDocument doc)
        {
            int index = 0;

            using (HttpClient client = new HttpClient())
            {
                foreach (XmlNode node in doc["rss"]["channel"].ChildNodes)
                {
                    if (node.Name == "item" && index < 10)
                    {
                        index++;
                        string url         = node["link"].InnerText;
                        string title       = node["title"].InnerText;
                        string category    = node["category"].InnerText;
                        string description = node["description"].InnerText;
                        if (string.IsNullOrWhiteSpace(description))
                        {
                            description = title;
                        }
                        string localHref = string.Format("BM/{0}.html", index);
                        string path      = string.Format("./KindleBook/{0}", localHref);

                        try
                        {
                            HttpResponseMessage response = await client.GetAsync(url).ConfigureAwait(false);

                            using (FileStream fileStream = new FileStream(path, FileMode.Create))
                            {
                                StreamWriter writer   = new StreamWriter(fileStream);
                                var          document = await ModifyArticle(response.Content);

                                document.ToHtml(writer, HtmlMarkupFormatter.Instance);
                                writer.Flush();

                                OPFItem item = new OPFItem();
                                item.Href      = localHref;
                                item.Id        = index.ToString();
                                item.MediaType = "application/xhtml+xml";
                                opf.Items.Add(item);

                                OPFItemRef itemRef = new OPFItemRef();
                                itemRef.IdRef = index.ToString();
                                opf.Spine.Items.Add(itemRef);

                                NavPoint articalNav = new NavPoint();
                                articalNav.Id          = index.ToString();
                                articalNav.Label.Text  = title;
                                articalNav.NavClass    = "artical";
                                articalNav.Content.Src = localHref;
                                articalNav.MBP         = new List <NCXMBP>();
                                articalNav.MBP.Add(new NCXMBP("name", title));
                                articalNav.MBP.Add(new NCXMBP("description", description));

                                ncx.NavMap.Add(articalNav);

                                await Task.Delay(5000);
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Ignore the exception and continue. " + e.Message);
                        }
                    }
                }
            }
        }
Пример #2
0
        public void SerializeNCX()
        {
            NCX ncx = new NCX();

            ncx.Version = "text version";

            List <NCXMeta> metas = new List <NCXMeta>();
            NCXMeta        meta  = new NCXMeta();

            meta.Content = "test content";
            meta.Name    = "test name";
            metas.Add(meta);
            metas.Add(meta);

            ncx.Head = metas;

            NCXText title = new NCXText("test");

            title.Text = "test title";

            ncx.Title = title;

            List <NavPoint> navMap = new List <NavPoint>();
            NavPoint        root   = new NavPoint();

            root.Content     = new NavContent();
            root.Content.Src = "test src";

            root.Id         = "testId";
            root.Label      = new NCXText("label");
            root.Label.Text = "test label";

            NavPoint p2 = new NavPoint();

            p2.Content     = new NavContent();
            p2.Content.Src = "test src";

            p2.Id         = "testId";
            p2.Label      = new NCXText("llll");
            p2.Label.Text = "test label";

            p2.MBP = new List <NCXMBP>();
            NCXMBP mbpname = new NCXMBP();

            mbpname.Name = "author";
            mbpname.Text = "test author";
            NCXMBP mbpdesc = new NCXMBP();

            mbpdesc.Name = "description";
            mbpdesc.Text = "this is a test description";

            p2.MBP.Add(mbpname);
            p2.MBP.Add(mbpdesc);

            root.Items = new List <NavPoint>();
            root.Items.Add(p2);
            root.Items.Add(p2);

            navMap.Add(root);

            ncx.NavMap = navMap;

            using (FileStream fileStream = new FileStream("./KindleBook/bm.ncx", FileMode.Create))
            {
                XmlSerializer           serializer = new XmlSerializer(typeof(NCX));
                XmlSerializerNamespaces ns         = new XmlSerializerNamespaces();
                ns.Add("mbp", "http://mbp.mock");
                serializer.Serialize(fileStream, ncx, ns);
            }
        }