forked from stigger/trakt-for-appletv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
55 lines (47 loc) · 1.56 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import asyncio
import sys
from helpers.graceful_exit import GracefulExit, cancel_tasks
from scrobbling_protocol import ScrobblingProtocol
async def _play_handler():
"""The main task initializes the pyatv library and starts the scrobbling protocol."""
listener = ScrobblingProtocol()
try:
await listener.setup()
finally: # pragma: no cover
if not listener.is_setup:
await listener.print("\nSetup cancelled, shutting down...")
await listener.shutdown()
return 1
# sleep forever by 1 hour intervals,
# on Windows before Python 3.8 wake up every 1 second to handle
# Ctrl+C smoothly
try:
if sys.platform == "win32" and sys.version_info < (3, 8):
delay = 1
else:
delay = 3600
while True:
await asyncio.sleep(delay)
finally:
await listener.print("\nShutdown requested, cleaning up...")
await listener.cleanup()
def main():
"""Application start here."""
loop = asyncio.get_event_loop()
main_task = loop.create_task(
_play_handler()
)
try:
asyncio.set_event_loop(loop)
loop.run_until_complete(main_task)
except (GracefulExit, KeyboardInterrupt): # pragma: no cover
pass
finally:
cancel_tasks({main_task}, loop)
cancel_tasks(asyncio.all_tasks(loop), loop)
loop.run_until_complete(loop.shutdown_asyncgens())
loop.close()
asyncio.set_event_loop(None)
print("Shutdown complete.")
if __name__ == "__main__":
sys.exit(main())