Archive for November, 2008

Facebook Word Challenge: cracked!

Friday, November 28th, 2008

And now a small script which tries to score the world record for ‘Word Challenge’, one of the games you can play on Facebook.
In order to use this hack, you have to install python and python-xlib packages, then run:

$ python facebook.py /path/to/dictionary

where facebook.py is the script down here.

On startup you’ll be prompted with a ‘?’ indicating that the script is waiting for the six letters of the game; once you have entered them (followed by the ‘Return’ button), you have to give the focus to the browser window by clicking with your mouse over the flash game: now you’ll se the score growing and growing again.
That’s all: once you have cleared the ‘level’, you have to type the new 6 letters inside the terminal window, and give the focus to the browser again as stated before.
It’s easy isn’t it? A friend of mine scored 195k points, but it depends on the dictionary used. Here is an italian good one [1].
“Stop explaining, we need the code!”.. here it is.

#!/usr/bin/env python

import sys
import time
import Xlib.display
import Xlib.ext
import Xlib.ext.xtest

MIN = 3
MAX = 6
keycode = {
	'a': 38, 'b': 56, 'c': 54, 'd': 40, 'e': 26, 'f': 41, 'g': 42,
	'h': 43, 'i': 31, 'j': 44, 'k': 45, 'l': 46, 'm': 58, 'n': 57,
	'o': 32, 'p': 33, 'q': 24, 'r': 27, 's': 39, 't': 28, 'u': 30,
	'v': 55, 'w': 25, 'x': 53, 'y': 29, 'z': 52, 'cr': 36
}

class Node:
	def __init__(self, value):
		self.value = value
		self.end = False
		self.child = {}

class PTree:
	def __init__(self, parent, alphabet, result):
		self.dtree = parent
		self.root = Node('')
		self.create_tree(alphabet, self.root, '', result)

	def create_tree(self, alphabet, node, word, result):
		if len(word) >= MIN:
			if self.dtree.lookup(word):
				result[word] = True
		for i in range(len(alphabet)):
			node.child[i] = Node(alphabet[i])
			self.create_tree(alphabet[:i] + alphabet[i + 1:],
					node.child[i], word + alphabet[i],
					result)

class DTree:
	def __init__(self, filename):
		self.root = Node('')
		for line in open(filename):
			word = line.strip()
			if len(word) < MIN or len(word) > MAX or
				    not word.islower() or not word.isalpha():
				continue
			self.create_tree(word, self.root)

	def create_tree(self, word, node):
		if len(word) == 0:
			node.end = True
			return
		i = ord(word[0]) - ord('a')
		if not node.child.has_key(i):
			node.child[i] = Node(word[0])
		self.create_tree(word[1:], node.child[i])

	def lookup(self, word, node=None):
		if node is None:
			node = self.root
		if len(word) == 0:
			if node.end:
				return True
			return False
		i = ord(word[0]) - ord('a')
		if node.child.has_key(i):
			return self.lookup(word[1:], node.child[i])
		return False

def fake_input(word):
	for c in key:
		Xlib.ext.xtest.fake_input(display,
				Xlib.X.KeyPress,
				keycode[c])
		Xlib.ext.xtest.fake_input(display,
				Xlib.X.KeyRelease,
				keycode[c])
	Xlib.ext.xtest.fake_input(display,
			Xlib.X.KeyPress,
			keycode['cr'])
	Xlib.ext.xtest.fake_input(display,
			Xlib.X.KeyRelease,
			keycode['cr'])
	display.sync()

if __name__ == "__main__":
	if len(sys.argv) == 1:
		print 'Usage: python facebook.py /path/to/dictionary'
		sys.exit(1)
	display = Xlib.display.Display()
	dtree = DTree(sys.argv[1])
	while True:
		result = {}
		ptree = PTree(dtree, raw_input('? '), result)
		time.sleep(2)
		for key in result.keys():
			fake_input(key)
			print key

Gcalendar

Sunday, November 9th, 2008

GCalendar is a small port to python+gtk of the old XCalendar tool.It’s a simple interactive calendar program with notebook capability.
The directory used for the stored annotations is ~/Calendar.

You can take the sources from the repository. Project name: gcalendar