主要以爲往kindle里加書籍太麻煩了,要下載下來,還要經過郵件發送,特別一些網頁文字版的書籍沒辦法放到kindle裏,因此想着還不如本身動手豐衣足食,寫一個程序直接抓取網頁內容,製做成書籍,而後自動發送到kindle裏。html
程序首先操做簡單「一鍵推送」,還有就是書籍要帶目錄。安全
先作了個winform版的,有時間再編寫個Web版的。服務器
程序相關的技術:多線程
1.使用NSoup分析網頁字體
2.爬蟲規則設計url
3.多線程爬網頁spa
4.生成txt文本和pdf文件線程
5.使用郵件推送到本身的kindle設備設計
程序界面效果:3d
生成的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