Scripting an Ice Cream Delivery with the Android SDK
Today, a smartphone-connected taxi company called Uber is operating five ice cream trucks in Seattle. You click a button in the Uber app and select your location, and an ice cream truck comes to you. Or at least that’s what should happen. Even with today’s weather (read: periodic torrential rain), people are clamoring to get their ice cream due to the fact that it’s only available for one day… meaning almost nobody is actually able to get a spot in the queue. Uber’s Twitter feed is full of advice telling customers just keep hitting the button, but man, I have work to do. I can’t sit here and press the little ice cream icon all day. Something has got to give.
The Android SDK contains a tool called monkeyrunner. Monkeyrunner lets you simulate touch events, take screenshots, etc… and it does it through a very simple Python library. I decided to see if I could write a little script to keep pressing the ice cream button in Uber’s app, and it only took a few minutes.
from time import sleep from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice # Attach to Android device d = MonkeyRunner.waitForConnection() # Take a screenshot of the "sorry, keep trying" screen for reference raw_input("Get to the error screen, then hit enter.\n") oldpic = d.takeSnapshot() while True: # Touch the ice cream button d.touch(540, 200, MonkeyDevice.DOWN_AND_UP) sleep(4) newpic = d.takeSnapshot() # If the screen looks like our old error # screen (95%), hit OK and just keep going (in 10 seconds) if newpic.sameAs(oldpic, .95): d.touch(460, 700, MonkeyDevice.DOWN_AND_UP) sleep(10) # But if the screen has changed, stop running the loop. else: break
I still don’t have ice cream, but the script is running and working.