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

python通过图片对比 控制酷我音乐下载歌曲

程序员文章站 2022-04-28 09:58:12
...
# -*- coding:utf-8 -*-
import time
import win32gui
import win32ui
import win32api
import win32con

import pyautogui
import pyperclip

from pykeyboard import *
from pymouse import *

import os
from PIL import Image
from PIL import ImageGrab

import cv2
import aircv as ac
import numpy

#kwmusicmaindlg
def FindKWmuaic():	
	lpClassName="kwmusicmaindlg"
	lpWindowName=None
	hwnd=win32gui.FindWindow(lpClassName, lpWindowName)
	print(hwnd)
	return hwnd

#删除原有歌曲名称
def DelMusicName():
	win32api.SetCursorPos([310, 25])
	x,y=310,25
	for i in range(0,8):
		win32api.SetCursorPos([x+2*i, 25])
		time.sleep(0.3)
		#模拟点击
		win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
		win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
		#删除  清空字符串  键位码表 8:为backspace键
		win32api.keybd_event(8,0,0,0)
		win32api.keybd_event(46,0,0,0)

#搜索歌曲
def FindMUSIC(str):
	print("搜索歌曲:"+str)
	pyperclip.copy(str)  # 先复制
	pyautogui.hotkey('ctrl', 'v')  # 再粘贴
	win32api.keybd_event(13,0,0,0)

def FindPOS():
	win32api.SetCursorPos([10, 10])
	time.sleep(0.3)
	win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
	win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)

def captureIMG():
	# Fhwnd=FindKWmuaic()
	# rect = win32gui.GetWindowRect(Fhwnd);
	img = ImageGrab.grab((0, 0, 1000, 1000))
	img.save("capture"+".jpg")
	img.close()
	time.sleep(5)
	print("截屏完毕等待5秒")
	#img.show()

def FindDownP():
	imsrc = ac.imread('capture.jpg')
	imobj = ac.imread('download1.png')
	all_pos = ac.find_all_template(imsrc, imobj)
	print(all_pos)
	print("==========================")
	for i in range(0,2):
		print("不同下载按钮位置****************")
		print(all_pos[i]['result'])
		x,y=all_pos[i]['result']
		m=int(x)
		n=int(y)
		win32api.SetCursorPos([m,n])
		time.sleep(2)
		print("设置位置"+str(i))
		print("点击下载按钮")
		win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
		win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
		print("开始第二次对比")
		imgTwo = pyautogui.screenshot()
		imgTwo.save(r'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\pic_pyautogui.jpg')
		imgTwo.close()
		time.sleep(1)
		imgTwo = pyautogui.screenshot()
		imgTwo.save(r'C:\Users\Administrator\AppData\Local\Programs\Python\Python36-32\pic_pyautogui.jpg')
		imgTwo.close()
		time.sleep(3)
		imsrc = ac.imread('pic_pyautogui.jpg')
		imobj2 = ac.imread('down.png')
		pos2 = ac.find_template(imsrc, imobj2)
		print("################################################")
		print(pos2)
		print("################################################")		
		x2,y2=pos2['result']
		w=int(x2)
		z=int(y2)
		pyautogui.moveTo(w,z)
		win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
		win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
		print("pyautogui.moveTo")
		time.sleep(2)
		win32api.SetCursorPos([w+2,z+3])
		time.sleep(1)
		win32api.SetCursorPos([w+2,z+3])
		print("点击下载到电脑")
		win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
		win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
		time.sleep(1)
		# xx=int(POSx)
		# yy=int(POSy)
		# win32api.SetCursorPos([xx,yy])
		# time.sleep(1)
		# print("位置设定"+DownLoadP['result'])

	# for m in range(0,6):
	# 	print(m)
	# 	print(pos[m]['result'])
    # for i in range(0,6):
    # 	print(pos[i]['result'])
    # 	time.sleep(1)
    # 	win32api.SetCursorPos(pos[i]['result'])



if __name__ == "__main__":
	Fhwnd=FindKWmuaic()
	print(Fhwnd)
	#获取窗口坐标位置
	rect = win32gui.GetWindowRect(Fhwnd);
	print(rect)
	#窗口置顶 设置窗口位置
	win32gui.SetWindowPos(Fhwnd,win32con.HWND_TOPMOST,0,0,0,0,win32con.SWP_NOSIZE);
	#移动鼠标到搜索窗口
	print("移动鼠标到XY位置")
	win32api.SetCursorPos([310, 25])
	print("删除原有字符")
	DelMusicName()
	FindMUSIC("曾经心疼")
	print("download music")
	FindPOS()
	print("等待5秒,网络下载歌曲")
	time.sleep(5)
	print("截屏")
	captureIMG()
	print("下载按钮查找。")
	FindDownP()

python通过图片对比 控制酷我音乐下载歌曲
python通过图片对比 控制酷我音乐下载歌曲
python通过图片对比 控制酷我音乐下载歌曲

python通过图片对比 控制酷我音乐下载歌曲

相关标签: 代码 python