r/Python • • Jun 18 '19

Created a click bot after reading Automate boring stuff :)

1.1k Upvotes

93 comments sorted by

294

u/OSRS_DabSlab Jun 18 '19

Reported for botting

🦀🦀🦀🦀🦀🦀

23

u/zeus5552 Jun 18 '19

Rise of robots😂😂The machine age

31

u/OSRS_DabSlab Jun 18 '19

Lol, I was just making a runescape meme.

But awesome project my dude, I used automate the boring stuff to create an app that scraped several different websites to compare cannabis strains and seed prices.

11

u/zeus5552 Jun 18 '19

That's awesome buddy,sorry for not understanding the meme though

15

u/OSRS_DabSlab Jun 18 '19

Its understandable. Runescape is a very niche community

6

u/temp123456789098765 Jun 18 '19

I got banned for botting lobbies 😭😭

0

u/Dreieck Jun 18 '19 edited Jun 19 '19

Wow, “lobbies”. I havent heard or seen that word for a loooong time.

Edit: why the hell am I getting downvoted for this, that word is nostalgic for people who played RS back in the day.

2

u/regeya Jun 18 '19

I've been thinking of writing an IMDB scraper to compare the cast list of shows and movies to the cast of MASH. I got the idea after realizing Lawrence Fishburne and Joe Patoliano (sp) were both on MASH.

Edit: meh, I'm not fixing it.

4

u/zeus5552 Jun 18 '19

I also implemented a similar idea , it scraped data from imdb and would tell if the two actor/actress/director/any notable crew has ever worked together and list it.

6

u/supplefball Jun 18 '19

Do you still have the App? as an avid marijuana “enthusiast” I think this would be awesome!

46

u/[deleted] Jun 18 '19

First thing i thought of was RS too lol.

64

u/Dgc2002 Jun 18 '19

🦀JAGEX IS POWERLESS AGAINST INTERPRETED SNAKE LANGUAGE 🦀

12

u/OSRS_DabSlab Jun 18 '19

Fucking legend lol

1

u/Jaakko2000 Jun 20 '19

🦀🦀🦀 RUNELITE NEEDS SNAKE TALK SCRIPTING SUPPORT 🦀🦀🦀

7

u/JagexFlaym Jun 18 '19

🦀 gottem 🦀

68

u/[deleted] Jun 18 '19

I know, right? If anyone can teach you Python through a book format, it's Al Sweigart.

9

u/zeus5552 Jun 18 '19

And his cats :)

6

u/[deleted] Jun 18 '19

GREAT SCOTT! I FORGOT THE CATS!!

8

u/genuikav Jun 18 '19

Now this is awesome

4

u/zeus5552 Jun 18 '19

Thanks buddy, the book is great!!

5

u/Stabilo_0 Jun 18 '19

How do you spot red dots? I get the clicking part, but you have to find where to click first.

8

u/zeus5552 Jun 18 '19

In this particular case, the popups are not exactly red , there RGB values varies by 3-4 points around red so you can analyse the popups by cropping them and checking there exact pixel values and then writing the condition.

2

u/Stabilo_0 Jun 18 '19

Thank you, i saw a source code too.

12

u/[deleted] Jun 18 '19

take an image of the page, loop through the pixels and check if the color is red.

2

u/Stabilo_0 Jun 18 '19

Thanks for the responce

4

u/zita_1 Jun 18 '19

I think it's more convenient to use ImageGrab.grab(bbox=(x1,y1,x2,y2)) rather than capturing the hole screen then hiding the browser header and the taskbar

Here's my version of the code with 1920x1080 resolution:

import pyautogui,time
from PIL import ImageGrab

pyautogui.FAILSAFE = True
time.sleep(5)

while True:
    screen = ImageGrab.grab(bbox=(0,215,1915,1029))
    x,y = screen.size
    c_break = False
    for i in range (0,x,10):
        if c_break == True:
            break
        for j in range (0,y,10):
            r,g,b = screen.getpixel((i,j))
            if r == 255:
                pyautogui.click(i,j+215)
                c_break = True
                break

moving the mouse to the top left corner of the screen will stop the program when time is up.

3

u/zeus5552 Jun 18 '19

Great advice , I learned about that now, but I wrote the code after just 1 hour into pyautogui and was just excited to do something,but if I do a partial grab then the resulting image would not be in the native resolution and it would be too much hassle to adjust the clicking coordinates according to that.

