From d446d984a5cc9cff2f9951c32acf2d04c80824ab Mon Sep 17 00:00:00 2001 From: CriptDit <1487413334@qq.com> Date: Fri, 31 May 2024 11:18:37 +0800 Subject: [PATCH] commit008 --- TR/TrialRecommend/Main.py | 197 ++++++++++++++++++++++--- TR/TrialRecommend/Notes_Request.py | 8 +- TR/TrialRecommend/collections/root.csv | 0 TR/TrialRecommend/dis_collect.py | 3 +- TR/TrialRecommend/test.py | 7 + 5 files changed, 186 insertions(+), 29 deletions(-) create mode 100644 TR/TrialRecommend/collections/root.csv create mode 100644 TR/TrialRecommend/test.py diff --git a/TR/TrialRecommend/Main.py b/TR/TrialRecommend/Main.py index d487d12..966e0ba 100644 --- a/TR/TrialRecommend/Main.py +++ b/TR/TrialRecommend/Main.py @@ -1,5 +1,7 @@ import csv +import os import random +import tempfile import time import tkinter as tk from io import BytesIO @@ -13,6 +15,12 @@ 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 = '' @@ -100,11 +108,101 @@ class Mainpage: self.init_text() self.init_main_Page() self.update_max() - self.image_url(self.current_data_index, self.current_data_image_index) + self.image_url(self.current_data_image_index) def to_like_page(self): - self.page.destroy() - self.init_like_page() + 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('
', '\n').replace('.
', '\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) @@ -120,37 +218,25 @@ class Mainpage: 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.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')) + 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')) + 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')) + 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')) + 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_liked = tk.Button(self.page2, text='收藏攻略', bg='#D7EFFF',font=('SimHei',15,'bold')) - self.btn_liked.place(relx=0.9, rely=0.2, relheight=0.05, relwidth=0.1) - - self.btn_dis_liked = tk.Button(self.page2, text='取消收藏', bg='#D7EFFF',font=('SimHei',15,'bold')) + self.btn_dis_liked = tk.Button(self.page2, text='取消收藏', bg='#D7EFFF',font=('SimHei',15,'bold'),command=self.func_discollect) self.btn_dis_liked.place(relx=0.9, rely=0.3, relheight=0.05, relwidth=0.1) - self.lable_like = tk.Label(self.page2, text='喜欢人数:', font=('SimHei',15,'bold')) - self.lable_like.place(relx=0.05, rely=0.93) - - self.lable_like = tk.Label(self.page2, text='收藏人数:', font=('SimHei',15,'bold')) - self.lable_like.place(relx=0.17, rely=0.93) - - self.lable_like = tk.Label(self.page2, text='转发人数:', font=('SimHei',15,'bold')) - self.lable_like.place(relx=0.29, rely=0.93) - def notes_request(self,next_back): if next_back == 'next': if self.current_data_index + 1 <= self.max_data_count: @@ -261,7 +347,7 @@ class Mainpage: messagebox.showinfo(message='这是最后一张') else: self.current_data_image_index += 1 - self.image_url(self.current_data_index, self.current_data_image_index) + self.image_url(self.current_data_image_index) # 上一张图片 def pre_image(self): @@ -280,6 +366,23 @@ class Mainpage: messagebox.showinfo(title='提示', message='你还没有输入任何东西!') else: if DataRequest.DataRequest(str(self.research.get())).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())) time.sleep(random.uniform(0,1)) self.init_notes_request() @@ -325,7 +428,7 @@ class Mainpage: if self.csv_is_empty(): messagebox.showinfo('提示',message='数据加载失败') else: - self.image_url(self.current_data_index,0) + self.image_url(0) def csv_is_empty(self): with open('notes.csv','r',encoding='utf-8') as f2: @@ -405,11 +508,57 @@ class Mainpage: 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] + + 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.current_data_index) + 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)) if __name__ == '__main__': diff --git a/TR/TrialRecommend/Notes_Request.py b/TR/TrialRecommend/Notes_Request.py index 10df68f..6763321 100644 --- a/TR/TrialRecommend/Notes_Request.py +++ b/TR/TrialRecommend/Notes_Request.py @@ -19,11 +19,11 @@ def notes_request(url): 'a1':'18f9b67ecdbh41bri2la0dgguminrd7dmmrk4pxf750000215521', 'webId':'6d97ebd3e6051489537e7a6aa1d1cf99', 'gid':'yYijDKYydJ6JyYijDKWdS6UCfDx4yDF3J108FiWfkk763T28IFfWf4888Jy22Jy8WYf0SyjD', - 'webBuild':'4.16.1', + 'webBuild':'4.17.2', 'web_session':'040069b3643d32585fff79ee79344bd4b89bc6', - 'acw_tc':'25f299cc55249ae472ab6eab4f8404ce338ad4b4544c7f87a592375d787ede3a', - 'websectiga':'16f444b9ff5e3d7e258b5f7674489196303a0b160e16647c6c2b4dcb609f4134', - 'sec_poison_id':'9e6926ce-0bb8-4335-992a-0ec7875fff69', + 'acw_tc':'459a6388281f21d6215ff80f1402afbfdf47eb5a1ef949b426ace99565a6fc50', + 'websectiga':'7750c37de43b7be9de8ed9ff8ea0e576519e8cd2157322eb972ecb429a7735d4', + 'sec_poison_id':'b1e1329b-735f-427c-bfd0-698a912c0366', 'xsecappid':'xhs-pc-web' } diff --git a/TR/TrialRecommend/collections/root.csv b/TR/TrialRecommend/collections/root.csv new file mode 100644 index 0000000..e69de29 diff --git a/TR/TrialRecommend/dis_collect.py b/TR/TrialRecommend/dis_collect.py index f3eb818..4fe6601 100644 --- a/TR/TrialRecommend/dis_collect.py +++ b/TR/TrialRecommend/dis_collect.py @@ -4,12 +4,13 @@ from tkinter import messagebox def dis_collec(user,data_index): id='' - with open('data.csv', 'r', encoding='utf-8', newline='') as file: + with open('collections/{}.csv'.format(user), 'r', encoding='utf-8', newline='') as file: reader = csv.reader(file) index=0 for row in reader: if index == data_index: id=row[0] + break index=index+1 db = mysql.connector.connect( host="localhost", diff --git a/TR/TrialRecommend/test.py b/TR/TrialRecommend/test.py new file mode 100644 index 0000000..3277e5f --- /dev/null +++ b/TR/TrialRecommend/test.py @@ -0,0 +1,7 @@ +import csv + +with open('data.csv','r',encoding='utf-8') as csvfile: + reader = csv.reader(csvfile) + for row in reader: + print(len(eval(row[3]))) + break \ No newline at end of file