Последняя активность 1699432823

并添加了hexo文章格式

export_wp_posts.py Исходник
1import os
2import pymysql.cursors
3import html2text as ht
4
5host = 'localhost'
6port = 3306
7username = 'root'
8password = 'root'
9database = 'wordpress'
10
11# Connect to the database
12connection = pymysql.connect(host=host,
13 port=port,
14 user=username,
15 password=password,
16 db=database)
17
18work_path = os.getcwd() + os.path.sep + "posts"
19if not os.path.exists(work_path):
20 os.makedirs(work_path)
21print("文件将会导出到脚本同目录下的'posts'文件夹中")
22
23hexo_md_head = """---
24title: %s
25category: auto_export
26comments: false
27comment: false
28date: %s
29updated:
30tags:
31permalink:
32thumbnail:
33toc:
34top:
35mathJax:
36---\n\n
37%s
38"""
39
40# 查询所有文章
41sql_all = """SELECT `ID`,`post_date_gmt`,`post_title`,`post_status`,`post_modified_gmt`,`post_type`,`post_content`
42FROM `wp_posts`
43WHERE `post_parent`=0
44AND `post_type`='post'
45ORDER BY `post_date_gmt` DESC
46"""
47
48# 根据ID查询文章最新版本
49sql_post_newest = """SELECT `ID`,`post_date_gmt`,`post_title`,`post_status`,`post_modified_gmt`,`post_type`,`post_content`
50FROM `wp_posts`
51WHERE `post_parent`=%s
52AND `post_status`='inherit'
53ORDER BY `post_date_gmt`
54DESC """
55
56
57try:
58 with connection.cursor() as cursor:
59
60 cursor.execute(sql_all)
61
62 results = cursor.fetchall()
63 print("共查询到 %d 篇文章,即将开始导出..." % (results.__len__()))
64
65 htmlWorker = ht.HTML2Text()
66 htmlWorker.bypass_tables = False
67
68 for i, row in enumerate(results):
69 ID = row[0]
70 date = row[1]
71 title = row[2]
72 content = row[6]
73 print("正在导出第 %d 篇文章: ID=%d, date=%s, title=《%s" % (i+1, ID, date, title))
74
75 cursor.execute(sql_post_newest % ID)
76 new_post = cursor.fetchone()
77 if new_post:
78 title = new_post[2]
79 content = new_post[6]
80
81 content = htmlWorker.handle(content)
82
83 try:
84 tmpPost = open(work_path + os.path.sep + str(date)[0:10] + "-"
85 + str(title).replace(" ", "-")
86 .replace("/", "").replace("|","").replace("\\","").replace("\"","'")
87 .replace(":","").replace("*","").replace("?","").replace("#","")
88 .replace("<","(").replace(">",")").replace("","[").replace("","]")
89 + ".md",
90 mode='w', encoding="utf-8")
91 tmpPost.write(hexo_md_head % (title, date, content))
92 tmpPost.flush()
93 tmpPost.close()
94 except:
95 err_post_info = "文章导出失败: ID=%d, title=《%s" % (ID, title)
96 print(err_post_info + "请查看日志")
97 export_log = open("." + os.path.sep + "error.log", mode='a', encoding='utf-8')
98 export_log.write(err_post_info + "\n\n")
99 export_log.close()
100
101 cursor.close()
102
103finally:
104 connection.close()