3

u/zita_1 Jun 18 '19

That's not really a problem , you just need to add the distance between the top left corner of the screen and top left corner of the resulting image to the y coordinate in the click method.

In my case, as you see I added y1(215) to the 'j' in the click method and it's works flawlessly.

2

u/zeus5552 Jun 19 '19

Now I feel stupid 😂,it took me quite some time to understand the format of a box tuple so I took the lazy way.

4

u/[deleted] Jun 18 '19

This is impressive. I've been toiling away at the book for literally 4 years and the best I can do is some stupid random number guessing game on the command line. I feel like an idiot now. Thanks for posting this to remind me that I am not applying myself intelligently enough.

2

u/zeus5552 Jun 18 '19

Thanks a lot!. A piece of advice is to keep practicing because the adrenaline rush and sense of accomplishment you get once your program does what you set out to do is worth every trouble you face while writing it. For me it's specially when I manage to complete 3-4kyu katas on codewars.com.

3

u/pinaywdm Jun 18 '19

Nice! I'm still reading the Python Crash Course and will go through ATBS afterwards.

4

u/zeus5552 Jun 18 '19

Head first python is also a good book.

2

u/pinaywdm Jun 18 '19

Thank you! I'll keep it in mind 😊

2

u/[deleted] Jun 18 '19

PCC is amazing

1

u/pinaywdm Jun 19 '19

Lovin is so far :)

2

u/[deleted] Jun 18 '19

Great job! :)

1

u/zeus5552 Jun 18 '19

Thanks :)

2

u/[deleted] Jun 18 '19 edited Jun 18 '19

I tried it on the hardest difficulty (a shitton of tiny dots) for 30 seconds, but it wasn't quick enough. Also the skipping of 10 pixels made it miss some points and dialing it down to 1 pixel per iteration just made it way too slow.

Therefore I tried to use multithreading, which made it even slower but hey! - now I know when to not use Threads in Python. Then I came upon a very helpful forum thread (heh), that suggested multiprocessing instead! And now it works great!

Without your program I probably wouldn't have had the motivation to learn about that stuff, so thanks alot!

I split the screen into 4 equal areas and distributed them between 4 processes. The screen space and the duration of the processes are hard coded though. It's not pretty, but it works.

edit: I'm probably gonna try to not take entire screenshots everytime to optimize it.

    from PIL import Image
    import pyautogui
    import time
    from multiprocessing import Process
    pyautogui.FAILSAFE = True
    
    def clicker(startingPixel):#In seconds
        time.sleep(1)#Time to switch to https://www.mouseaccuracy.com/game
    
        initial = time.time()
    
        while True:                
            c_break = False
            pic = pyautogui.screenshot()
    
            for i in range(startingPixel,startingPixel+480,1):#Skip 1 pixels at each iter.
    
                if c_break == True: break
    
                for j in range(170,1040,1):
    
                    r,g,b = pic.getpixel((i,j))
    
                    if (r in range(245,254)):#RED
    
                        pyautogui.click(i,j)
                        c_break = True
                        break
    
            if time.time() - initial > 30: break
    
    if __name__ == "__main__":
        procs = []
        
        proc1 = Process(target=clicker, args=(0,))
        proc2 = Process(target=clicker, args=(480,))
        proc3 = Process(target=clicker, args=(960,))
        proc4 = Process(target=clicker, args=(1440,))
    
        procs.append(proc1)
        procs.append(proc2)
        procs.append(proc3)
        procs.append(proc4)
    
        proc1.start()
        proc2.start()
        proc3.start()
        proc4.start()
    
        for proc in procs:
            proc.join()

2

u/zeus5552 Jun 18 '19

As I said before, I was only 1 hour into pyautogui and pillow so my code is not much sophisticated, and for me it worked with 100% efficiency on insane difficulty (720p res) so I didn't have any motivation to improvise it.Splitting into 4 parts and using multiprocessing is a very awesome approach though :)

1

u/[deleted] Jun 19 '19

This is AWESOME! I was trying to optimize the code to run faster on my 1920x1080 monitor and i was getting like 30 clicks. Using multi threading im getting 100% on insane mode w tiny dots. Thank you so much for a great intro to multi threading

2

u/swordmasterman Jun 22 '19 edited Jun 30 '19

