importos#登录状态
status_dic ={'username': None,'status': False
}
flag=True#用户名,密码 以字典形式打开
defuser_pwd():
with open("user", encoding="utf-8") as f:
dic={}for i inf:
li= i.strip().split("|")
dic[li[0].strip()]= li[1]returndic#登录函数
deflogin():
u_p=user_pwd()
count=0while count < 3:
username= input("请输入姓名:").strip()
password= input("请输入密码:").strip()if username in u_p and password ==u_p[username]:
status_dic["username"] =username
status_dic['status'] = True #状态改为True
print("登陆成功")returnTrueelse:print("账号或密码错误,请重新登录")
count+= 1
#注册函数
defregister():while 1:
dict1=user_pwd()
username= input("请输入新账号:").strip()
password= input("请输新入密码:").strip()if username in dict1: #判断用户是否在文件中存在
print("用户名已存在,请重新输入")continue
elif not username.isalnum(): #判断用户是否是字母或数字组成
print("账号含有非法字符,请重新输入")continue
elif 5 < len(password) < 15: #限制密码在6-14位之间
with open("user", encoding="utf-8", mode="a") as f1:
f1.write(username+"|"+password+"\n")print("恭喜!注册成功")returnTrueelse:print("密码不合规范,请重新输入")#装饰器
def auth(f): #限制3-7选项,没有登陆不能访问
def inner(*args, **kwargs):if status_dic["status"]:
ret= f(*args, **kwargs)returnretelse:print("-------登录页面-------")
login()
ret= f(*args, **kwargs)returnretreturninner
@authdefarticle():print(f'欢迎{status_dic["username"]}访问文章页面')print("""1.写入文件
2.导入md文件""")while 1:
choice= input("请输入要选择的操作:").strip()if choice == "1":
file_name= input("输入文件名:").strip()
content= input("请输入内容").strip()
with open(r'article/'+file_name, encoding="utf-8", mode="a") as f1:
f1.write(content)print("写入文章成功")break
if choice == "2":
with open("函数的进阶.md", encoding="utf-8") as f2,\
open(r"article/"+"函数的进阶.text", encoding="utf-8", mode="w") as f3:
f3.seek(0)for i inf2:
f3.write(i)print("导入文件成功")break
else:print("您输入的不合规范")
@authdefdiary():print(f'欢迎{status_dic["username"]}访问日记页面')
@authdefcomment():print(f'欢迎{status_dic["username"]}访问评论页面')
file= os.listdir(os.path.dirname(__file__)+'/article') #把article文件夹里的文件,变成列表形式
for index, obj in enumerate(file, 1): #循环列表
print(index, obj)while 1:
choice= input("请输入要评论的文章的序号:").strip()ifchoice.isdigit():
choice=int(choice)if 0 < choice <= len(file): #限制用户输入序号的范围
path = os.path.join(os.path.dirname(__file__)+'/article', file[choice-1]) #拼接路径,找到该文件
with open(path, encoding="utf-8", mode="r+") as f1:
ret=f1.read()print(ret)
content= input("请输入要评论的内容")
sensitive= ["苍老师", "东京热", "武藤兰", "波多野结衣"] #此列表为敏感字符
for i in range(len(sensitive)): #敏感字符替换等长度*
if sensitive[i] incontent:
content= content.replace(sensitive[i], '*' *len(sensitive[i]))if "-"*20 not in ret: #判断如果不在这里加上"评论区"三个字
f1.write("评论区:" + "\n" + "-"*20 + "\n" + f'{status_dic["username"]}:\n{content}' + "\n")print("评论成功")returnTrueelse: #否则直接写入评论
f1.write(f'{status_dic["username"]}:\n{content}' + "\n")print("评论成功")returnTrueelse:print("请选择正确的文章序号")else:print("输入内容含有非法字符")
@authdefenshrine():print(f'欢迎{status_dic["username"]}访问收藏页面')#注销
deflogin_out():
status_dic['username'] =None
status_dic['status'] =Falseprint('注销成功')#退出程序
defexit_program():globalflag
flag=Falsereturnflagdefrun():
choice_dict={1: login,2: register,3: article,4: diary,5: comment,6: enshrine,7: login_out,8: exit_program,
}whileflag:print('''欢迎来到博客园首页
1:请登录
2:请注册
3:文章页面
4:日记页面
5:评论页面
6:收藏页面
7:注销
8:退出程序''')
choice= input('请输入您选择的序号:').strip()ifchoice.isdigit():
choice=int(choice)if 0 < choice <=len(choice_dict):
choice_dict[choice]()else:print("您输入的超出范围")else:print("输入不合规范,重新输入")if __name__ == '__main__':
run()