Menu

博客旧数据迁移

祝福大家中秋快乐!!

在中秋到来之际,终于把以前BO-BLOG的数据全部迁移到这里,其实代码没有多少,但是分析的时间比较多。

之前非常非常喜欢写博客,所以写了很多很多年,但由于一些事情,我停止了博客的更新,不过最近,我又开始将线下写日记改成了线上写博客了。

之前的往事,我是不想再提了。

我以前是Bo-Blog 2.1.1这个版本,为了适配到新的WP,我查看了2边数据库的差别,然后直接采用数据库转存的方式,将Bo-Blog 的数据迁移到了这里。

迁移中,我迁移了附件,图片。但我的WP主题,是有特色图片的,以前 Bo-Blog中并没有这个功能,所以,这部分估计到时候还需要慢慢来添加好看的特色图片。

其中有2个模块,我也完整的迁移过来,第一个是代码,第二个是分类,我按照我需要的,在WP中增加了一个AH代码高亮的插件,然后将以前的代码标签转换成了现在的标签。

图片附件标签同样我也做了转换。

下面就是完整的python迁移代码 [ 因为AH没有PYTHON高亮,我用的其他高亮展示]:

 

import pymysql
import hashlib
import re
import time
import datetime
		

mysql_tyohouse = {
          'host':'',
          'port':,
          'user':'',
          'password':'',
          'db':'tyohouse',
          'charset':'utf8mb4',
          'cursorclass':pymysql.cursors.DictCursor,
          }

mysql_shermine = {
          'host':'',
          'port':,
          'user':'',
          'password':'',
          'db':'',
          'charset':'',
          'cursorclass':pymysql.cursors.DictCursor,
          }

mysql_config = mysql_tyohouse

mydb = pymysql.connect(**mysql_config)
dbcursor = mydb.cursor()
_db_wid = 0

old_data_list = []

try:
    sql_str = """SELECT * FROM tyohouse.blog_blogs;""" 
    dbcursor.execute(sql_str)
    _mysql_result = dbcursor.fetchall()

    for _result_node in _mysql_result:
        timeStamp = _result_node['pubtime']
        timeArray = time.localtime(timeStamp)
        otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)

        dateArray = datetime.datetime.utcfromtimestamp(timeStamp)
        otherStyleTimeGMT = dateArray.strftime("%Y-%m-%d %H:%M:%S")

        old_data_list.append(dict(title=_result_node['title'],content=_result_node['content'],date=otherStyleTime,dategmt=otherStyleTimeGMT,category=_result_node['category']))

except Exception as e:
    print(str(e))
finally:
	mydb.close()

mysql_config = mysql_tyohouse

mydb = pymysql.connect(**mysql_config)
dbcursor = mydb.cursor()
_db_wid = 0

old_attach_list = []

try:
    sql_str = """SELECT * FROM tyohouse.blog_upload;""" 
    dbcursor.execute(sql_str)
    _mysql_result = dbcursor.fetchall()

    for _result_node in _mysql_result:
        old_attach_list.append(dict(fid=_result_node['fid'],filepath=_result_node['filepath']))

except Exception as e:
    print(str(e))
finally:
	mydb.close()

mysql_config = mysql_shermine

mydb = pymysql.connect(**mysql_config)
dbcursor = mydb.cursor()
_db_wid = 0

try:
    for _old_node in old_data_list:
        
        #_ix = _old_node['content'].find("[codes=")
        #if _ix > 0:
        #    _ss = str(_old_node['content'])[_ix:_ix+10]
        #    print(_ss)
        #else:
        #    continue

        #_old_node['content'] = _old_node['content'].replace("',"\'")
        #_old_node['content'] = _old_node['content'].replace("\"","\\\"")
        #_old_node['content'] = _old_node['content'].replace("%","%%")
        _old_node['content'] = _old_node['content'].replace("[separator]","")
        _old_node['content'] = _old_node['content'].replace("[codes=C]","<pre class=\"line-numbers\"><code class=\"language-cpp\">")
        _old_node['content'] = _old_node['content'].replace("[codes=C#]","<pre class=\"line-numbers\"><code class=\"language-csharp\">")
        _old_node['content'] = _old_node['content'].replace("[/codes]","</code></pre>")
        _old_node['content'] = _old_node['content'].replace("[file][attach]176[/attach][/file]","【太久远,已经失效】")
        
        for _attach_old_node in old_attach_list:
            __c_attach_find = "[img][attach]%d[/attach][/img]" % _attach_old_node['fid']
            __c_attach_rp = "<br><figure class=\"wp-block-image\"><img src=\"http://www.shermine.cc/boblog_attachment/%s\" alt=\"\" class=\"wp-image-30\"/></figure>" % _attach_old_node['filepath']
            
            _old_node['content'] = _old_node['content'].replace(__c_attach_find,__c_attach_rp)


        sql_str = 'insert into shermine.shermine_posts(shermine_posts.post_author,shermine_posts.post_date,shermine_posts.post_date_gmt,shermine_posts.post_content,shermine_posts.post_title, shermine_posts.post_status,shermine_posts.comment_status,shermine_posts.ping_status,shermine_posts.post_modified,shermine_posts.post_modified_gmt,shermine_posts.post_parent,shermine_posts.guid,shermine_posts.menu_order,shermine_posts.post_type) values (1,%s,%s,%s,%s,"publish","open","open",%s,%s,0,%s,0,"post")'

        dbcursor.execute(sql_str,(_old_node['date'], _old_node['dategmt'], _old_node['content'], _old_node['title'], _old_node['date'], _old_node['dategmt'], ' '))
        mydb.commit();
        _db_wid = dbcursor.lastrowid

        if _old_node['category'] == 1:
            sql_str = """insert into shermine.shermine_term_relationships(
            shermine_term_relationships.object_id,
            shermine_term_relationships.term_taxonomy_id,
            shermine_term_relationships.term_order) values (%d,1,0);""" % (_db_wid)
        else:
            sql_str = """insert into shermine.shermine_term_relationships(
            shermine_term_relationships.object_id,
            shermine_term_relationships.term_taxonomy_id,
            shermine_term_relationships.term_order) values (%d,2,0);""" % (_db_wid)
        
        dbcursor.execute(sql_str)
        mydb.commit();

        

except Exception as e:
    print(str(e))
finally:
	mydb.close()

print("end")

 

Categories:   Garfield's Diary

Comments