import os
import hashlib
status_dic = {} # 用于记录登陆状态
file_path = 'register'
def get_username_psw(): # 获取密码库
username_psw = {}
with open(file_path, 'r', encoding='utf-8') as f:
for line in f:
username, psw = line.strip().split('|')
username_psw[username] = psw
return username_psw
def logon(): # 注册模块,三次机会
time = 3
username_psw = get_username_psw() # 调用密码库
while time > 0:
username = input('请输入用户名(数字和字母):').strip()
psw = input('请输入密码(长度在6~14之间):').strip()
if username.isalnum() and (6 <= len(psw) <= 14) and (username not in username_psw):
with open(file_path, mode='a', encoding='utf-8') as f:
md5 = hashlib.md5()
md5.update(psw.encode('utf-8'))
f.write(f'{username}|{md5.hexdigest()}\n')
print('----注册成功----')
if len(status_dic) == 0:
print('----已帮您登陆----')
status_dic[username] = True
return True
time -= 1
print(f'----注册不成功,你还有{time}次机会----')
print('----注册失败,正在退出----')
return False
def login(): # 登陆模块,三次机会
if status_dic: # 判断是否已登陆
print('----您已登录,请先注销已登录账号----')
else:
time = 3
username_psw = get_username_psw() # 调用密码库
while time > 0:
username = input('请输入账户:').strip()
psw = input('请输入密码:').strip()
md5 = hashlib.md5()
md5.update(psw.encode('utf-8'))
if username_psw.get(username) == md5.hexdigest():
status_dic[username] = True # 将这个用户名的登陆状态改为True
print('----登陆成功----')
return True
time -= 1
print(f'----账号或密码错误,你还有{time}次机会----')
print('----登陆失败,正在退出----')
return False
def wrapper(func): # 装饰器
def inner():
if status_dic: # 判断登陆状态
func([i for i in status_dic][0]) # 获取登陆的用户名
else:
print('----请先登陆----')
if login():
func([i for i in status_dic][0])
else:
return False
return inner
@wrapper
def home(name): # 文章页面
print(f'----欢迎{name}进入文章页面----')
while 1:
num = input('请选择[1.直接写入内容 2.导入md文件 3.退出文章页面]:').strip()
if num == '1':
file_content = input('请输入[文件名|文件内容]:').strip()
try:
file, cotent = file_content.split('|', 1)
with open(f'./评论文章/{file}', mode='w', encoding='utf-8') as f:
f.write(cotent + '\n')
print('----写入成功----')
except ValueError:
print('----输入错误----')
elif num == '2':
file = input('请输入md文件的路径:').strip()
filename = os.path.basename(file).split('.')[0]
try:
with open(file, mode='r', encoding='utf-8') as f1,\
open(f'./评论文章/{filename}.txt', mode='w', encoding='utf-8') as f2:
for line in f1:
f2.write(line)
f2.write('\n')
print('----导入成功----')
except FileNotFoundError:
print('----路径不存在,请重新输入----''')
except OSError:
print('----路径不存在,请重新输入----''')
elif num == '3':
break
else:
print('----输入错误,请重新输入----')
@wrapper
def discuss(name): # 评论页面
print(f'----欢迎{name}进入评论页面----')
import os
tactful = ['苍老师', '东京热', '武藤兰', '波多野结衣']
lst = os.listdir('./评论文章')
while 1:
print('----文章列表----')
for num, el in enumerate(lst, 1):
print(num, el)
temp = input('请输入想要评论的文章的序号(输入0退出评论页面):').strip()
if temp == '0':
break
elif temp.isdecimal() and int(temp) <= len(lst):
with open(f'./评论文章/{lst[int(temp) - 1]}', mode='r+', encoding='utf-8') as f:
for line in f:
print(line, end='')
content = input('请输入你的评论:').strip()
for el in tactful:
if el in content:
content = content.replace(el, '*' * len(el))
f.write('-----------------评论区-----------------\n')
f.write(name + ':\n')
f.write('\t' + content + '\n')
print('----评论成功----')
else:
print('----输入错误,请重新输入----')
@wrapper
def diary(name): # 日记页面
print(f'----欢迎{name}进入日记页面----')
@wrapper
def collect(name): # 收藏页面
print(f'----欢迎{name}进入收藏页面----')
@wrapper
def logout(name): # 注销登陆
status_dic.clear()
print(f'----{name}登陆状态已注销----')
funcs = {1: login, 2: logon, 3: home, 4: discuss, 5: diary, 6: collect, 7: logout}
while 1:
print('''
1.登录
2.注册
3.进入文章页面
4.进入评论页面
5.进入日记页面
6.进入收藏页面
7.注销账号
8.退出程序''')
num = input('请输入您的选择:').strip()
if num.isdecimal() and 0 < int(num) < 8:
ret = funcs[int(num)]()
if ret == False:
break
elif num == '8':
print('----正在退出程序----')
break
else:
print('----输入有误,请重新输入----')