You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

590 lines
24 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import csv
import os
import random
import shutil
import tempfile
import time
import tkinter as tk
from io import BytesIO
from tkinter import messagebox
import requests
from PIL import Image, ImageTk
from TrialRecommend import DataRequest, Notes_Request
import func_collec
import dis_collect
class Mainpage:
def __init__(self,master,name):
self.c_data_index=0
self.c_image_index=0
self.c_content='暂无数据!'
self.c_image_max=0
self.c_data_max=0
self.content = ''
self.time = ''
self.collect = ''
self.likes = ''
self.share = ''
self.flag = 0
self.is_pre = 'False'
self.current_data_index = 0
self.current_data_image_index = 0
self.current_notes_index = 0
self.max_data_count = 0
self.max_notes_count = 0
self.max_iamge_count = 0
self.data_csv_file_path = 'data.csv'
self.notes_csv_file_path = 'notes.csv'
self.data_request = None
self.research = tk.StringVar()
self.user = name
self.root = master
self.root.title('推荐中心 v0.0.1')
# 调用函数来居中窗口
self.center_window(self.root)
self.init_main_Page()
def init_main_Page(self):
self.page = tk.Frame(self.root)
self.page.place(relx=0, rely=0, relheight=1, relwidth=1)
self.btn_exit = tk.Button(self.page, text='退出', command=self.exit, bg='#D7EFFF', fg='red',font='bold')
self.btn_exit.place(relx=0.9, rely=0.9, relheight=0.05, relwidth=0.07)
self.lable_user = tk.Label(self.page, text='欢迎您:' + self.user, fg='green', font=24)
self.lable_user.place(relx=0.86, rely=0, relheight=0.08, relwidth=0.13)
self.text = tk.Text(self.page, bg='#ADD9FE', font=18)
self.text.insert(tk.END, self.content)
self.text.config(state=tk.DISABLED, cursor='')
self.text.place(relx=0.58, rely=0.07, relheight=0.86, relwidth=0.3)
self.btn_Image_pre=tk.Button(self.page,text='上一张',bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.pre_image)
self.btn_Image_pre.place(relx=0.14, rely=0.01, relheight=0.05, relwidth=0.07)
self.btn_Image_next = tk.Button(self.page, text='下一张', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.next_image)
self.btn_Image_next.place(relx=0.24, rely=0.01, relheight=0.05, relwidth=0.07)
self.btn_liked_page = tk.Button(self.page, text='我的收藏夹', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.to_like_page)
self.btn_liked_page.place(relx=0.9, rely=0.1, relheight=0.05, relwidth=0.1)
self.btn_next = tk.Button(self.page, text='下一篇', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.next_data)
self.btn_next.place(relx=0.45, rely=0.45, relheight=0.08, relwidth=0.09)
self.btn_pre = tk.Button(self.page, text='上一篇', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.back_data)
self.btn_pre.place(relx=0.45, rely=0.35, relheight=0.08, relwidth=0.09)
self.btn_liked = tk.Button(self.page, text='收藏攻略', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.func_collect)
self.btn_liked.place(relx=0.9, rely=0.2, relheight=0.05, relwidth=0.1)
#
# self.btn_dis_liked = tk.Button(self.page, text='取消收藏', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.re)
# self.btn_dis_liked.place(relx=0.9, rely=0.3, relheight=0.05, relwidth=0.1)
self.lable_like=tk.Label(self.page,text='喜欢人数:'+self.likes,font=('SimHei',15,'bold'))
self.lable_like.place(relx=0.05, rely=0.93)
self.lable_like = tk.Label(self.page, text='收藏人数:'+self.collect,font=('SimHei',15,'bold'))
self.lable_like.place(relx=0.17, rely=0.93)
self.lable_like = tk.Label(self.page, text='转发人数:'+self.share,font=('SimHei',15,'bold'))
self.lable_like.place(relx=0.29, rely=0.93)
self.entry=tk.Entry(self.page,textvariable=self.research,font=('',20, "bold"))
self.entry.place(relx=0.35, rely=0.01, relheight=0.05, relwidth=0.3)
self.btn_research=tk.Button(self.page,text='搜索', bg='#D7EFFF',font=("SimHei",20, "bold"),command=self.researchPage)
self.btn_research.place(relx=0.66, rely=0.01, relheight=0.05, relwidth=0.1)
self.lable_time=tk.Label(self.page,text='发布时间:'+self.time,font=('SimHei',15,'bold'))
self.lable_time.place(relx=0.41, rely=0.93)
def to_init_page(self):
self.page2.destroy()
self.init_text()
self.init_main_Page()
self.update_max()
self.image_url(self.current_data_image_index)
def to_like_page(self):
data=0
with open('collections/{}.csv'.format(self.user),'r+',newline='',encoding='utf-8') as f:
reader = csv.reader(f)
if len(list(reader))>0:
data=1
if data==1:
self.c_data_index = 0
self.c_image_index = 0
self.c_content = '暂无数据!'
self.c_image_max = 0
self.c_data_max = 0
self.page.destroy()
self.c_init_text()
self.init_like_page()
self.c_image_url()
else:
self.init_like_page()
def c_next_data(self):
if self.c_data_index+1 < self.c_data_max:
self.c_data_index += 1
self.c_image_index = 0
self.c_init_text()
self.init_like_page()
self.c_image_url()
else:
messagebox.showinfo(message='这是最后一篇')
def c_pre_data(self):
if self.c_data_index-1<0:
messagebox.showinfo(message='这是第一篇')
else:
self.c_data_index -= 1
self.c_image_index=0
self.c_init_text()
self.init_like_page()
self.c_image_url()
def c_image_url(self):
with open('collections/{}.csv'.format(self.user),'r+',encoding='utf-8') as f:
reader = csv.reader(f)
row =''
i=0
for rows in reader:
if i==self.c_data_index:
row=rows
break
i += 1
self.load_image_from_url(self.page2,eval(row[2])[self.c_image_index])
def c_pre_image(self):
if self.c_image_index - 1 < 0:
messagebox.showinfo(title='提示', message='没有上一张了')
else:
self.c_image_index -= 1
self.c_image_url()
def c_next_image(self):
if self.c_image_index + 1 >= self.c_image_max:
messagebox.showinfo(message='这是最后一张')
else:
self.c_image_index += 1
self.c_image_url()
def c_init_text(self):
with open('collections/{}.csv'.format(self.user),'r+',encoding='utf-8') as f:
reader = csv.reader(f)
i=0
for row in reader:
i+=1
self.c_data_max=i
flag=0
with open('collections/{}.csv'.format(self.user),'r+',encoding='utf-8') as f:
reader = csv.reader(f)
if not next(reader):
self.c_content='暂无收藏数据'
else:
flag=1
if flag==1:
with open('collections/{}.csv'.format(self.user), 'r+', encoding='utf-8') as f:
reader = csv.reader(f)
index = 0
for row in reader:
if index == self.c_data_index:
title = row[1]
text = row[3].replace('<br/>', '\n').replace('.<br/>', '\n')
self.c_content = title + text
self.c_image_max=len(eval(row[2]))
break
index += 1
def init_like_page(self):
self.page2=tk.Frame(self.root)
self.page2.place(relx=0,rely=0,relheight=1,relwidth=1)
self.btn_init_page = tk.Button(self.page2, text='返回主页面',command=self.to_init_page, bg='#D7EFFF',font=('SimHei',15,'bold'))
self.btn_init_page.place(relx=0.9, rely=0.1, relheight=0.05, relwidth=0.1)
self.lable_user = tk.Label(self.page2, text='欢迎您:' + self.user, fg='green', font=24)
self.lable_user.place(relx=0.86, rely=0, relheight=0.08, relwidth=0.13)
self.btn_exit = tk.Button(self.page2, text='退出', command=self.exit, bg='#D7EFFF', fg='red',font='bold')
self.btn_exit.place(relx=0.9, rely=0.9, relheight=0.05, relwidth=0.07)
self.text = tk.Text(self.page2, bg='#ADD9FE', font=18)
self.text.insert(tk.END, self.c_content)
self.text.config(state=tk.DISABLED, cursor='')
self.text.place(relx=0.58, rely=0.07, relheight=0.86, relwidth=0.3)
self.btn_Image_pre = tk.Button(self.page2, text='上一张', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.c_pre_image)
self.btn_Image_pre.place(relx=0.14, rely=0.01, relheight=0.05, relwidth=0.07)
self.btn_Image_next = tk.Button(self.page2, text='下一张', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.c_next_image)
self.btn_Image_next.place(relx=0.24, rely=0.01, relheight=0.05, relwidth=0.07)
self.btn_next = tk.Button(self.page2, text='下一篇', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.c_next_data)
self.btn_next.place(relx=0.45, rely=0.45, relheight=0.08, relwidth=0.09)
self.btn_pre = tk.Button(self.page2, text='上一篇', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.c_pre_data)
self.btn_pre.place(relx=0.45, rely=0.35, relheight=0.08, relwidth=0.09)
self.btn_dis_liked = tk.Button(self.page2, text='取消收藏', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.remove_rows_with_content)
self.btn_dis_liked.place(relx=0.9, rely=0.3, relheight=0.05, relwidth=0.1)
def notes_request(self,next_back):
if next_back == 'next':
if self.current_data_index + 1 <= self.max_data_count:
with open('data.csv', 'r', encoding='utf-8') as f:
if self.current_notes_index!=self.flag:
self.current_notes_index += 1
self.current_data_index += 1
else:
self.current_notes_index += 1
self.current_data_index += 1
self.flag=self.current_notes_index
csv_reader = csv.reader(f)
data=''
i=0
for row in csv_reader:
if i == self.current_data_index:
data = row[0]
break
i += 1
if self.is_pre=='False':
Notes_Request.notes_request(data)
if self.flag==self.current_notes_index:
self.is_pre='False'
time.sleep(random.uniform(0, 1))
self.update_max()
#print(self.max_data_count, self.current_data_index, self.current_data_image_index)
self.init_text()
self.init_main_Page()
self.image_show()
else:
messagebox.showinfo(message='正在载入新的一批数据,请稍等!')
self.data_request.dataRequest()
if self.data_request.success == 'True':
time.sleep(random.uniform(1, 2))
with open('data.csv', 'r', encoding='utf-8') as f:
if self.current_notes_index != self.flag:
self.current_notes_index += 1
self.current_data_index += 1
else:
self.current_notes_index += 1
self.current_data_index += 1
self.flag = self.current_notes_index
csv_reader = csv.reader(f)
data=''
i=0
for row in csv_reader:
if i == self.current_data_index:
data = row[0]
i += 1
if self.is_pre == 'False':
Notes_Request.notes_request(data)
if self.flag == self.current_notes_index:
self.is_pre = 'True'
time.sleep(random.uniform(0, 1))
self.update_max()
print(self.max_data_count, self.current_data_index, self.current_data_image_index)
self.init_text()
self.init_main_Page()
self.image_show()
else:
messagebox.showinfo(message='网络请求失败,请稍后尝试!')
if next_back == 'back':
if self.current_notes_index-1<0:
messagebox.showinfo(message='这是第一篇文章')
else:
self.is_pre = 'True'
self.current_notes_index -= 1
self.current_data_index -= 1
with open('data.csv', 'r', encoding='utf-8') as f:
csv_reader = csv.reader(f)
first_row = ''
i=0
for row in csv_reader:
if i==self.current_data_index:
first_row=row[0]
i += 1
Notes_Request.notes_request(first_row)
time.sleep(random.uniform(0, 1))
self.update_max()
print(self.max_data_count, self.current_data_index, self.current_data_image_index)
self.init_text()
self.init_main_Page()
self.image_show()
# 返回上一篇
def back_data(self):
if self.max_data_count != 0:
self.current_data_image_index = 0
self.notes_request('back')
else:
messagebox.showinfo(message='请先搜索')
# 下一篇
def next_data(self):
if self.max_data_count != 0:
self.current_data_image_index = 0
self.notes_request('next')
else:
messagebox.showinfo(message='请先搜索')
# 下一张图片
def next_image(self):
if self.max_iamge_count == 0:
messagebox.showinfo(message='请先搜索')
elif self.current_data_image_index + 1 >= self.max_iamge_count:
messagebox.showinfo(message='这是最后一张')
else:
self.current_data_image_index += 1
self.image_url(self.current_data_image_index)
# 上一张图片
def pre_image(self):
if self.max_iamge_count == 0:
messagebox.showinfo(message='请先搜索')
elif self.current_data_image_index - 1 < 0:
messagebox.showinfo(title='提示', message='没有上一张了')
else:
self.current_data_image_index -= 1
self.image_url(self.current_data_image_index)
def researchPage(self):
self.clear_csv_file()
rs = self.research.get()
if rs == '':
messagebox.showinfo(title='提示', message='你还没有输入任何东西!')
else:
if DataRequest.DataRequest(str(self.research.get()), self.user).success == 'True':
self.content = ''
self.time = ''
self.collect = ''
self.likes = ''
self.share = ''
self.flag = 0
self.is_pre = 'False'
self.current_data_index = 0
self.current_data_image_index = 0
self.current_notes_index = 0
self.max_data_count = 0
self.max_notes_count = 0
self.max_iamge_count = 0
self.data_request = DataRequest.DataRequest(str(self.research.get()), self.user)
time.sleep(random.uniform(0,1))
self.init_notes_request()
else:
messagebox.showinfo(message='网络请求失败,请稍后尝试!')
def init_notes_request(self):
with open('data.csv', 'r', encoding='utf-8') as f:
reader = csv.reader(f)
for row in reader:
Notes_Request.notes_request(row[0])
break
self.update_max()
self.init_text()
self.init_main_Page()
self.image_show()
def image_url(self,col):
with open(self.data_csv_file_path,'r',encoding='utf-8') as f:
reader = csv.reader(f)
row =''
i=0
for rows in reader:
if i==self.current_data_index:
row=rows
i += 1
self.load_image_from_url(self.page,eval(row[3])[col])
def load_image_from_url(self,View,url):
# 使用requests从URL下载图片
response = requests.get(url)
img = Image.open(BytesIO(response.content))
img = img.resize((450, 600), Image.BICUBIC) # 调整像素,使用高质量重采样算法
# 使用PIL的ImageTk将PIL图片转换为Tkinter可以显示的格式
tk_img = ImageTk.PhotoImage(img)
# 创建一个标签来显示图片并添加到Tkinter窗口中
label = tk.Label(View, image=tk_img)
label.image = tk_img # 保持对tk_img的引用防止被垃圾回收
label.place(relx=0.05, rely=0.07)
def image_show(self):
if self.csv_is_empty():
messagebox.showinfo('提示',message='数据加载失败')
else:
self.image_url(0)
def csv_is_empty(self):
with open('notes.csv','r',encoding='utf-8') as f2:
rows = []
reader = csv.reader(f2)
for row in reader:
# 如果找到非空行则将rows列表设置为非空
if any(row):
rows.append(row)
return False # 如果只需要检查是否至少有一行非空,则可以提前退出循环
# 如果rows列表为空则CSV文件是空的或只包含空行
if len(rows) == 0:
print('notes 无数据')
return True
def init_text(self):
with open(self.notes_csv_file_path,'r',encoding='utf-8') as f:
reader = csv.reader(f)
index=0
for row in reader:
if index==self.current_notes_index:
title = row[0]
text = row[1].replace('<br/>', '\n').replace('.<br/>', '')
self.content=title+text
self.time = row[2]
self.collect = row[3]
self.likes = row[4]
self.share = row[5]
index += 1
def update_max(self):
with open(self.data_csv_file_path,'r',encoding='utf-8') as f:
reader = csv.reader(f)
index=0
for row in reader:
if index==self.current_data_index:
self.max_iamge_count = len(eval(row[3]))
break
index+=1
with open(self.data_csv_file_path, 'r', encoding='utf-8') as f:
reader = csv.reader(f)
counts = 0
for i in reader:
counts += 1
self.max_data_count = counts
with open(self.notes_csv_file_path,'r',encoding='utf-8') as f:
reader = csv.reader(f)
counts=0
for i in reader:
counts+=1
self.max_notes_count=counts
def clear_csv_file(self):
with open(self.data_csv_file_path, 'w') as file1: # 使用'w'模式打开文件
pass # 不需要执行任何操作,因为打开文件时内容已被清空
with open(self.notes_csv_file_path, 'w') as file2:
pass # 不需要执行任何操作,因为打开文件时内容已被清空
def exit(self):
messagebox.showinfo(title='提示', message='欢迎下次使用!')
self.root.quit()
def center_window(self, root, width=1300, height=700):
# 获取屏幕宽度和高度
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
# 计算窗口的左上角应该放置的位置
left = (screen_width / 2) - (width / 2)
top = (screen_height / 2) - (height / 2)
# 设置窗口的位置和大小
root.geometry("{0}x{1}+{2}+{3}".format(width, height, int(left), int(top)))
def func_collect(self):
if self.max_data_count!=0:
func_collec.collec(self.user, self.current_data_index)
dic={}
with open(self.data_csv_file_path,'r',encoding='utf-8') as f:
reader = csv.reader(f)
i=0
for row in reader:
if i==self.current_data_index:
dic={
'文章ID':row[0],
'文章标题':row[1],
'图片资源':eval(row[3])
}
break
i+=1
with open(self.notes_csv_file_path,'r',encoding='utf-8') as f:
reader = csv.reader(f)
i=0
for row in reader:
if i==self.current_data_index:
dic['文章内容']=row[1]
i+=1
with open('collections/{}.csv'.format(self.user),'a+',encoding='utf-8',newline='') as f:
writer = csv.DictWriter(f, fieldnames=['文章ID','文章标题','图片资源','文章内容'])
writer.writerow(dic)
else:
messagebox.showinfo('提示','请先搜索')
def func_discollect(self):
dis_collect.dis_collec(self.user,self.c_data_index)
row_to_delete = self.c_data_index
# 使用临时文件来存储修改后的内容
with open('collections/{}.csv'.format(self.user), newline='', encoding='utf-8') as csvfile, \
tempfile.NamedTemporaryFile(mode='w+t', newline='', encoding='utf-8', delete=False) as temp:
reader = csv.reader(csvfile)
writer = csv.writer(temp)
# 读取并写入除了要删除的行之外的所有行
for i, row in enumerate(reader):
if i != row_to_delete:
writer.writerow(row)
try:
os.remove('collections/{}.csv'.format(self.user))
except FileNotFoundError:
pass
# 将临时文件重命名为原始文件名
os.rename(temp.name, 'collections/{}.csv'.format(self.user))
def remove_rows_with_content(self):
dis_collect.dis_collec(self.user, self.c_data_index)
# 创建一个临时文件
with tempfile.NamedTemporaryFile(mode='w', delete=False,encoding='utf-8',newline='') as tmpfile:
# 打开原始文件并读取内容
with open('collections/{}.csv'.format(self.user), 'r', newline='',encoding='utf-8') as infile:
reader = csv.reader(infile)
writer = csv.writer(tmpfile)
# 遍历每一行,如果不包含指定内容则写入临时文件
i=0
for row in reader:
if self.c_data_index != i:
writer.writerow(row)
i+=1
# 替换原始文件为临时文件
tmpfile.close()
shutil.move(tmpfile.name, 'collections/{}.csv'.format(self.user))
if __name__ == '__main__':
root = tk.Tk()
Mainpage(master=root,name='Null')
root.mainloop()