博客迁移

 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
58
59
60
import os
import json

class HugoBlog:
    def __init__(self):
        self.dir = ""

    def loadBlogs(self, dir):
        outDir = os.path.abspath(os.path.join(os.path.dirname(dir), os.pardir, "draft"))
        if os.path.exists(outDir):
            for f in os.listdir(outDir):
                os.remove(os.path.join(outDir, f))
        else:
            os.mkdir(outDir)
        for f in os.listdir(dir):
            print(os.path.basename(f))
            if os.path.basename(f).endswith(".json"):
                print(f)
                with open(os.path.join(dir, f),  mode='r', encoding='utf-8') as file:
                    data = json.loads(file.read())
                    content = []
                    content.append("---")
                    name = str(data["name"]).replace("#","").replace("/","、").split("''")[0].replace("'","")
                    print(name)
                    content.append("title: \""+name+"\"")
                    print(data["createDate"])
                    createDate = data["createDate"].split(".")[0]+"+08:00"
                    print(createDate)
                    content.append("date: "+ createDate)
                    # updatedDate = data["updatedDate"].split("\\.")[0]+"+08:00"
                    # print(updatedDate)
                    mkContent = data["mkContent"]
                    # print(mkContent)
                    tags = data["tags"]
                    categories = data["categories"]
                    print("tags:")
                    print(tags)
                    content.append("tags: ")
                    for tag in tags:
                        print(tag["name"])
                        content.append("- "+tag["name"])
                    print("category:")
                    print(categories)
                    content.append("categories: ")
                    for category in categories:
                        print(category["name"])
                        content.append("- "+category["name"])


                    content.append("draft: true")
                    content.append("---")
                    content.append(mkContent)
                    with open(os.path.join(outDir, name+".md"), mode="w") as md:
                        for l in content:
                            md.write(l)
                            md.write("\n")

if "__main__" == __name__:
    hugoBlog = HugoBlog()
    hugoBlog.loadBlogs("/Users/guananjie/github/blogworkspace/temp/blog/draft")