今天不想复习,用了半晚上的时间把typecho的网站搬到github pages上

由于我阿里云是去年十月申请免费空间,而不知不觉中已经快要到期了,期间我也没怎么更新网站,而最近又由于考研没时间没精力打理,就转到github吧。 githubpages相对来说创建文章结构比较简单,干脆直接连接数据库然后读取出来吧。 由于我php实在不怎么样,就在本地用python链接然后存取吧。 首先我没怎么看typecho的具体表结构,就按照大体想法读取,所以肯定不是多标准,不过发现转存出来的还能用,这就可以了。 hexo文档主要有几个参数

1
2
3
4
5
6
7
---
title: 网站搬家到github
date: 2016-08-21 21:46:55
tags: [技术,python,github]
categories: 建站
---
正文

很简单,干脆直接write吧,对效率没什么要求。 然而我在具体写代码的时候发现tags以及categories没在contents表里面明写,而是用的相互的一个表关系来做的。 这就要稍微复杂点了,干脆来两个全局变量写进去吧。 这个是主要的代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#coding:utf-8
import mysql.connector
from datetime import *
import time
from functools import reduce
import codecs

metas=[]
relationships=[]


def get_data(table):
	conn=mysql.connector.connect(user='root',password='',database='qdm201219609_db')
	cursor=conn.cursor()
	cursor.execute('select * from %s'%table)
	values=cursor.fetchall()
	cursor.close()
	conn.close()
	return values

def shorten_content(content):
	content=content.replace('','',1)
	return content

def get_tags_and_cat(id):
	tags=[]
	for relation in relationships:
		if(id==relation[0]):
			for meta in metas:
				if (relation[1]==meta[0]):
					tags.append(meta[1])
	if (tags==None or len(tags)==0):
		tags,cat='',''
	else:
		cat=tags[0]	
		tags='['+reduce(lambda x,y:x+','+y,tags)+']'
	return tags,cat


def save(contents):
	for content in contents:
		with codecs.open(content[1]+".md",'w+','utf-8') as f:
			f.write("---\n")
			f.write('title: '+content[1]+'\n')
			curr=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(content[3]))
			f.write('date: '+curr+'\n')
			tags,cat=get_tags_and_cat(content[0])
			f.write('tags: '+tags+'\n')
			f.write('categories: '+cat+'\n')
			f.write("---\n")
			f.write(shorten_content(content[5]))

if __name__ == '__main__':
	metas=get_data('typecho_metas')
	relationships=get_data('typecho_relationships')
	contents=get_data('typecho_contents')
	save(contents)

嗯,这个是文章内容,差不多完成了。 评论还得导入啊,用多说吧,应该导入也不难,打开多说开发者中心。。。。 算了,我还是有时间了继续折腾吧,原来博客的评论也没有什么精华的内容。 等到有时间了看下typecho的表结构再来写吧,直接按想法瞎蒙确实挺坑。 嗯,就写到这吧,考研了对什么都提不起来兴趣。 不过用python写点小程序还是比较爽的。