It's way too fast for my eyes. I watched a Instazood vs Jarvee video, and it was hard to follow it.

1

u/zeus5552 Jun 23 '19

Mine too

2

u/[deleted] Jun 24 '19

I just love this!

1

u/zeus5552 Jun 24 '19

Thanks mate:),how did you found this though,i thought it was buried too deep now to get comments.

1

u/[deleted] Jun 24 '19

I don’t know. I just appeared almost at the top of the r/python list.

1

u/zeus5552 Jun 24 '19

Never mind,I can be stupid at times

7

u/Sororita Jun 18 '19

Time to cheat on cookie clicker

6

u/oobrat2i30liga Jun 18 '19

Upload the code to github

4

u/robstersgaming Jun 18 '19

Make this so it clicks Csgos players heads thx

1

u/[deleted] Jun 18 '19

Oh that’s cool :D I made something like that too with cv2 and pygui

what packages did you use? Cause I’m pretty sure that cv2 wasn’t in automating boring stuff xD

1

u/zeus5552 Jun 18 '19

I used pillow with pyautogui :)

1

u/[deleted] Jun 18 '19

what api did you use for this?

2

u/zeus5552 Jun 18 '19

pillow and pyautogui module

1

u/[deleted] Jun 18 '19

thank you.

1

u/brand0n Jun 18 '19

the UI in this is very clean and neat................which I've never gotten w/python :D

tkinter was rather hard to use. QT was a bit easier but thats about the time I got a job using .net stuff

2

u/zeus5552 Jun 18 '19

Sorry bud, I didnt make the website, I just designed the bot to play it at superhuman speed.

2

u/brand0n Jun 18 '19

ah...this makes a lot more sense. Cool nonetheless :)

1

u/dukwrth Jun 19 '19

Nice! I just began reading the book a few days ago. Did you read/study anything else while going through ATBS, or did you focus solely on that? Also, did you have any prior Python/programming experience going into ATBS?

1

u/zeus5552 Jun 19 '19

I read only the book in a linear sequence,I do have a year worth of experience(very casual) in python, I worked as a freelancer on fiverr for paid projects and I have read a lot of other books,rn I am learning as many new modules as I can. And I do regularly solve challenges on codewars.com for practice.

4

u/Z_Zeay Jun 18 '19

Haha I love this, did something similar for Human benchmark thing. I've uploaded the script to gist:

https://gist.github.com/dybdaltech/67ba3404ae3a66eca5c038e5cf052121

0

u/[deleted] Jun 18 '19

[deleted]

3

u/flappableboi Jun 18 '19

Gotta love Selenium, mate. I try to use it to automate stuff everytime it's possible

0

u/[deleted] Jun 18 '19

[deleted]

3

u/Z_Zeay Jun 18 '19

Hehe, the lowest legit one I've seen so far is 119ms, which was done by my cousin.

2

u/Bloom_Kitty Jun 18 '19

Osu! wants to know your location.

2

u/trungvong Jun 19 '19

Nice project! I am reading that book as well. :))

2

u/shashank-py Jun 19 '19

In my case, I saw one video, creator of pyautogui and showed some cool stuff, then I tried to automate typing test ... Just like how you did .... Keep it up :)

1

u/0xeli Jun 18 '19

Anyone have experience putting something like this in a disconnected vm?

0

u/[deleted] Jun 18 '19

[deleted]

1

u/0xeli Jun 18 '19

If you don’t mind me asking how did you do it?

0

u/[deleted] Jun 18 '19

[deleted]

37

u/zeus5552 Jun 18 '19 edited Jun 18 '19

Sure,Its Not much sophisticated as I read about pyautogui an hour ago,and l hardcoded a lot of pixel dimensions for 1280x720 res , so without any customisation it will only work with 1280x720 res:

def Click_Bot(duration):#In seconds

from PIL import Image

import pyautogui

import time

time.sleep(5)#Time to switch to https://www.mouseaccuracy.com/game

im = Image.new('RGBA', (1280, 200), 'black')#A Black strip to cover the browser header

im2= Image.new('RGBA', (1280, 80), 'black')#A Black Strip to cover taskbar

initial = time.time()

while True:

c_break = False

pic = pyautogui.screenshot()

pic.paste(im,(0,0))

pic.paste(im2,(0,640))

x,y = pic.size

