欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python 制作磁力搜索工具

程序员文章站 2022-03-23 10:04:13
不知不觉已经到了 大年三十,在此祝大家 牛年大吉,恭喜发财!今天还是要发一篇博客,是关于tkinter的一款磁力搜索gui工具,可以帮助我们检索网络 资源。一.准备工作装库,主要有tkinter os...

不知不觉已经到了 大年三十,在此祝大家 牛年大吉,恭喜发财!今天还是要发一篇博客,是关于tkinter的一款磁力搜索gui工具,可以帮助我们检索网络 资源。

一.准备工作

装库,主要有tkinter os threading win32 pillow

二.预览

预览我们的软件

1.启动

python 制作磁力搜索工具

2.运行

python 制作磁力搜索工具

3.结果

可以选择保存链接到本地,也可以选择复制链接倒剪切板。

3.1保存到本地:

保存格式为txt

python 制作磁力搜索工具

3.2复制链接:

复制以后,如果存在第三方下载工具,就能在工具中打开。

python 制作磁力搜索工具

三.tk源代码

设计流程就不说了,本次设计主要在界面布局上。这里直接上源代码。

#coding:utf-8
from tkinter import ttk
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import askdirectory
import tkinter.simpledialog
from pil import image,imagetk
import os
import threading
import win32clipboard as w
from spiders.spider1 import magent_spider1
from spiders.spider2 import magent_spider2
from spiders.spider3 import magent_spider3
from spiders.spider4 import magent_spider4
from qq_infos import qq_data

