为了搬家到wp我可是下了一番功夫,自写了一个脚本,关于日期的处理还多要感谢我的队友Caesar@11111010。用python写的,一开始打算用sql语句直接写到数据库,后来一看数据库结构稍微有些复杂,索性懒了下,用xmlrpc提交的博客。呃,最早想用网上插件,不知道为啥上传完就没动静了,所以得自己写啊!!几经波折,耗时1小时多才搞定,哎, 都是泪啊。

源码如下:

#!/usr/env python
# -*- coding: GBK –*-


## Script Name: cnblogs2wordpress
## Date: 2014-04-29
## Author: CRoot,Caesar@11111010

from xml.etree import ElementTree
import xmlrpclib,datetime

def load():
    #print "请输入您要转换的.xml文件路径:"
    XMLFilename = raw_input("请输入您要转换的.xml文件路径:")
    fo = open(XMLFilename).read()
    #fo = 'blog.xml'
    read_xml(fo)

def read_xml(text):
    wp_username = raw_input("WordPres 账号:")
    wp_password = raw_input("WordPress 密码:")
    wp_url = raw_input("您的博客xmlrpc路径:")
    root = ElementTree.fromstring(text)
    lst_title = root.getiterator("title")
    lst_date = root.getiterator("pubDate")
    lst_article = root.getiterator("description")
    for i in range(0, len(lst_title)):
        #print "=============================================="
        #print lst_title[i].text.encode('gb2312')
        #print lst_date[i].text.encode('gb2312')
        #print lst_article[i].text.encode('gb2312')
		# print lst_date[i].text
		# print month2num(lst_date[i].text)
        rs = updatetowp(wp_url,wp_username,wp_password,lst_title[i].text,month2num(lst_date[i].text),lst_article[i].text,'','')
        print rs
    
    
def updatetowp(wp_url,wp_username,wp_password,title,sdate,content,tags,categories):
    wp_blogid=''
    status_published = 0
    server = xmlrpclib.ServerProxy(wp_url)
    #中文的gbk转换成utf8格式, 不转换是会出错
    categories= [categories.encode("UTF-8")]
    #中文的gbk转换成utf8格式
    title=title.encode("UTF-8")
    #中文的gbk转换成utf8格式
    content=content.encode("UTF-8")
    date_created=xmlrpclib.DateTime(datetime.datetime.strptime(sdate, "%Y-%m-%d %H:%M"))
    data = {'title': title, 'description': content, 'dateCreated': date_created , 'categories':categories, 'mt_keywords': tags,'post_status':'publish'}
    post_id = server.metaWeblog.newPost(wp_blogid, wp_username,wp_password, data, status_published)
    return post_id
    
def month2num(otime):
    month2num = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    result = ''
    weekday = otime[0:3]
    day = otime[5:7]
    month = str(month2num.index(otime[8:11]) + 1)
    year = otime[12:16]
    hour = otime[17:19]
    minute = otime[20:22]
    second = otime[23:25]
    
    result = year + '-' + month + '-' + day + ' ' + hour + ':' + minute
    return result

if __name__ == '__main__':
    load()
    #updatetowp("https://xcroot.com/xmlrpc.php","","","test112","content2222",'','')

目前就是这个样子啦~~

完整下载

页面下部广告

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*

鲁ICP备17018668号-1