Recently bought a Kindle, in order to facilitate reading, wrote a program crawl Web content sent to the Kindle

Source: Internet
Author: User

The main idea to add books to the Kindle is too troublesome, to download down, but also to send by mail, especially some pages text version of the book can not be put on the Kindle, so think about it is not as good as their hands and clothing, write a program to crawl Web content directly, make books, and then automatically sent to the Kindle.

The program first operation Simple "one-click Push", there is the book to bring a directory.

First made a version of the WinForm, there is time to write a web version.

Program-related technologies:

1. Analyzing Web pages with Nsoup

2. Crawler Rule Design

3. Multi-threaded crawling web pages

4. Generate txt text and PDF files

5. Use mail to push to your Kindle device

Program Interface Effect:

Generated TXT file

The resulting PDF file

Bookrule.xml configuration file

<BooksiteName= "Vertical Fiction net"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><SearchSiteName= "Crossbar Search"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>    <Bookurleq= "1">. search_oprate P Span A</Bookurl>  </SearchSite>

Multi-threaded crawling Web reference: http://www.cnblogs.com/kakake/p/4151691.html

Generating PDF files using the Itextsharp.dll component

 Public voidSavepdf (stringfilename) {FileInfo FileInfo=NewFileInfo (filename); if(FileInfo. Directory.Exists = =false) Directory.CreateDirectory (FileInfo.            DirectoryName); Document Doc=NewDocument (PAGESIZE.A5,Ten,Ten,Ten,Ten); Pdfwriter.getinstance (Doc,NewFileStream (filename, filemode.create)); Doc.            Open (); //Specify a font library and create fontsBasefont Basefont =Basefont.createfont ("c:\\windows\\fonts\\simyou. TTF", Basefont.identity_h, basefont.not_embedded); ITextSharp.text.Font Font1=NewITextSharp.text.Font (Basefont, -); ITextSharp.text.Font Font2=NewITextSharp.text.Font (Basefont, -); Chapter Chapter1=NewChapter (title,1); Chapter1. ADD (NewParagraph (title, Font2)); Chapter1. ADD (NewParagraph (Auther, font1)); Chapter1. ADD (NewParagraph (Introduction, font1));  for(inti =0; I < catalogs. Count; i++) {section Section1=Chapter1.                AddSection (Catalogs[i].text); Section1. ADD (NewParagraph (Catalogs[i].page.text, font1)); Section1. Triggernewpage=true; Section1. Bookmarkopen=false; } chapter1. Bookmarkopen=false; Doc.            ADD (Chapter1); Doc.        Close (); }

Send mail use System.Net.Mail in Microsoft packaged MailMessage class, because there is no own mail server, using QQ mailbox push to kindle very unstable, sometimes can sometimes send failed.

 /// <summary>        ///Send your Kindle email/// </summary>        /// <param name= "Toemail" >Kindle's email address</param>        /// <param name= "type" >sent files 0:pdf;1:txt</param>        /// <param name= "filename" >file name</param>         Public voidSendMail (stringToemail,intTypestringfilename) {            //determine the SMTP server address. Instantiate an SMTP clientSystem.Net.Mail.SmtpClient client =NewSystem.Net.Mail.SmtpClient ("smtp.qq.com"); //generate a Send address            stringStrfrom ="* * * * @qq. com"; //constructs a sender address objectMailAddress from=NewMailAddress (Strfrom,"Kakake", Encoding.UTF8); //constructs a recipient address objectMailAddress to =NewMailAddress (Toemail,"Kindle", Encoding.UTF8); //construct a Message object for an emailMailMessage message =NewMailMessage ( from, to); if(Type = =0) savetxt (filename); Else if(Type = =1) savepdf (filename); //Get file name            stringFileName =filename; //determine if a file exists            if(File.exists (fileName)) {//constructs an Attachment objectAttachment attach =NewAttachment (fileName); //get information about a filecontentdisposition disposition =Attach.                Contentdisposition; Disposition. CreationDate=System.IO.File.GetCreationTime (fileName); Disposition. Modificationdate=System.IO.File.GetLastWriteTime (fileName); Disposition. Readdate=System.IO.File.GetLastAccessTime (fileName); //add an attachment to a messagemessage.            Attachments.Add (attach); }            Else            {                Throw NewException ("["+ FileName +"] File not found! "); }            //add message subject and contentMessage. Subject ="Books sent to Kindle"; Message. Subjectencoding=Encoding.UTF8; Message. Body=""; Message. Bodyencoding=Encoding.UTF8; //set information for a messageClient. Deliverymethod =smtpdeliverymethod.network; Message. Bodyencoding=System.Text.Encoding.UTF8; Message. Isbodyhtml=false; //if the server supports a secure connection, set the secure connection to true. //Gmail support, 163 not supported, if it is Gmail, be sure to set it to trueClient. Enablessl =true; //set the user name and password. //string userState = message. Subject;Client. useDefaultCredentials =false; stringUsername ="User"; stringpasswd ="****"; //User Login InformationNetworkCredential mycredentials =Newnetworkcredential (username, passwd); Client. Credentials=mycredentials; //Send mailclient.        Send (message); }    }

Program: Http://pan.baidu.com/s/1dDndERB

Recently bought a Kindle, in order to facilitate reading, wrote a program crawl Web content sent to the Kindle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.