MetaWeblog-based blog information capturing-C ++ blog migration

Source: Internet
Author: User

 

Author: deercoder

 
Recently, I am preparing to migrate my blog to GitHub. However, this blog has a lot of content, so I want to find a way to migrate it. However, although the C ++ blog provides the backup function, however, the saved XML file is not in the standard Wordpress XML format, so you have to perform manual migration on your own. However, in the past, there were a lot of records of Hydrology, so you had to abandon it. So you thought about how to migrate your blog, how to capture Blog content and other issues.

When searching online, we found that CSDN and other websites provide standard MetaWeblog interfaces for third-party program access, blog writing, publishing, deletion, and other operations, C ++ provides related interfaces. You only need to log on to your account, select allow Web Service and MetaWeblog in Option-configuration, and access the MetaWeblog interface provided below.

For this reason, I have carefully studied the interfaces provided by the C ++ blog. The basic functions have provided API descriptions, such as writePost and getPost. For the struct, such as Post, it also defines the composition and type of the relevant member variables. Therefore, it is actually better. To this end, I started to write my own blog migration tool.

First determine the platform and technology used, after research, decided to use Apache XML-RPC library, and use Java implementation.
Second, write the corresponding functions and functions, and focus on preliminary verification during initial development. For this reason, a few basic functions are written in disorder, and some variables and definitions, as well as the required keys are written, then, I used an article in my blog to verify that I could obtain the corresponding information and save it to the HTML file. After opening the article, I found that all the required basic content was saved intact, therefore, subsequent work can be performed.
In the future, we will mainly expand the extended functions, including the use of other interfaces, such as obtaining all recent blogs, obtaining classified information, and obtaining blog information. In addition, after reconstruction, transfer the relevant tool functions to a class for implementation, and try to reuse the code as much as possible. This process has gone through several times and gradually found that the initial code is terrible. After reconstruction, fix unchanged fields, such as tag fields, addresses provided by service providers, and so on in a specific class. Once applied to other platforms, such as CSDN blogs, you only need to make the minimal modifications to facilitate code reuse.
Finally, a simple test is conducted and released to Github for version management and backup, and related logs are filled in. Further development may be performed in the future.

The above is just a brief introduction of the technology and development process used. As for the specific implementation details, the following describes the key points:
1. How to initiate a network connection and obtain the required blog information from the server?
The use of Apache XML-RPC library implementation, very simple, just need to make a simple configuration, the Code is as follows:

Config = new XmlRpcClientConfigImpl ();
Client = new XmlRpcClient ();
Config. setServerURL (new URL (url ));
Client. setConfig (config); in this way, the initialization and configuration of the client are completed. Then, the client can be directly used to discover connection requests and obtain the corresponding information.
List params = new ArrayList ();
Params. add ("test ");
Params. add (USER_NAME );
Params. add (USER_PASSWORD );
// The Object data group must be used. The List or other data groups cannot be used.
Object [] arr = (objectdomainclientclientclient.exe cute (GET_BLOGS_METHOD, params); when initiating a request, you only need to configure parameters first, according to the function parameters defined in each API document, and put them in an array, then execute the corresponding function, and save the function name using GET_BLOGS_METHOD to obtain the corresponding result.

2. How to handle the obtained results?
Because the types are defined in the API documentation, for example, the information of a blog is defined as a Post object, and this object is a class defined by it, including the title, time, and other Members, the member types may be different. For example, most of them are String type, but they are also of the Date type. How to obtain it?
For a Post object, obtaining is actually a Key-value pair, for example, the Key is "title", the value is the corresponding value "my blog", and so on. Therefore, you can use Map in Java to save key-value pairs, but here we will have a question: Since Post objects are saved by key-value pairs, as shown in
{
"Title", "my blog title"
"Description", "my blog body"
"Date", "blog posting time"
}
Then, can we directly use Map to save each key value and then read the information separately based on the key? The answer is no, so not all types are uniform, and keys are of the String type, because the values may be different as defined in the document, for example, the value corresponding to Date is of the Date type, while the value corresponding to title is of the String type, and they are all affiliated to Post objects. How can this problem be solved?
Here, we use Map to store each key-Value Pair and obtain information based on the type. The Code is as follows:
String title = getPostTitle (result );
If (title! = Null ){
System. out. println (title );
SaveString + = title +"
"; // The title of the article, which is stored in HTML Format
}

Date date = getPostDate (result );
If (date! = Null ){
String dateString = getPostTime (date );
System. out. println (dateString );
SaveString + = dateString +"
";
}

String article = getPostArticle (result );
If (article! = Null ){
System. out. println (article );
SaveString + = article +"
";
} Of course, the sub-function implementation is used here, but the actual effect is that, according to the type in the API documentation, when obtaining the value, the Object type is forcibly converted to the required type, for example, when the key is title, the value should be String, so the value is converted to the String type. When the Key-bit datedTime is used, the value type should be Date, which is converted to Date. In this way, all information can be read completely. Www.2cto.com
If the returned value is a struct array, the Object array is also used for storage. Then, each array member is read, forcibly converted to the Map type, and all key-value pairs of a struct are saved, then read the corresponding values one by one according to the struct definition. Of course, if all the struct types are String, you can directly use Map to save and read them.

Basically speaking, the above should be the main difficulty in these programs. After the solution is completed, most problems can also be completed. As for blog writing, the principles are the same, since all the information can be read, you only need to assign values to related statements and execute a corresponding function.
Of course, in the process of writing code, refactoring is very important. Although the amount of code is not large, the changes are still very large in history, later code and coupling are much lower than the previous ones, and fixed content is put into class members, which facilitates inheritance and code reuse, if you are interested, you can try the related functions of CSDN and other blogs.

Attached is the final output structure:
4765
Http://www.cppblog.com/deercoder/
My programming Park
Category Description: ACM
Category HTML Url: http://www.cppblog.com/deercoder/Category/17069.aspx
Category RSS Url: http://www.cppblog.com/deercoder/rss.aspx? Catid = 17069
Category Title: ACM
Category ID: 17069

Category Description: Android
Category HTML Url: http://www.cppblog.com/deercoder/Category/17867.aspx
Category RSS Url: http://www.cppblog.com/deercoder/rss.aspx? Catid = 17867
Category Title: Android
Category ID: 17867

Category Description: C ++
Category HTML Url: http://www.cppblog.com/deercoder/Category/13117.aspx
Category RSS Url: http://www.cppblog.com/deercoder/rss.aspx? Catid = 13117
Category Title: C ++
Category ID: 13117

Category Description: CTeX and LateX
Category HTML Url: http://www.cppblog.com/deercoder/Category/13991.aspx
Category RSS Url: http://www.cppblog.com/deercoder/rss.aspx? Catid = 13991
Category Title: CTeX and LateX
Category ID: 13991

Category Description: Git
Category HTML Url: http://www.cppblog.com/deercoder/Category/18145.aspx
Category RSS Url: http://www.cppblog.com/deercoder/rss.aspx? Catid = 18145
Category Title: Git
Category ID: 18145

Related Article

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.