for i in range(0,x,10):#Skip 10 pixels at each iter.

if c_break:break

for j in range(200,y-80,10):

r,g,b = pic.getpixel((i,j))

if (r in range(245,254)):#RED

pyautogui.click(i,j)

c_break = True

break

if time.time()-initial>duration:break

2

u/ASCIASCI Jun 18 '19

Fun fact: when you use variables like “c_break == false”, in if statements those will read as booleans. So instead of writing “if c_break==True:”, you can just write “if c_break:”

1

u/zeus5552 Jun 18 '19

Updated it, I am aware of that it's just in my muscle memory :)

3

u/Jonno_FTW hisss Jun 18 '19

Nice work, in the future, don't do if c_break == True, skip the comparison to true and do if c_break

5

u/zeus5552 Jun 18 '19

I am aware of that , but it's kinda in my muscle memory now , I am obsessed with abridged code with comprehension,recursion,map(),filter(),decorators etc but I always forget to adapt to this simple thing.

-4

u/Sidwasnthere Jun 18 '19

I started learning programming with Python 3 a couple years ago and switched to mostly using C# about a year ish ago (game development). Now I get anxiety when I see things like while-loops with no braces and every line with no semi colon. I don’t have an opinion like “omg x programming language is the best”, just wanted to share my tangent

0

u/[deleted] Jun 18 '19

[deleted]

14

u/zeus5552 Jun 18 '19

Correct except last statement is on the indentation level of while loop and Thanks for the website as I was searching for something like that.

11

u/Ecopath Jun 18 '19

Hey man, thanks for sharing your stuff. It's cool!

If you're looking for an auto code formatter, check out the python package "black". It's a very efficient, bare bones formatter, I love it.

6

u/[deleted] Jun 18 '19

I love it except for its use of double quotes. But I can deal with that. As someone who usually hates opinionated autolinters, I make an exception for Black.

3

u/PaulSandwich Jun 18 '19

I agree, but it's hard to argue against the fact that you'll always spend more time escaping single quotes. Hell, I used one typing this sentence.

2

u/[deleted] Jun 18 '19

I don't care about that, the shift key slows me down.

0

u/non_NSFW_acc Jun 19 '19 edited Jun 19 '19

Ah yes. The one 1/4th of a second it takes to type “ using shift each time is tremendous. /s

0

u/[deleted] Jun 19 '19

[deleted]

→ More replies (0)

0

u/fleyk-lit Jun 19 '19

So couldn't you fix your own post?

1

u/zeus5552 Jun 19 '19

Nah I am bit stupid.

0

u/[deleted] Jun 19 '19

[deleted]

1

u/zeus5552 Jun 19 '19

Thanks buddy, I will surely do it next time,but as this good stranger above has already done it for me ,I won't steal his comment :)

3

u/Ek_Los_Die_Hier Jun 18 '19

/u/zeus5552 you could try using numpy. Convert the image to a 3D array of pixels, extract the red pixels and find the index of the maximum value:

pix = numpy.array(pic)
red_pix = pix[:, :, 0]
i, j = numpy.argmax(red_pix, axis=1)
pyautogui.click(i, j) 

This is not tested code but shouldn't be too far off from working (on my phone). Should be a lot faster I reckon.

2

u/zeus5552 Jun 18 '19

Awesome advice and approach, but my approach never scans all the pixels as it will quit searching once a red pixel is found and will update the screenshot and do it all over again,but I could try this but my concern being that if I keep the image static and scan all the pixels then the lower bubbles might fade away in that time, I am most probably wrong so I will try this for sure.Frankly I would have done more optimisation if the current code missed even a single bubble on insane difficulty but it didn't so I didn't have any motivation to improvise it.

1

u/woanders Jun 18 '19

Good bot.

1

u/badpotato Jun 22 '19

Or use a gist.

1

u/appinv Python&OpenSource Oct 26 '19

Awesome. Very nice logic!

0

u/[deleted] Jun 18 '19

[deleted]

1

u/zeus5552 Jun 18 '19

I implemented it as fail-safe but forgot to remove it as the bot passed the hardest difficulty on that website and then left the code as it was, but you are right there is always room for improvement :)

0

u/[deleted] Jun 18 '19

[deleted]

0

u/[deleted] Jun 19 '19

[deleted]

1

u/zeus5552 Jun 19 '19

I love code bullet too, his videos are great.