#spider1和spider4比较稳定
imgs=['lsj.png']
class app:

 def __init__(self):
  self.window=tk()
  self.window.resizable(0,0)
  width=420
  height=400
  screen_width=self.window.winfo_screenwidth()
  screen_height=self.window.winfo_screenheight()
  left=(screen_width-width)/2
  top=(screen_height-height)/2
  self.window.geometry('%dx%d+%d+%d'%(width,height,left,top))
  self.window.title('老司机搜索器-v1.0')
  self.create_widget()
  self.place_sidget()
  self.set_config()
  self.window.mainloop()

 def create_widget(self):
  self.l1=ttk.label(self.window,text='选择引擎:')
  self.r_chose=stringvar()
  self.filepath=stringvar()
  self.ls_var=stringvar()
  self.l5_var=stringvar()
  self.l_p_var=stringvar()
  self.qq_screenname_var=stringvar()
  self.r1=radiobutton(self.window,text='引擎一',value='1',variable=self.r_chose)
  self.r2=radiobutton(self.window,text='引擎二',value='2',variable=self.r_chose)
  self.r3=radiobutton(self.window,text='引擎三',value='3',variable=self.r_chose)
  self.r4=radiobutton(self.window,text='引擎四',value='4',variable=self.r_chose)
  self.l2=ttk.label(self.window,text='关键字:')
  self.e1=ttk.entry(self.window)
  self.b1=ttk.button(self.window,text='搜索',)
  self.l3 = ttk.label(self.window, text='存储路径:')
  self.e2 = ttk.entry(self.window,textvariable=self.filepath)
  self.b2 = ttk.button(self.window, text='选择目录')
  self.qq_screenname=ttk.label(self.window,textvariable=self.qq_screenname_var)
  self.ls=label(self.window,bg='pink',textvariable=self.ls_var)
  self.listbox=listbox(self.window,selectmode=single)
  self.s_coll_vertical = scrollbar(self.window, orient=vertical)
  self.s_coll_level = scrollbar(self.window, orient=horizontal)
  self.b3=ttk.button(self.window,text='保存')
  self.paned = panedwindow(self.window)
  self.img=imgs
  img = image.open(self.img[0])
  photo = img.resize((180, 190))
  self.paned.image = imagetk.photoimage(photo)
  self.l4=label(self.window,image=self.paned.image,)
  self.b4=ttk.button(self.window,text='上一页')
  self.l_p=ttk.label(self.window,textvariable=self.l_p_var,background='orange')
  self.b5=ttk.button(self.window,text='下一页')
  self.l5=ttk.label(self.window,text='影片信息:',)
  self.m=menu(self.window)
  self.s1=menu(self.m,tearoff=false)
  self.s2=menu(self.m,tearoff=false)
  self.s3=menu(self.m,tearoff=false)

 def place_sidget(self):
  self.l1.place(x=20,y=12)
  self.r1.place(x=100,y=10)
  self.r2.place(x=180,y=10)
  self.r3.place(x=260,y=10)
  self.l2.place(x=20,y=45)
  self.e1.place(x=90,y=40,height=30,width=120)
  self.b1.place(x=230,y=40,width=80)
  self.l3.place(x=20,y=85)
  self.e2.place(x=90,y=80,height=30,width=120)
  self.b2.place(x=230,y=80,width=80)
  self.ls.place(x=0,y=114,width=420,height=22)
  self.listbox.place(x=20,y=140,width=190,height=180)
  self.b3.place(x=230,y=140,width=80)
  self.s_coll_vertical.place(x=200, y=140,width=15,height=180)
  self.s_coll_level.place(x=20, y=310,width=190,height=15)
  self.paned.place(x=230,y=170,height=180,width=300)
  self.b4.place(x=15,y=328,width=60)
  self.l_p.place(x=85,y=331,)
  self.b5.place(x=145,y=328,width=60)
  self.l4.place(x=215,y=170,height=200,width=220)
  self.l5.place(x=0,y=365,height=25,width=420)

 def set_config(self):
  self.lock=true
  self.ls_var.set('---还未搜索---')
  self.b1.config(command=self.search_res)
  self.r_chose.set(1)
  self.s_coll_vertical.config(command=self.listbox.yview)
  self.listbox['yscrollcommand'] = self.s_coll_vertical.set
  self.s_coll_level.config(command=self.listbox.xview)
  self.listbox['xscrollcommand'] = self.s_coll_level.set
  self.window['menu']=self.m
  self.s1.add_command(label='选择目录',command=self.open_file_savepath)
  self.s1.add_command(label='打开文件夹',command=self.open_dir)
  self.s1.add_separator()
  self.s1.add_command(label='退出',command=self.quit_window)
  self.m.add_cascade(label='文件',menu=self.s1)
  self.m.add_cascade(label='操作',menu=self.s2)
  self.m.add_cascade(label='关于',menu=self.s3)
  self.s2.add_command(label='搜索',command=lambda :self.thread_it(self.search_res))
  self.s2.add_command(label='复制',command=lambda :self.thread_it(self.copy_magent))
  self.s2.add_command(label='保存',command=lambda :self.thread_it(self.save_magent))
  self.s3.add_command(label='联系作者',command=self.show_qq)
  self.s3.add_command(label='解锁隐藏引擎',command=self.unlock_engine)
  self.b1.config(command=lambda :self.thread_it(self.search_res))
  self.b3.config(command=lambda :self.thread_it(self.save_magent))
  self.b4.config(command=lambda :self.thread_it(self.page_min))
  self.b5.config(command=lambda :self.thread_it(self.page_add))
  self.l5.config(textvariable=self.l5_var,background='#008080')
  self.b2.config(command=self.open_file_savepath)
  self.l_p_var.set('页码:')
  self.listbox.bind('<<listboxselect>>',self.display_infos)
  # 绑定esc键---退出
  self.window.bind('<escape>', self.escape)
  # 使用return键给输入框entry绑定enter事件---search搜索
  self.e1.bind('<return>', self.enter)
  self.window.protocol('wm_delete_window',self.quit_window)

 def enter(self):
  self.search_res()

 def escape(self,event):
  self.quit_window()

 def quit_window(self):
  ask=messagebox.askyesno('退出','真的要退出吗?')
  if ask:
   self.window.destroy()

 def open_file_savepath(self):
  self.file = askdirectory()
  self.filepath.set(self.file)
  abs_path = os.path.abspath(self.filepath.get())
  self.magnetic_dir = abs_path

 def open_dir(self):
  os.startfile(self.magnetic_dir)

 def show_qq(self):
  messagebox.showinfo('联系作者','作者qq:xxxxxxxxx')

 def search_res(self):
  key_word=self.e1.get()
  self.ls_var.set('')
  self.page=1
  if key_word:
   # print(self.r_chose.get())
   self.listbox.delete(0,end)
   #使用self.r_chose.get()判断选中了哪个
   if self.r_chose.get()=='1':
    spider1=magent_spider1()
    self.speed,self.flag,self.result_list= spider1.get_data(key_word,1)
    if self.speed is none:
     messagebox.showinfo('提示','此引擎停用!')
     self.r1.config(state='disable')
    self.ls_var.set(self.speed)
    self.l_p_var.set('页码:1')
    for data in self.result_list:
     title = data.split(')-*/(')[0]
     self.listbox.insert(end, title)
   elif self.r_chose.get()=='2':
    spider2=magent_spider2()
    self.speed,self.flag,self.result_list=spider2.get_data(key_word,1)
    if self.speed is none:
     messagebox.showinfo('提示','此引擎停用!')
     self.r2.config(state='disable')
    else:
     self.ls_var.set(self.speed)
     for data in self.result_list:
      title = data.split(')-*/(')[0]
      self.listbox.insert(end, title)
   #爬虫3只返回flag和result_list
   elif self.r_chose.get()=='3':
    spider3=magent_spider3()
    self.flag, self.result_list = spider3.get_data(key_word, 1)
    if self.flag:
     self.ls_var.set(f'搜索到关于{key_word}的资源')
     self.l_p_var.set('页码:1')
     for data in self.result_list:
      title = data.split(')-*/(')[0]
      self.listbox.insert(end, title)
    else:
     messagebox.showinfo('提示','此引擎停用!')
     self.r3.config(state='disable')
   elif self.r_chose.get()=='4':
    spider4=magent_spider4()
    self.speed,self.flag,self.result_list=spider4.get_data(key_word,1)
    if self.speed is none:
     messagebox.showinfo('提示','此引擎停用!')
     self.r4.config(state='disable')
    else:
     self.l_p_var.set('页码:1')
     self.ls_var.set(self.speed)
     for data in self.result_list:
      title = data.split(')-*/(')[0]
      self.listbox.insert(end, title)

   self.listbox.update()
  else:
   messagebox.showwarning('警告','请先输入关键字!')

 def display_infos(self,event):
  try:
   curr_content_index=self.listbox.curselection()[0]
   content=self.result_list[curr_content_index].split(']-*/[')[0]
   pre_con=content.split(')-*/(')[0]
   aft_con = ' 大小:' + content.split(')-*/(')[1]
   if len(pre_con)>45:
    #print(self.listbox[curr_content_index])
    self.l5_var.set(aft_con)
   else:
    self.l5_var.set(pre_con+aft_con)
  except indexerror:
   pass

 def do_turn_page(self,page):
  self.listbox.delete(0, end)
  key_word=self.e1.get()
  if self.r_chose.get()=='1':
   spider1 = magent_spider1()
   self.flag=spider1.get_data(key_word, page)[1]
   if self.flag:
    self.result_list = spider1.get_data(key_word, page)[2]
  elif self.r_chose.get()=='2':
   spider2 = magent_spider2()
   self.flag=spider2.get_data(key_word, page)[1]
   if self.flag:
    self.result_list = spider2.get_data(key_word, page)[2]
  elif self.r_chose.get()=='3':
   spider3 = magent_spider3()
   self.flag,self.result_list=spider3.get_data(key_word, page)
  elif self.r_chose.get()=='4':
   spider4 = magent_spider4()
   self.flag = spider4.get_data(key_word, page)[1]
   if self.flag:
    self.result_list = spider4.get_data(key_word, page)[2]
  if self.flag:
   for data in self.result_list:
    title = data.split(')-*/(')[0]
    self.listbox.insert(end, title)
   self.listbox.update()
  else:
   messagebox.showinfo('提示','已经是最后一页')

 def page_min(self):
  if self.page==1:
   messagebox.showwarning('警告','当前已经是第一页了')
  else:
   self.page-=1
   self.l_p_var.set('页码:'+str(self.page))
   self.do_turn_page(self.page)
   self.listbox.update()

 def page_add(self):
  if self.flag is false :
   messagebox.showwarning('警告','当前已经是最后一页了')
  else:
   self.page += 1
   self.l_p_var.set('页码:'+str(self.page))
   self.do_turn_page(self.page)
   self.listbox.update()

 def get_magnet(self):
  try:
   curr_content_index = self.listbox.curselection()[0]
   hashes = self.result_list[curr_content_index].split(']-*/[')[-1]
   file_name=self.result_list[curr_content_index].split(')-*/(')[0]
   if self.r_chose.get()=='1':
    spider1=magent_spider1()
    magnet=spider1.get_megent(hashes)
    return file_name, magnet
   elif self.r_chose.get() == '2':
    spider2 = magent_spider2()
    magnet = spider2.get_magent(hashes)
    return file_name, magnet
   elif self.r_chose.get() == '3':
    spider3 = magent_spider3()
    magnet = spider3.get_magent(hashes)
    return file_name, magnet
   elif self.r_chose.get() == '4':
    spider4 = magent_spider4()
    magnet = spider4.get_magent(hashes)
    return file_name, magnet
  except indexerror:
   messagebox.showwarning('警告','请先选中要下载的资源!')

 def save_magent(self):
  if os.path.exists(self.filepath.get()):
   try:
    filename, magent_link=self.get_magnet()
    if '.' in filename:
     filename=filename.replace('.','')
    file_path=self.magnetic_dir
    with open(file_path+filename+'.txt','w')as f:
     f.write(magent_link)
    messagebox.showinfo('提示','保存磁力链接成功!')
   except typeerror:
    pass
  else:
   messagebox.showwarning('警告', '您还未选择路径!')

 def copy_magent(self):
  magent_link=self.get_magnet()[1]
  w.openclipboard()
  w.emptyclipboard()
  w.setclipboardtext(magent_link)
  w.closeclipboard()
  messagebox.showinfo('提示','磁力链接已经复制到了剪切板!')

 def unlock_engine(self):
  if self.lock:
   # 获取字符串(标题,提示,初始值)initialvalue初始值
   qq_number = tkinter.simpledialog.askstring(title='解锁', prompt='请输入qq号:', )
   # 打印内容
   qq_screenname=qq_data(qq_number).get_screenname()
   if qq_screenname:
    self.qq_screenname_var.set('欢迎您:\n\n'+qq_screenname)
    self.qq_screenname.config(font=(('times',12)),foreground='tomato')
    self.qq_screenname.place(x=320,y=40)
    qq_data(qq_number).save_heade_img()
    messagebox.showinfo('恭喜','解锁成功!')
    self.img2=os.path.abspath('./')
    self.img2+='/head_img/'+qq_number+'.jpg'
    img = image.open(self.img2)
    photo = img.resize((190, 170))
    self.paned.image = imagetk.photoimage(photo)
    self.l4 = label(self.window, image=self.paned.image, )
    self.l4.place(x=215, y=170, height=180, width=210)
    self.r4.config(font=(('times',12)),foreground='blue')
    self.l4.config(background='red')
    self.r4.place(x=340, y=10)
    self.lock=false
   else:
    messagebox.showerror('错误','解锁失败!')
  else:
   messagebox.showwarning('警告','您已成功解锁!')


 def thread_it(self,func,*args):
  t=threading.thread(target=func,args=args)
  t.setdaemon(true)
  t.start()


if __name__ == '__main__':
 a=app()

以上就是python 制作磁力搜索工具的详细内容,更多关于python 磁力搜索的资料请关注其它相关文章!

相关标签: python 磁力搜索