Skip to content

Instantly share code, notes, and snippets.

@vickyg3
vickyg3 / snapshot.py
Created August 8, 2011 18:15
Take Snapshot
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
filename = '1.png'
device = MonkeyRunner.waitForConnection()
device.takeSnapshot().getSubImage((10, 10, 220, 40)).writeToFile(filename, 'png')
@vickyg3
vickyg3 / ocr.sh
Created August 8, 2011 18:21
PNG Conversion and OCR
#! /bin/bash
pngtopnm $1 |
ocrad --format=utf8 --filter=numbers |
sed -r -e 's/x/\*/' -e 's/÷/\//' -e 's/\_([0-9]+)/int\(math\.sqrt\(\1\)\)/' |
tr -d '\n' |
tr -d ' '
@vickyg3
vickyg3 / result_eval.py
Created August 8, 2011 18:25
Result Evaluation
percent = re.compile(r'([0-9]+)\%of([0-9]+)')
question = os.popen("./ocr.sh %s" % filename).read()
if '+' not in question and '-' not in question and '/' not in question and '*' not in question and '%' not in question and 'i' not in question:
result = int(question[0:-1]) * int(question[0:-1])
elif '%' in question:
result = int(percent.search(question).group(1)) * int(percent.search(question).group(2)) / 100;
else:
result = eval(question)
@vickyg3
vickyg3 / gist:1132371
Created August 8, 2011 18:30
Touch simulation
result_str = str(result)
for ch in result_str:
device.touch(getx(ch), gety(ch), 'DOWN_AND_UP')
@vickyg3
vickyg3 / auto_math_workout.py
Created August 8, 2011 18:33
Hacking Math Workout with OCR
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import os
import time
import math
import re
def getx(n):
if n in ('1', '4', '7'):
return 50
elif n in ('2', '5', '8', '0'):
@vickyg3
vickyg3 / kid_vs_programmer.py
Created April 8, 2012 09:56
kid vs programmer
#! /usr/bin/python
import numpy.linalg as lin
def get_values(s):
lhs, rhs = s.split("=")
sparts = map(int, lhs)
freq = dict((i, sparts.count(i)) for i in set(sparts))
return ([freq.get(i,0) for i in range(10)], int(rhs))
@vickyg3
vickyg3 / sample_controller.py
Created June 23, 2012 16:32
Sample monkeyrunner code
#! /path_to_sdk/tools/monkeyrunner
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
print 'waiting for device'
device = MonkeyRunner.waitForConnection()
print 'device attached'
# the body will probably go like this
while True:
@vickyg3
vickyg3 / temple_runner.py
Created June 23, 2012 16:42
Temple runner controller
#! /home/vignesh/android-sdk-linux/tools/monkeyrunner
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
import sys
def jump():
global device
device.drag((400,900), (400, 800), 0.25)
def duck():