main.py
import os
import time
from zhihu_oauth import ZhihuClient
import html2text
_h = html2text.HTML2Text()
layout_format = {}
for filename in os.listdir('mod'):
if filename.endswith('py'):
exec(open(os.path.join('mod', filename)).read())
print(layout_format)
def format_date(unix_timestamp_str):
return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(unix_timestamp_str)))
class ZhihuParser:
def __init__(self, answer, layout='md'):
self.layout = layout
self.title = answer.question.title
self.author = answer.author.name
self.created_time = format_date(answer.created_time)
self.updated_time = format_date(answer.updated_time)
self.voteup_count = answer.voteup_count
self.thanks_count = answer.thanks_count
self.content = answer.content
self.content = self.__parser()
def __parser(self):
return _h.handle(self.content)
def __str__(self):
return layout_format.get(self.layout)(self)
def __getitem__(self, key):
return eval('self.' + key)
if __name__ == "__main__":
TOKEN_FILE = 'token.pkl'
client = ZhihuClient()
if os.path.isfile(TOKEN_FILE):
client.load_token(TOKEN_FILE)
else:
client.login_in_terminal()
client.save_token(TOKEN_FILE)
print(ZhihuParser(client.answer(128260462)))
exit()
mod/md.py
markdown_template = """# %(title)s
Author: %(author)s
Create Date: %(created_time)s
Last Update: %(updated_time)s
%(content)s
"""
def markdown(self):
return markdown_template % self
layout_format['md'] = markdown
其中,
def __getitem__(self, key):
return eval('self.' + key)
这段有更好的写法吗?
以及我想让用户把自己写的mod文件放到mod文件夹下程序自动挂载,实现代码为:
layout_format = {}
for filename in os.listdir('mod'):
if filename.endswith('py'):
exec(open(os.path.join('mod', filename)).read())
配合插件中的
layout_format['mod_name'] = mod_function
这样写有啥优化改进的地方吗?欢迎提建议
添加回答
举报
0/150
提交
取消