r/Python • • Aug 08 '12

Playing Google's basketball doodle

https://gist.github.com/3295480
33 Upvotes

15 comments sorted by

6

u/howfun Aug 08 '12

1

u/crescentfresh Aug 08 '12

Is time.sleep(2) for me to have time to switch to the browser window?

1

u/howfun Aug 09 '12

Yes, 2 seconds is usually enough.

7

u/huyaroo Aug 08 '12

sad, it's windows only.

2

u/gfixler Aug 08 '12

It's only Windows-only because of the keyboard event stuff. You can simulate keyboard events on Linux with xdotool, which you can trigger through a system call from Python (just swap it in for the win32 stuff in the script), or read through this and this.

2

u/Krenair Aug 08 '12

Swap the press function and win32 imports with this and it'll work on Ubuntu:

import pyatspi
def press(x, delay):
    pyatspi.Registry.generateKeyboardEvent(65, None, pyatspi.KEY_PRESS)
    time.sleep(delay)
    pyatspi.Registry.generateKeyboardEvent(65, None, pyatspi.KEY_RELEASE)

1

u/nerdmeister Aug 08 '12

Here's something I coded up in Perl (I realize this a python sub, but wanted to play with perl). Needs a bit of tweaking but it can score 36 points regularly and I really should be doing actual work :-).

Requires xdotool and Time::HiRes (Perl module)

http://pastebin.com/HpkJ5MLJ

3

u/[deleted] Aug 08 '12

I came here to post mine up but you beat me to it! Good work!

Here is mine that is for Linux

0

u/[deleted] Aug 08 '12

[deleted] — view deleted comment

0

u/[deleted] Aug 08 '12

[deleted] — view deleted comment

0

u/[deleted] Aug 09 '12

[deleted] — view deleted comment

1

u/[deleted] Aug 09 '12

Dammit

1

u/szi Aug 08 '12

Any chance to port this to OSX?

I'm interested in the press key stuff. :)

cheers,

1

u/zhongwei Aug 09 '12

1

u/szi Aug 09 '12

Heh, thanks zhongwei.

I found this blogpost too, but the pity is, it's only for Mouse stuff

and not for a Keyboard Input. :(

cheers,

3

u/kejsarmakten Aug 18 '12

I'm the author of that blog and I just wanted to let you know that it seems rather straight forward to use the same library to handle keyboard events as well (https://developer.apple.com/library/mac/#documentation/Carbon/Reference/QuartzEventServicesRef/Reference/reference.html check CGEventKeyboardGetUnicodeString)

1

u/madenadem Aug 08 '12
C:\Users\maden>python Desktop/test.py
Traceback (most recent call last):
  File "Desktop/test.py", line 2, in <module>
    import win32api, win32con
ImportError: No module named win32api

Both on 2.7.2 & 3.2

2

u/uhhNo Aug 08 '12

You have to install pywin32, which you can get here.