Advertisement
Guest User

Untitled

a guest
Jul 1st, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.11 KB | None | 0 0
  1. import autopy
  2. import pyautogui
  3. import os
  4. import sys
  5. import win32api
  6. import win32gui
  7. import win32ui
  8. import win32con
  9. import ctypes
  10. import time
  11. import thread
  12. import Image
  13. import subprocess
  14. from smtplib import SMTP as SMTP
  15. from os.path import basename
  16. from email.mime.text import MIMEText
  17. from email.MIMEMultipart import MIMEMultipart
  18. from email.mime.application import MIMEApplication
  19. from email.MIMEBase import MIMEBase
  20. from email import Encoders
  21. from functools import reduce
  22. from ctypes import windll, Structure, c_ulong, byref, wintypes
  23.  
  24. byref = ctypes.byref
  25. user32 = ctypes.windll.user32
  26.  
  27. emailFromUsername="email"
  28. emailToUsername=['email']
  29. emailPassword="NOPE"
  30. emailServer="smtp.gmail.com"
  31. screenshotbmp="C:\Users\Jeremy\Desktop\screenshot.bmp"
  32. screenShotLocation="C:\Users\Jeremy\Desktop\screenshot.jpg"
  33.  
  34. check_x=2362
  35. check_y=1224
  36. check_c="0x3367ae"
  37.  
  38. enable_run = 0
  39.  
  40. HOTKEYS = {
  41.     1 : (win32con.VK_F2, win32con.MOD_WIN),
  42.     2 : (win32con.VK_F3, win32con.MOD_WIN),
  43.     3 : (win32con.VK_F4, win32con.MOD_WIN),
  44.     4 : (win32con.VK_F12, win32con.MOD_WIN)
  45. }
  46.  
  47. def takeScreenShot():
  48.     hwin = win32gui.GetDesktopWindow()
  49.     width = win32api.GetSystemMetrics(win32con.SM_CXVIRTUALSCREEN)
  50.     height = win32api.GetSystemMetrics(win32con.SM_CYVIRTUALSCREEN)
  51.     left = win32api.GetSystemMetrics(win32con.SM_XVIRTUALSCREEN)
  52.     top = win32api.GetSystemMetrics(win32con.SM_YVIRTUALSCREEN)
  53.     hwindc = win32gui.GetWindowDC(hwin)
  54.     srcdc = win32ui.CreateDCFromHandle(hwindc)
  55.     memdc = srcdc.CreateCompatibleDC()
  56.     bmp = win32ui.CreateBitmap()
  57.     bmp.CreateCompatibleBitmap(srcdc, width, height)
  58.     memdc.SelectObject(bmp)
  59.     memdc.BitBlt((0, 0), (width, height), srcdc, (left, top), win32con.SRCCOPY)
  60.     bmp.SaveBitmapFile(memdc, screenshotbmp)
  61.     im = Image.open(screenshotbmp)
  62.     im.save(screenShotLocation, "JPEG")
  63.  
  64. def emailScreenShot():
  65.     send_mail(emailFromUsername, emailToUsername,"AutoBot","Screenshot Attached", screenShotLocation, emailServer)
  66.  
  67. def send_mail(send_from, send_to, subject, text, files=None,
  68.               server="127.0.0.1"):
  69.     assert isinstance(send_to, list)
  70.  
  71.     msg = MIMEMultipart()
  72.     msg['From'] = send_from
  73.     msg['To'] = send_to[0]
  74.     msg['Subject'] = subject
  75.  
  76.     msg.attach(MIMEText(text))
  77.  
  78.  
  79.     with open(files, "rb") as fil:
  80.         part = MIMEApplication(
  81.             fil.read(),
  82.             Name=basename(files)
  83.         )
  84.         part['Content-Disposition'] = 'attachment; filename="%s"' % basename(files)
  85.         msg.attach(part)
  86.  
  87.    
  88.     conn = SMTP(host=server, port=587)
  89.     conn.set_debuglevel(1)
  90.     conn.ehlo()
  91.     conn.starttls()
  92.     conn.ehlo()
  93.     conn.login(emailFromUsername,emailPassword)
  94.     conn.sendmail(send_from, send_to, msg.as_string())
  95.     conn.close()
  96.  
  97. class POINT(Structure):
  98.     _fields_ = [("x", c_ulong), ("y", c_ulong)]
  99.  
  100. def queryMousePosition():
  101.     pt = POINT()
  102.     windll.user32.GetCursorPos(byref(pt))
  103.     return { "x": pt.x, "y": pt.y}
  104.  
  105. def exec_win_f2 ():
  106.     #XY = queryMousePosition()
  107.     global check_x
  108.     #check_x = XY['x']
  109.     global check_y
  110.     #check_y = XY['y']
  111.     global check_c
  112.     check_c=str(hex(autopy.bitmap.capture_screen().get_color(check_x, check_y)))
  113.     check_c=check_c
  114.     print("X:" + str(check_x) + " Y:" + str(check_y) + " Colour:" + check_c)
  115.  
  116. def exec_win_f3 ():
  117.     takeScreenShot()
  118.     emailScreenShot()
  119.  
  120. def exec_win_f4 ():
  121.     global enable_run
  122.     if enable_run == 1:
  123.         enable_run = 0
  124.         print("Disabled Execution")
  125.     else:
  126.         enable_run = 1
  127.         print("Enabled Execution")
  128.  
  129. def exec_win_f12 ():
  130.     print("exit")
  131.     sys.exit()
  132.  
  133. HOTKEY_ACTIONS = {
  134.     1 : exec_win_f2,
  135.     2 : exec_win_f3,
  136.     3 : exec_win_f4,
  137.     4 : exec_win_f12
  138. }
  139.  
  140. def test_for_change ():
  141.     global check_c
  142.     global check_x
  143.     global check_y
  144.     changedtest=hex(autopy.bitmap.capture_screen().get_color(check_x, check_y))
  145.     changedtest=changedtest
  146.     if  changedtest != check_c:
  147.         return 0
  148.     print ("appeared" + check_c + " = " + changedtest )
  149.     return 1
  150.  
  151. def execution_thread (threadName, delay):
  152.     while True:
  153.         while test_for_change() == 0:
  154.             time.sleep(delay)
  155.         print("Looping")
  156.         takeScreenShot()
  157.         emailScreenShot()
  158.         global enable_run
  159.         exit()
  160.         enable_run=0
  161.  
  162. for id, values in HOTKEYS.items ():
  163.     vk, modifiers = values[0], reduce (lambda x, y: x | y, values[1:])
  164.     print ("Registering id", id, "for key", vk)
  165.     if not user32.RegisterHotKey (None, id, modifiers, vk):
  166.         print ("Unable to register id", id)
  167.  
  168. try:
  169.     msg = wintypes.MSG ()
  170.     while user32.GetMessageA (byref (msg), None, 0, 0) != 0:
  171.         if msg.message == win32con.WM_HOTKEY:
  172.             action_to_take = HOTKEY_ACTIONS.get (msg.wParam)
  173.             if action_to_take:
  174.                 action_to_take ()
  175.         if enable_run == 1:
  176.             try:
  177.                 thread.start_new_thread( execution_thread, ("Thread-1", 2, ) )
  178.             except:
  179.                 print ("Error: unable to start thread")
  180.         user32.TranslateMessage (byref (msg))
  181.         user32.DispatchMessageA (byref (msg))
  182.  
  183. finally:
  184.   for id in HOTKEYS.keys ():
  185.     user32.UnregisterHotKey (None, id)
  186.     print("user32.UnregisterHotKey (None, id)")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement