I recently implemented similar functionality in Python, I haven't open sourced it (yet) but I can share some code if you want.