#!python
from time import sleep
from dasbus.connection import SessionMessageBus

MESSAGE_BUS = SessionMessageBus()

SCREENCAST_PROXY_OBJECT = MESSAGE_BUS.get_proxy(
    service_name="org.gnome.Shell.Screencast",
    object_path="/org/gnome/Shell/Screencast"
)

for _ in range(5):
    try:
        SCREENCAST_PROXY_OBJECT.Screencast("/home/test/Videos/Screencast.webm", {})
        break
    except Exception as error:
        # Exception can happen when session is not fully loaded - but we can give it more tries.
        print(f"Screencast failed: '{error}'. Trying again...")
        sleep(1)

sleep(10000)
