Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- from pynput.keyboard import Key
- import pynput.keyboard as keyboardO
- from pynput.mouse import Button
- import pynput
- mouse = pynput.mouse.Controller()
- import keyboard
- import cv2
- import numpy
- import sys
- import PyQt5
- import os
- import atexit
- import socket
- dirname = os.path.dirname(PyQt5.__file__)
- plugin_path = os.path.join(dirname, 'plugins', 'platforms')
- os.environ['QT_QPA_PLATFORM_PLUGIN_PATH'] = plugin_path
- from PyQt5 import QtWidgets
- import configparser
- from PyQt5.QtCore import *
- from PyQt5 import QtGui
- from PyQt5 import QtCore
- from PyQt5.QtWidgets import *
- from PyQt5 import uic
- from threading import Thread
- import datetime
- from time import sleep
- import pickle
- from copy import deepcopy
- import sys
- keyboardB = keyboardO.Controller()
- canvasWidth = 1200
- canvasHeight = 800
- startPositionX = 300
- startPositionY = 300
- countdown = 3
- timeBetweenCommand = 2
- ping_before_command = True
- ping_address = "www.google.com"
- ping_port = 80
- internet_reconnect_auto_unpause = True
- def get_platform():
- platforms = {
- 'linux1': 'Linux',
- 'linux2': 'Linux',
- 'darwin': 'OS X',
- 'win32': 'Windows'
- }
- if sys.platform not in platforms:
- return sys.platform
- return platforms[sys.platform]
- platform = get_platform()
- currentPath = os.path.dirname(os.path.realpath(__file__))
- if platform == 'linux':
- imageFolder = currentPath + "/Image"
- progressFolder = currentPath + "/Progress"
- configPath = currentPath + "/config.ini"
- else:
- imageFolder = currentPath + "\Image"
- progressFolder = currentPath + "\Progress"
- configPath = currentPath + "\config.ini"
- config = configparser.ConfigParser()
- config.read(configPath)
- mainWindow = None
- startPlacing = False
- pause = False
- commands = None
- errorMsg = ""
- progress = 0
- recentImagePath = None
- internetPause = False
- mouse_move = False
- mouseStartPosition = None
- startTime = None
- canSaveAgain = True
- """
- Gets image from image folder and converts it in to a numpy array
- """
- def imageToArray(path):
- global recentImagePath
- recentImagePath = path
- print(path)
- im = cv2.imread(path, cv2.IMREAD_UNCHANGED)
- return im
- """
- Main Window, mainly handles data input
- """
- class PrimaryWindow(QMainWindow):
- """
- PyQt signals for updating the GUI from the worker threads
- """
- createError = pyqtSignal()
- updateTimeLeft = pyqtSignal()
- def __init__(self):
- QWidget.__init__(self)
- uic.loadUi("window.ui", self)
- self.addItemSelection()
- self.addProgressSelection()
- #self.loadUIC()
- self.move(200, 200)
- self.convertCommands.clicked.connect(self.convertIntoCommands)
- self.loadProgress.clicked.connect(self.loadProgressFromFile)
- self.reload.clicked.connect(self.reloadImage)
- self.begin.clicked.connect(beginProcess)
- self.pause.clicked.connect(lambda: self.toggleProcess(True))
- self.terminate.clicked.connect(lambda: self.toggleProcess(False))
- self.saveProgress.clicked.connect(lambda: saveProgress())
- self.loadConfigValues()
- self.createError.connect(self.showErrorBox)
- self.updateTimeLeft.connect(self.updateRunning)
- self.progressBar.setValue(0)
- self.folderpath.setText(str(imageFolder))
- print(imageFolder)
- if not os.path.isfile(imageFolder):
- try:
- os.makedirs(imageFolder)
- print("couldn't find image folder creating one")
- except:
- print("skippin exception")
- else:
- print("found image folder")
- def toggleProcess(self, flag):
- global pause
- pause = flag
- def loadProgressFromFile(self):
- global commands, progress, pause, recentImagePath
- if os.path.exists(progressFolder):
- savedDetails = os.listdir(progressFolder)
- loadTemp = []
- for detail in savedDetails:
- if platform == 'linux':
- with open(progressFolder + "/" + detail, "rb") as input:
- loadTemp.append(pickle.load(input))
- else:
- with open(progressFolder + "\\" + detail, "rb") as input:
- loadTemp.append(pickle.load(input))
- for temp in loadTemp:
- if temp.imageName == str(self.toBeLoaded.currentText()).split(".")[0]:
- self.commands = deepcopy(temp.commands)
- commands = deepcopy(temp.commands)
- progress = temp.progress
- print(progress)
- self.allCommands.clear()
- if platform == 'linux':
- recentImagePath = imageFolder + "/" + temp.imageName + ".png"
- else:
- recentImagePath = imageFolder + "\\" + temp.imageName + ".png"
- pause = True
- for i in range(len(commands)):
- self.allCommands.append(
- "(" + str(i + 1) + "/" + str(len(commands)) + ") " + str(commands[i]))
- else:
- print("No progress folder found")
- def addProgressSelection(self):
- self.toBeLoaded.clear()
- if os.path.exists(progressFolder):
- savedDetails = os.listdir(progressFolder)
- self.toBeLoaded.addItems(savedDetails)
- for detail in savedDetails:
- if platform == 'linux':
- with open(progressFolder + "/" + detail, "rb") as input:
- load = pickle.load(input)
- self.progressArea.append(str((str(load.imageName), str(load.progress) + "/" + str(load.maxProgress), str(round((load.progress / load.maxProgress), 2) * 100) + "%")))
- else:
- with open(progressFolder + "\\" + detail, "rb") as input:
- load = pickle.load(input)
- self.progressArea.append(str((str(load.imageName), str(load.progress) + "/" + str(load.maxProgress), str(round((load.progress / load.maxProgress), 2) * 100) + "%")))
- else:
- print("No progress folder found")
- def addItemSelection(self):
- self.imageName.clear()
- if os.path.exists(imageFolder):
- savedDetails = os.listdir(imageFolder)
- self.imageName.addItems(savedDetails)
- def reloadImage(self):
- global errorMsg
- savedContracts = os.listdir(imageFolder)
- if savedContracts:
- try:
- if platform == 'linux':
- self.loadImage(imageFolder + '/' + self.imageName.currentText())
- else:
- self.loadImage(imageFolder + '\\' + self.imageName.currentText())
- except:
- errorMsg = "Couldn't find specified image: " + self.imageName.text()
- self.showErrorBox()
- else:
- print(imageFolder + "\\lol")
- print("no images")
- def updateRunning(self):
- self.progressBar.setValue((progress / len(self.commands) * 100))
- estimated_seconds = (len(self.commands) - progress) * float(self.betweenCommand.text())
- self.timeLeft.setText("Time Left: " + str(datetime.timedelta(seconds=estimated_seconds, milliseconds=0, microseconds=0)))
- newTime = startTime + datetime.timedelta(seconds=(len(self.commands)) * float(self.betweenCommand.text()), milliseconds=0, microseconds=0)
- self.estComplete.setText("Est. Complete: " + str(newTime))
- def showErrorBox(self):
- global errorMsg
- if not hasattr(self, "errorBox"):
- self.errorBox = self.createCriticalMessage(errorMsg)
- if not self.errorBox.isVisible():
- self.errorBox.show()
- def loadConfigValues(self):
- self.canvasWidth.setText(str(canvasWidth))
- self.canvasHeight.setText(str(canvasHeight))
- self.startX.setText(str(startPositionX))
- self.startY.setText(str(startPositionY))
- self.betweenCommand.setText(str(timeBetweenCommand))
- self.startTime.setText(str(countdown))
- def loadImage(self, imag):
- self.image = imageToArray(imag)
- image_shape = self.image.shape
- self.image_width = image_shape[1]
- self.image_height = image_shape[0]
- self.total_pixels = self.image_width * self.image_height
- self.coloredIndexs = numpy.where(self.image[:, :, 3:] != [0])
- self.totalPixels.setText("Total Pixels: " + str(self.total_pixels))
- self.coloredPixels.setText("Colored Pixels: " + str(len(self.coloredIndexs[0])) + " (" + str(int((len(self.coloredIndexs[0]) / self.total_pixels) * 100)) + "%)")
- self.imageDimensions.setText("Image Dimensions: " + str((self.image_width, self.image_height)))
- def convertIntoCommands(self):
- global commands
- try:
- self.reloadImage()
- except Exception:
- self.noImages = self.createCriticalMessage("No Images in Image folder!")
- self.noImages.show()
- return
- if self.image_width + int(self.startX.text()) >= canvasWidth or self.image_height + int(self.startY.text()) >= canvasHeight:
- self.totalCommands.setText("Change start positions! Current one with image dimensions goes out of canvas boundaries!")
- return
- self.commandsRaw = []
- usedColors = []
- for i in range(len(self.coloredIndexs[0])):
- x = self.coloredIndexs[0][i]
- y = self.coloredIndexs[1][i]
- color = self.image[x, y, :3]
- if (y, x, color[2], color[1], color[0]) in self.commandsRaw:
- continue
- currentColorCheck = (color[2], color[1], color[0])
- for n in range(len(self.coloredIndexs[0])):
- x = self.coloredIndexs[0][n]
- y = self.coloredIndexs[1][n]
- color = self.image[x, y, :3]
- if color[2] == currentColorCheck[0] and color[1] == currentColorCheck[1] and color[0] == currentColorCheck[2]:
- if not (y, x, color[2], color[1], color[0]) in self.commandsRaw:
- self.commandsRaw.append((y, x, color[2], color[1], color[0]))
- self.commands = []
- last = (0, 0, 0)
- for i in range(len(self.commandsRaw)):
- if not self.commandsRaw[i][2] == last[0] or not self.commandsRaw[i][3] == last[1] or not self.commandsRaw[i][4] == last[2]:
- last = (self.commandsRaw[i][2], self.commandsRaw[i][3], self.commandsRaw[i][4])
- self.commands.append("setrgb(%s,%s,%s)" % (self.commandsRaw[i][2], self.commandsRaw[i][3], self.commandsRaw[i][4]))
- self.commands.append("pp(%s,%s)" % (str(self.commandsRaw[i][0] + int(self.startX.text())), str(self.commandsRaw[i][1] + int(self.startY.text()))))
- for i in range(len(self.commands)):
- self.allCommands.append("(" + str(i + 1) +"/" + str(len(self.commands)) + ") " + str(self.commands[i]))
- self.totalCommands.setText("Total Commands: " + str(len(self.commands)))
- estimated_seconds = len(self.commands) * float(self.betweenCommand.text())
- self.estimatedTime.setText("Estimated Time: " + str(datetime.timedelta(seconds=estimated_seconds, milliseconds=0, microseconds=0)))
- self.convertCommands.setEnabled(False)
- commands = self.commands
- if not os.path.isfile(progressFolder):
- try:
- os.makedirs(progressFolder)
- print("couldn't find progress folder creating one")
- except:
- print("skippin exception")
- def createCriticalMessage(self, error):
- msg = QMessageBox()
- msg.setIcon(QMessageBox.Critical)
- msg.setWindowTitle("Error")
- msg.setText(error)
- msg.setStandardButtons(QMessageBox.Ok | QMessageBox.Cancel)
- return msg
- def closeEvent(self, event):
- exit_handler()
- #def loadImage(self):
- def create_connection(address, port):
- if not ping_before_command:
- return True
- try:
- socket.create_connection((address, port))
- return True
- except OSError:
- pass
- return False
- """
- Creates config file in the same directory
- These are the default values it puts in
- """
- def generateConfigFile():
- """
- Skip file creation if file exists goes to read function
- """
- if not os.path.isfile(configPath):
- print("Created new config file in path: " + configPath)
- config.add_section("settings")
- config.set("settings", "canvas_width", "1360")
- config.set("settings", "canvas_height", "768")
- config.set("settings", "start_posX", "300")
- config.set("settings", "start_posY", "300")
- config.set("settings", "countdown", "3")
- config.set("settings", "time_between_command", "2")
- config.set("settings", "ping_before_command", "True")
- config.set("settings", "ping_address", "www.google.com")
- config.set("settings", "ping_port", "80")
- config.set("settings", "internet_reconnect_auto_unpause", "True")
- config.set("settings", "move_mouse", "True")
- with open(configPath, 'w') as configfile:
- config.write(configfile)
- else:
- print("Found config file")
- loadValues()
- """
- Populates global values
- """
- def loadValues():
- global canvasWidth, canvasHeight, startPositionX, startPositionY, timeBetweenCommand, countdown, ping_before_command, ping_address, ping_port, internet_reconnect_auto_unpause
- global mouse_move
- config = configparser.ConfigParser()
- config.read(configPath)
- canvasWidth = config.getint("settings", "canvas_width")
- canvasHeight = config.getint("settings", "canvas_height")
- startPositionX = config.getint("settings", "start_posX")
- startPositionY = config.getint("settings", "start_posY")
- timeBetweenCommand = config.getint("settings", "time_between_command")
- countdown = config.getint("settings", "countdown")
- ping_before_command = config.getboolean("settings", "ping_before_command")
- ping_address = config.get("settings", "ping_address")
- ping_port = config.getint("settings", "ping_port")
- internet_reconnect_auto_unpause = config.getboolean("settings", "internet_reconnect_auto_unpause")
- mouse_move = config.getboolean("settings", "move_mouse")
- class SaveImage():
- def __init__(self, imageName, progress, maxProgress, commandsN):
- self.imageName = imageName
- self.progress = progress
- self.maxProgress = maxProgress
- self.commands = commandsN
- def saveProgress():
- if platform == 'linux':
- imageName = recentImagePath.split("/Image/")[1]
- imageName = imageName.split(".")[0]
- newSave = SaveImage(imageName, progress, len(commands), commands)
- newPath = progressFolder + "/" + imageName + ".cmds"
- else:
- imageName = recentImagePath.split("\Image\\")[1]
- imageName = imageName.split(".")[0]
- newSave = SaveImage(imageName, progress, len(commands), commands)
- newPath = progressFolder + "\\" + imageName + ".cmds"
- with open(newPath, "wb") as output:
- pickle.dump(newSave, output, pickle.HIGHEST_PROTOCOL)
- print("Saved progress to " + newPath)
- class App():
- def __init__(self):
- application = QtWidgets.QApplication(sys.argv)
- application.processEvents()
- generateConfigFile()
- global mainWindow
- mainWindow = PrimaryWindow()
- mainWindow.show()
- sys.exit(application.exec_())
- def type(message):
- keyboardB.type(message)
- keyboardB.press(Key.enter)
- keyboardB.release(Key.enter)
- def placeCommands():
- global commands, startPlacing, mainWindow, pause, progress, internetPause, mouse_move, mouseStartPosition
- while True:
- sleep(0.01)
- if (startPlacing) and not pause:
- time_between = float(mainWindow.betweenCommand.text())
- for i in range(progress, len(commands), 1):
- if not pause:
- time1 = datetime.datetime.now()
- if create_connection(ping_address, ping_port):
- sleep(time_between)
- if mouse_move:
- mouse.position = (mouseStartPosition[0], mouseStartPosition[1])
- mouse.press(Button.left)
- mouse.release(Button.left)
- time2 = datetime.datetime.now()
- workerThreadUpdateTime = (time2 - time1)
- print("Placing " + str(i + 1) + "/" + str(len(commands)) + ' took ' + str(workerThreadUpdateTime))
- type(commands[i])
- progress = i
- mainWindow.updateTimeLeft.emit()
- if i == len(commands) - 1:
- print('Finished!')
- startPlacing = False
- else:
- print("lost internet connection")
- if internet_reconnect_auto_unpause:
- internetPause = True
- else:
- pause = True
- print("Auto unpause after internet reconnect is not turned on. Go into the config to unpause automatically after internet reconnect.")
- else:
- onPause()
- if internetPause:
- onPause()
- while not create_connection(ping_address, ping_port):
- sleep(2)
- print("Waiting for internet to reconnect...")
- internetPause = False
- def onPause():
- global canSaveAgain
- if canSaveAgain:
- sleep(1)
- print("Paused " + str(progress + 1) + "/" + str(len(commands)))
- saveProgress()
- canSaveAgain = False
- def beginProcess():
- global errorMsg, startTime, mouseStartPosition, pause, startPlacing
- if commands is None:
- errorMsg = "Commands not converted yet!"
- mainWindow.createError.emit()
- else:
- startTime = datetime.datetime.now()
- mouseStartPosition = mouse.position
- pause = False
- startPlacing = True
- def read_key_board():
- global commands, errorMsg, mainWindow, startPlacing, pause, mouseStartPosition, startTime, canSaveAgain
- while True:
- sleep(0.01)
- if keyboard.is_pressed('f6'):
- beginProcess()
- if keyboard.is_pressed('f7'):
- pause = True
- if keyboard.is_pressed('f8'):
- canSaveAgain = True
- pause = False
- def exit_handler():
- print("Exit")
- """
- Uses two additional threads
- """
- if __name__ == "__main__":
- #main
- if hasattr(os, 'geteuid'):
- if os.geteuid() != 0:
- print('You must be root to use this bot on linux.')
- atexit.register(exit_handler)
- os.chdir(currentPath)
- commandThread = Thread(target=placeCommands)
- commandThread.start()
- keyboardListener = Thread(target=read_key_board)
- keyboardListener.start()
- app = App()
Advertisement
Add Comment
Please, Sign In to add comment