-
Notifications
You must be signed in to change notification settings - Fork 1
/
chromeplay.py
executable file
·98 lines (84 loc) · 2.58 KB
/
chromeplay.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env python
# Copyright, 2014 Stefano Rivera.
# Distributed under the ISC license, see LICENSE.rst
import argparse
import curses
import errno
import time
import socket
import ssl
import pychromecast
def main():
p = argparse.ArgumentParser()
p.add_argument('URL')
args = p.parse_args()
play(args.URL)
def play(url):
while True:
try:
cast = pychromecast.get_chromecast()
while not cast.is_idle:
cast.quit_app()
time.sleep(1)
time.sleep(1)
cast.play_media(url, "video/mp4")
except socket.error as e:
if e.errno != errno.EFAULT:
raise
except ssl.SSLError as e:
pass
else:
break
control_loop(cast.media_controller)
cast.quit_app()
def control_loop(mc):
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.keypad(True)
stdscr.nodelay(True)
try:
while True:
c = stdscr.getch()
if c == curses.ERR:
pass
elif c == ord(' '):
if mc.status.player_state == 'PAUSED':
mc.play()
else:
mc.pause()
elif c == ord('q'):
mc.stop()
break
elif c == curses.KEY_RIGHT:
mc.seek(mc.status.current_time + 10)
elif c == curses.KEY_LEFT:
mc.seek(max(mc.status.current_time - 10, 0))
elif c == curses.KEY_UP:
mc.seek(mc.status.current_time + 60)
elif c == curses.KEY_DOWN:
mc.seek(max(mc.status.current_time - 60, 0))
elif c == curses.KEY_PPAGE:
mc.seek(mc.status.current_time + 600)
elif c == curses.KEY_NPAGE:
mc.seek(max(mc.status.current_time - 600, 0))
if mc.status:
stdscr.addstr(0, 0, mc.status.player_state)
stdscr.clrtoeol()
minutes, seconds = divmod(mc.status.current_time, 60)
hours, minutes = divmod(minutes, 60)
stdscr.addstr(1, 0, "%02i:%02i:%02i"
% (hours, minutes, seconds))
if mc.status.player_state == 'IDLE':
break
mc.update_status()
stdscr.move(2, 0)
stdscr.refresh()
time.sleep(1)
finally:
curses.nocbreak()
stdscr.keypad(False)
curses.echo()
curses.endwin()
if __name__ == '__main__':
main()