import argparse,pathlib,subprocess,time,re
p=argparse.ArgumentParser();p.add_argument('session');p.add_argument('output');a=p.parse_args()
out=pathlib.Path(a.output)
def call(*args):
 return subprocess.check_output(['agent-device',*args,'--session',a.session],text=True)
def toggle():
 tree=call('snapshot','-i')
 ref=re.search(r'(@e\d+) \[button\] "Worked for',tree).group(1)
 call('press',ref)
tree=call('snapshot','-i')
if 'Worked for' not in tree: raise RuntimeError('Open the finished thread before recording')
call('record','start',str(out.with_suffix('.mp4')))
try:
 time.sleep(2)
 toggle();time.sleep(2)
 toggle();time.sleep(2)
finally:
 print(call('record','stop'))
