Skip to content

Instantly share code, notes, and snippets.

@addohm
Last active September 17, 2021 01:19
Show Gist options
  • Save addohm/ea5d91bc9577146593f641435c3994cc to your computer and use it in GitHub Desktop.
Save addohm/ea5d91bc9577146593f641435c3994cc to your computer and use it in GitHub Desktop.
Count by reading screen and writing next integer
# -*- coding: utf-8 -*-
import time
import re
import random
import pygetwindow
import pyautogui
import numpy as np
import pytesseract
import cv2
pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe' # your path may be different
pyautogui.FAILSAFE = True
window_name = '😱-counting-to-1000000 - Discord'
user_name = 'ENTER YOUR DISCORD NICKNAME HERE'
class discord_autoincrement():
last_string = ''
last_user_string = ''
def get_discord_screencap(self):
# Get the window properties
discord = pygetwindow.getWindowsWithTitle(window_name)[0]
# Bring window to the foreground
discord.activate()
# Define the window dimensions
x1,y1 = discord.topleft
x2,y2 = discord.bottomright
# Screen cap
image = pyautogui.screenshot()
# Store in numpy array and crop to predefined dimensions
image_arr = np.array(image)
image_arr = image_arr[y2-120:y2-87,x1+380:x2-20]
# image = Image.fromarray(image_ar4r).show()
# Convert image
image = cv2.cvtColor(image_arr, cv2.COLOR_RGB2BGR)
return image
def get_full_screencap(self):
image = pyautogui.screenshot()
image = cv2.cvtColor(np.array(image),
cv2.COLOR_RGB2BGR)
return image
def write_screencap(self,f,n):
cv2.imwrite(n + '.png', f)
def read_last_number(self,image):
# greyscale the image
grey_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# image = Image.fromarray(grey_image).show()
# get image size and blow it up
(h, w) = grey_image.shape[:2]
grey_image = cv2.resize(grey_image, (w*2, h*2))
# Read image
mask = cv2.threshold(grey_image, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
# text = pytesseract.image_to_string(mask, config="--psm 6 digits")
text = pytesseract.image_to_string(mask, config="").split()
if len(text) == 0: text = self.last_string
# Combine all of the elements from the list, except the last to form the user name as a string
name = str('').join(text[:-1])
# Extract only digits from string
num = re.sub(r"\D", "", text[-1])
# Make sure its the last six digits of the string read
num = num[-6:]
# Write images
self.write_screencap(image,'image')
self.write_screencap(mask,'mask')
# cv2.imshow("mask", mask)
# cv2.waitKey(0)
# print("Detected " + name + " entered " + num + " as the current number.")
return name, num
def type_next_number(self,text):
num = int(text)+1
# Write the number into the window and press enter
for c in str(num):
rand_delay = random.randint(2,10) * 0.01
if c.isdigit():
pyautogui.write(str(c))
time.sleep(rand_delay)
# Disabled because its too fast and wouldn't show "user typing..."
# pyautogui.write(str(num))
self.last_string = str(num)
pyautogui.press("Enter",1,0.2)
print("Wrote " + str(num) + " for the next number.")
if __name__ == "__main__":
humanize = True
loops = 0
loop_reset = False
disc = discord_autoincrement()
while True:
# Randomize how how consecutive the loop can be
loop_limit = random.randint(50,100)
# Capture time to create some random breaks to humanize
t = time.localtime(time.time())
# Pause loop on the 0s and 5s
m_on_5s = t.tm_min % 5
if (loops < loop_limit and m_on_5s != 0 and humanize == True) or humanize == False:
image = disc.get_discord_screencap()
disc.last_user_string, this_number_str = disc.read_last_number(image)
# After humanizing, make sure someone else has entered a number before starting loop
if ((loop_reset == True and disc.last_user_string != user_name) or (loop_reset == False)):
# Check to make sure the string is a number before continuing
if this_number_str != '' and isinstance(int(this_number_str), int):
disc.type_next_number(this_number_str)
loop_reset = False
loops = loops + 1
rand_delay1 = random.randint(1,5)
print("Sleeping for " + str(rand_delay1) + " seconds.")
time.sleep(rand_delay1)
else:
print("The number parsed was not a number.")
else:
print("Waiting for a new user to enter a number.")
else:
print("Inserting randomness. Loops: " + str(loops) + ", On the 5s: " + str(bool(m_on_5s)) + ", Last User: " + disc.last_user_string + ".")
#create some random breaks to humanize
if m_on_5s == 0:
rand_delay2 = random.randint(45,90)
loop_reset = True
print("Sleeping for " + str(rand_delay2) + " for humanization.")
time.sleep(rand_delay2)
elif loops >= loop_limit:
rand_delay2 = random.randint(300,1800)
loop_reset = True
loops = 0
print("Sleeping for " + str(rand_delay2) + " for humanization.")
time.sleep(rand_delay2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment