最近买了个kindle,为了方便阅读,写了个程序抓取网页内容发送到Kindle

      主要觉得往kindle里加书籍太麻烦了,要下载下来,还要通过邮件发送,特别一些网页文字版的书籍没办法放到kindle里,所以想着还不如自己动手丰衣足食,写一个程序直接抓取网页内容,制作成书籍,然后自动发送到kindle里。

程序首先操作简单“一键推送”,还有就是书籍要带目录。

先做了个winform版的,有时间再编写个Web版的。

程序相关的技术:

1.使用NSoup分析网页

2.爬虫规则设计

3.多线程爬网页

4.生成txt文本和pdf文件

5.使用邮件推送到自己的kindle设备

 

程序界面效果:

 

生成的TXT文件

 

生成的pdf文件

 

 

BookRule.xml配置文件

复制代码
<BookSite Name="纵横小说网" Url="http://book.zongheng.com" charset="utf-8">
    <title>.read_con h1</title>
    <auther>.read_con a.fb</auther>
    <cover></cover>
    <introduction></introduction>
    <catalog>.read_con .chapterBean a</catalog>
    <content>#chapterContent</content>
  </BookSite>

<SearchSite Name="纵横网搜索" SearchUrl="http://search.zongheng.com/search/bookName/{0}/1.html" charset="utf-8" list=".search_text">
    <title>h2</title>
    <auther>.rela a</auther>
    <cover></cover>
    <introduction>.j_info</introduction>
    <bookurl eq="1">.search_oprate p span a</bookurl>
  </SearchSite>
复制代码

 

多线程爬网页参考:http://www.cnblogs.com/kakake/p/4151691.html 

 

生成pdf文件使用了itextsharp.dll组件

复制代码
public void SavePdf(string filename)
        {
            FileInfo fileinfo = new FileInfo(filename);
            if (fileinfo.Directory.Exists == false)
                Directory.CreateDirectory(fileinfo.DirectoryName);

            Document doc = new Document(PageSize.A5, 10, 10, 10, 10);
            PdfWriter.GetInstance(doc, new FileStream(filename, FileMode.Create));
            doc.Open();

            //指定字体库,并创建字体
            BaseFont baseFont = BaseFont.CreateFont(
                "C:\\WINDOWS\\FONTS\\SIMYOU.TTF",
                BaseFont.IDENTITY_H,
                BaseFont.NOT_EMBEDDED);
            iTextSharp.text.Font font1 = new iTextSharp.text.Font(baseFont, 18);
            iTextSharp.text.Font font2 = new iTextSharp.text.Font(baseFont, 20);

            Chapter chapter1 = new Chapter(title, 1);
            chapter1.Add(new Paragraph(title, font2));
            chapter1.Add(new Paragraph(auther, font1));
            chapter1.Add(new Paragraph(introduction, font1));
            for (int i = 0; i < catalogs.Count; i++)
            {
                Section section1 = chapter1.AddSection(catalogs[i].text);
                section1.Add(new Paragraph(catalogs[i].page.text, font1));
                section1.TriggerNewPage = true;
                section1.BookmarkOpen = false;
            }

            chapter1.BookmarkOpen = false;
            doc.Add(chapter1);
            doc.Close();
        }
复制代码

 

发送邮件使用的System.Net.Mail中微软封装好的MailMessage类,由于没有自己的邮件服务器,使用QQ邮箱推送到kindle非常不稳定,有时候可以有时候又发送失败。

复制代码
 /// <summary>
        /// 发送kindle的EMail
        /// </summary>
        /// <param name="toEmail">kindle的EMail地址</param>
        /// <param name="type">发送的文件 0:pdf;1:txt</param>
        /// <param name="filename">文件名</param>
        public void SendMail(string toEmail, int type, string filename)
        {
            //确定smtp服务器地址。实例化一个Smtp客户端
            System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("smtp.qq.com");
            //生成一个发送地址
            string strFrom = "****@qq.com";

            //构造一个发件人地址对象
            MailAddress from = new MailAddress(strFrom, "kakake", Encoding.UTF8);
            //构造一个收件人地址对象
            MailAddress to = new MailAddress(toEmail, "kindle", Encoding.UTF8);

            //构造一个Email的Message对象
            MailMessage message = new MailMessage(from, to);

            if (type == 0)
                SaveTxt(filename);
            else if (type == 1)
                SavePdf(filename);

            //得到文件名
            string fileName = filename;
            //判断文件是否存在
            if (File.Exists(fileName))
            {
                //构造一个附件对象
                Attachment attach = new Attachment(fileName);
                //得到文件的信息
                ContentDisposition disposition = attach.ContentDisposition;
                disposition.CreationDate = System.IO.File.GetCreationTime(fileName);
                disposition.ModificationDate = System.IO.File.GetLastWriteTime(fileName);
                disposition.ReadDate = System.IO.File.GetLastAccessTime(fileName);
                //向邮件添加附件
                message.Attachments.Add(attach);
            }
            else
            {
                throw new Exception("[" + fileName + "]文件没找到!");
            }

            //添加邮件主题和内容
            message.Subject = "书籍发送到kindle";
            message.SubjectEncoding = Encoding.UTF8;
            message.Body = "";
            message.BodyEncoding = Encoding.UTF8;

            //设置邮件的信息
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = false;

            //如果服务器支持安全连接,则将安全连接设为true。
            //gmail支持,163不支持,如果是gmail则一定要将其设为true
            client.EnableSsl = true;

            //设置用户名和密码。
            //string userState = message.Subject;
            client.UseDefaultCredentials = false;
            string username = "user";
            string passwd = "****";
            //用户登陆信息
            NetworkCredential myCredentials = new NetworkCredential(username, passwd);
            client.Credentials = myCredentials;
            //发送邮件
            client.Send(message);
        }
    }
复制代码

 

程序下载地址:http://pan.baidu.com/s/1dDndERB

 

posted @   kakake  阅读(4745)  评论(18编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示