コード例 #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 SerializeOPF()
        {
            OPF opf = new OPF();

            opf.ID = "tst";
            DCMetadata dc = new DCMetadata();

            dc.Title       = "BM";
            dc.Creator     = "Marty";
            dc.DateTime    = DateTime.Now;
            dc.Description = "test";
            dc.Publisher   = "marty";
            dc.Subject     = "bm";

            XMetadata x       = new XMetadata();
            XOutput   xoutput = new XOutput();

            xoutput.ContentType = "application/x-mobipocket-subscription-magazine";
            xoutput.Encoding    = "utf-8";
            x.Output            = xoutput;

            OPFMetadata opfMetadata = new OPFMetadata();

            opfMetadata.DC = dc;
            opfMetadata.X  = x;
            opf.Metadata   = opfMetadata;

            OPFManifest manifest = new OPFManifest();

            manifest.Items = new List <OPFItem>();
            OPFItem item1 = new OPFItem();

            item1.Href      = "test";
            item1.Id        = "fsakl";
            item1.MediaType = "application/xhtml+xml";
            manifest.Items.Add(item1);
            manifest.Items.Add(item1);

            opf.Items = manifest.Items;

            OPFReference opfref = new OPFReference();

            opfref.Href    = "test";
            opfref.RefType = "type";
            opfref.Title   = "table of contents";

            List <OPFReference> guide = new List <OPFReference>();

            guide.Add(opfref);
            guide.Add(opfref);

            //opf.RefItems = guide;

            OPFSpine spine = new OPFSpine();

            spine.Items = new List <OPFItemRef>();
            OPFItemRef itemRef = new OPFItemRef();

            itemRef.IdRef = "tstes";
            spine.Items.Add(itemRef);
            spine.Items.Add(itemRef);

            //spine.Add(itemRef);
            //spine.Add(itemRef);
            spine.Toc = "test";

            opf.Spine = spine;

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