forked from sheepzh/poetry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
45 lines (36 loc) · 1.23 KB
/
util.py
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
# pip3 install pypinyin
from pypinyin import lazy_pinyin
import os
import shutil
class Profile:
def __init__(self, href, author, title):
self.href = href
self.author = author
self.title = title
def __str__(self):
return self.author + " " + self.title + " " + self.href
def poet_path(self):
pinyin = "".join(lazy_pinyin(self.author))
return os.path.join(".", "tmp", self.author + "_" + pinyin)
def file_path(self):
return os.path.join(self.poet_path(), self.title + ".pt")
def write_poem(p, content):
poet_dir_path = p.poet_path()
if not os.path.exists(poet_dir_path):
os.makedirs(poet_dir_path)
title = p.title
poem_path = p.file_path()
content = "title:" + title + "\n" + "date:\n\n" + content
try:
with open(poem_path, "w", encoding="utf-8") as file:
file.write(content)
except OSError:
print("author:" + p.author + "\r\n")
print("title:" + title + "\r\n")
print("href:" + p.href + "\r\n")
print("-----------------------")
def remove_tmp_all():
try:
shutil.rmtree(os.path.join(".", "tmp"))
except FileNotFoundError:
pass