Advertisement
here2share

# yt_pl_autoclicker.py

Jul 18th, 2019
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.76 KB | None | 0 0
  1. # yt_pl_autoclicker.py
  2. # Intended to Organize Youtube Playlists VIA Windows --
  3. # In Being Rushed. Might Still Have A Few Odd Conflicts
  4.  
  5. from Tkinter import *
  6. from ctypes import windll, Structure, c_ulong, byref
  7. import PIL.ImageGrab
  8. import time
  9.  
  10. root = Tk()
  11. root.title("AutoClicker")
  12. root.resizable(width=FALSE, height=FALSE)
  13.  
  14. mouse = windll.user32
  15. state = False
  16. clicked = 0
  17.  
  18. speed = StringVar()
  19. speed.set(3)
  20. mainframe = Frame(root)
  21. mainframe.grid(padx=5, pady=5)
  22.  
  23. def StateFalse():
  24.     global state
  25.     state = False
  26.  
  27. def StateTrue():
  28.     global state, clicked
  29.     if not state:
  30.         clicked = 0
  31.     state = time.time()-4
  32. 0
  33.  
  34. speedLabel = Label(mainframe, text="Click interval\n(in seconds)").grid(column=1, row=2, sticky=W)
  35. speedEntry = Entry(mainframe, textvariable=speed, width=5).grid(column=2, row=2)
  36. startButton = Button(mainframe, text="Start", width=10, command=StateTrue).grid(column=1, row=3,columnspan=2,sticky=W)
  37. stopButton = Button(mainframe, text="Stop", width=10, command=StateFalse).grid(column=2, row=3, columnspan=2,sticky=E)
  38.  
  39. """It simulates the mouse"""
  40. MOUSEEVENTF_MOVE = 0x0001 # mouse move
  41. MOUSEEVENTF_LEFTDOWN = 0x0002 # left button down
  42. MOUSEEVENTF_LEFTUP = 0x0004 # left button up
  43. MOUSEEVENTF_RIGHTDOWN = 0x0008 # right button down
  44. MOUSEEVENTF_RIGHTUP = 0x0010 # right button up
  45. MOUSEEVENTF_MIDDLEDOWN = 0x0020 # middle button down
  46. MOUSEEVENTF_MIDDLEUP = 0x0040 # middle button up
  47. MOUSEEVENTF_WHEEL = 0x0800 # wheel button rolled
  48. MOUSEEVENTF_ABSOLUTE = 0x8000 # absolute move
  49. SM_CXSCREEN = 0
  50. SM_CYSCREEN = 1
  51.  
  52. VK_NEXT = b'0x22'       # PAGE DOWN key
  53.  
  54. import ctypes
  55.  
  56. LONG = ctypes.c_long
  57. DWORD = ctypes.c_ulong
  58. ULONG_PTR = ctypes.POINTER(DWORD)
  59. WORD = ctypes.c_ushort
  60.  
  61. INPUT_MOUSE = 0
  62. INPUT_KEYBOARD = 1
  63. INPUT_HARDWARE = 2
  64.  
  65. KEYEVENTF_EXTENDEDKEY = 0x0001
  66. KEYEVENTF_KEYUP = 0x0002
  67. KEYEVENTF_SCANCODE = 0x0008
  68. KEYEVENTF_UNICODE = 0x0004
  69.  
  70.  
  71. class MOUSEINPUT(ctypes.Structure):
  72.     _fields_ = (('dx', LONG),
  73.                 ('dy', LONG),
  74.                 ('mouseData', DWORD),
  75.                 ('dwFlags', DWORD),
  76.                 ('time', DWORD),
  77.                 ('dwExtraInfo', ULONG_PTR))
  78.  
  79.  
  80. class KEYBDINPUT(ctypes.Structure):
  81.     _fields_ = (('wVk', WORD),
  82.                 ('wScan', WORD),
  83.                 ('dwFlags', DWORD),
  84.                 ('time', DWORD),
  85.                 ('dwExtraInfo', ULONG_PTR))
  86.  
  87.  
  88. class HARDWAREINPUT(ctypes.Structure):
  89.     _fields_ = (('uMsg', DWORD),
  90.                 ('wParamL', WORD),
  91.                 ('wParamH', WORD))
  92.  
  93.  
  94. class _INPUTunion(ctypes.Union):
  95.     _fields_ = (('mi', MOUSEINPUT),
  96.                 ('ki', KEYBDINPUT),
  97.                 ('hi', HARDWAREINPUT))
  98.  
  99.  
  100. class INPUT(ctypes.Structure):
  101.     _fields_ = (('type', DWORD),
  102.                 ('union', _INPUTunion))
  103.  
  104.  
  105. def send_input(*inputs):
  106.     nInputs = len(inputs)
  107.     LPINPUT = INPUT * nInputs
  108.     pInputs = LPINPUT(*inputs)
  109.     cbSize = ctypes.c_int(ctypes.sizeof(INPUT))
  110.     return ctypes.windll.user32.SendInput(nInputs, pInputs, cbSize)
  111.  
  112.  
  113. def input_structure(structure):
  114.     if isinstance(structure, MOUSEINPUT):
  115.         return INPUT(INPUT_MOUSE, _INPUTunion(mi=structure))
  116.     if isinstance(structure, KEYBDINPUT):
  117.         return INPUT(INPUT_KEYBOARD, _INPUTunion(ki=structure))
  118.     if isinstance(structure, HARDWAREINPUT):
  119.         return INPUT(INPUT_HARDWARE, _INPUTunion(hi=structure))
  120.     raise TypeError('Cannot create INPUT structure!')
  121.  
  122.  
  123. def keyboard_input(code, flags):
  124.     return KEYBDINPUT(0, code, flags, 0, None)
  125.  
  126.  
  127. def keyboard_event(code, flags=KEYEVENTF_UNICODE):
  128.     return input_structure(keyboard_input(code, flags))
  129.  
  130.  
  131. def press(character):
  132.     code = ord(character)
  133.     send_input(keyboard_event(code))
  134.     send_input(keyboard_event(code, KEYEVENTF_KEYUP))
  135. 0
  136. class POINT(Structure):
  137.     _fields_ = [("x", c_ulong), ("y", c_ulong)]
  138. 0
  139. def MouseClick(x,y):
  140.     try:
  141.         windll.user32.SetCursorPos(x,y)
  142.     except:
  143.         return
  144.     mouse.mouse_event(2, 0, 0, 0, 0)  # left mouse button down
  145.     mouse.mouse_event(4, 0, 0, 0, 0)  # left mouse button up
  146.     windll.user32.SetCursorPos(pt.x,pt.y)
  147. 0
  148. def scan(x,y,rgb=0):
  149.     if not clicked:
  150.         pixel = PIL.ImageGrab.grab(bbox=(pt.x,pt.y,pt.x+1,pt.y+1)).load()[0,0]
  151.         print "Mouse Position:", pt.x,pt.y
  152.         print "RGB:", pixel
  153.         print
  154.     else:
  155.         print "Scan Position:", x,y
  156.         x0,y0 = 1312,106
  157.         pixel = PIL.ImageGrab.grab(bbox=(x0,y0,x0+1,y0+1)).load()[0,0]
  158.         if pixel == (191, 54, 12):
  159.             print '+'*40
  160.             if cv.nav > 2:
  161.                 cv.nav = 1
  162.         elif pixel == (38, 11, 2):
  163.             print '-'*40
  164.             cv.nav = 3
  165.         else:
  166.             return
  167.         if cv.nav == 3:
  168.             cv.nav = 1
  169.             screen = PIL.ImageGrab.grab(bbox=(x-5,y-5,x+5,y+5)).load()
  170.             cbox = []
  171.             yy = 0
  172.             for yz in range(y-3,y+3):
  173.                 xx = 0
  174.                 for xz in range(x-3,x+3):
  175.                     cbox += [screen[xx,yy]]
  176.                     xx += 1
  177.                 yy += 1
  178.             if (6,95,212) in cbox:
  179.                 MouseClick(400,700) # for skipping to next video
  180.                 return
  181.             print '!'*30,'selected!'
  182.             MouseClick(x,y)
  183.         else:
  184.             for y2 in range(y,y+300,10):
  185.                 windll.user32.SetCursorPos(x,y2)
  186.                 time.sleep(0.002) # to make sure any changes are captured
  187.                 screen = PIL.ImageGrab.grab(bbox=(x-3,y2-3,x+3,y2+3)).load()
  188.                 data = {}
  189.                 yy = 0
  190.                 for y0 in range(y2-3,y2+3):
  191.                     xx = 0
  192.                     for x0 in range(x-3,x+3):
  193.                         data[x0,y0] = screen[xx,yy]
  194.                         xx += 1
  195.                     yy += 1
  196.  
  197.                 if rgb:
  198.                     d = [data[z] for z in data]
  199.                     if tuple(rgb) in d:
  200.                         print '*** Found ***'
  201.                         MouseClick(x,y2)
  202.                         cv.nav += 1
  203.                         return
  204.         cv.nav = 1
  205.         print '...'
  206. 0
  207. class Cv(): 0
  208. cv = Cv()
  209. cv.nav = 1
  210. ymin = 150
  211. ymax = 720
  212. page_scroll = 1380,704
  213. wait = 1
  214. y = 0
  215.  
  216. print ">>> Auto Clicker --"
  217. print ">>> This Version Is Intended To Organize Youtube Playlists VIA Windows"
  218. while True:
  219.     if state:
  220.         if not clicked:
  221.             clicked = 1
  222.             print ">>> Auto Clicker -- Activated..."
  223.         if time.time() > state:
  224.             pt = POINT()
  225.             windll.user32.GetCursorPos(byref(pt))
  226.             state = time.time()+4
  227.  
  228.             if cv.nav == 1:
  229.                 scan(1306,100+y,[96]*3) # video menu
  230.             elif cv.nav == 2:
  231.                 scan(1045,120+y,[144]*3) # video menu > save to playlist
  232.             elif cv.nav == 3:
  233.                 scan(582,300)
  234.                 0 # scan(582,258,(6,95,212)) # ensure watchlist is deselect
  235.                 y += 100
  236.                 print 'X'*30,y
  237.                 if y > 499:
  238.                     print '--- PAGE SCROLL ---'
  239.                     MouseClick(400,700) # added measure to avoid scrolling the pop-up
  240.                     windll.user32.keybd_event(0x22,0,0,0) # PAGEDOWN press
  241.                     windll.user32.keybd_event(0x22,0,0x0002,0) # PAGEDOWN release
  242.                     y = 0
  243.                    
  244.             windll.user32.SetCursorPos(pt.x,pt.y)
  245.             print "cv.nav:", cv.nav
  246.            
  247.  
  248.     else:
  249.         if clicked:
  250.             print ">>> Auto Clicker -- Stopped"
  251.         clicked = 0
  252.         if time.time() > wait:
  253.             pt = POINT()
  254.             windll.user32.GetCursorPos(byref(pt))
  255.             wait = time.time()+3
  256.             scan(pt.x,pt.y)
  257.     try:
  258.         root.update()
  259.     except:
  260.         print ">>> Auto Clicker -- Exited"
  261.         break
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement