-
Notifications
You must be signed in to change notification settings - Fork 1
/
mpvrpc.py
100 lines (87 loc) · 3.29 KB
/
mpvrpc.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
99
100
# Copyright 2018 goppacode (Michael Rosenthal)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import socket
from time import sleep
SOCKET_NAME = "/tmp/canito.socket"
TMUX_SESSION = "canito"
class MPV:
def __enter__(self):
self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
return self
def __exit__(self, *args):
self.s.__exit__(*args)
def connect_player(self):
try:
self.s.connect(SOCKET_NAME)
return True
except FileNotFoundError:
return False
except ConnectionRefusedError:
return False
def spawn_new_player(self):
"""
Start a tmux session with mpv running and connect to the socket
"""
tmux_invocation = [
"tmux",
"new-session",
"-s" + TMUX_SESSION,
"-d",
"mpv",
"--no-video",
"--idle",
"--input-ipc-server=" + SOCKET_NAME,
]
# start tmux with mpv and wait until ready
os.spawnvp(os.P_WAIT, tmux_invocation[0], tmux_invocation)
while not self.connect_player():
sleep(0.1)
def append_to_playlist(self, result):
result_msg = self.prettify_result(result)
MPV_Message(self.s).loadfile(result.location, "append-play", "term-playing-msg=\"{}\"".format(result_msg))
MPV_Message(self.s).print_text("Added to playlist: {}".format(result_msg))
def prettify_result(self, result):
if result.artist is None:
return "'{}'".format(result.name)
return "'{}' by '{}'".format(result.name, result.artist)
class MPV_Message:
def __init__(self, s):
self.s = s
def __getattr__(self, command):
def recieve():
# Warning: may steal reponses of another process
# May throw up hands if the reponse is not a single result json
response = bytearray()
while response.find(b'\n') < 0:
response.extend(self.s.recv(16))
result_bytes, _, _ = response.partition(b'\n')
result = json.loads(result_bytes)
# swallow anything that is not a result
if not "error" in result:
return recieve()
return result["error"] == 'success'
def send(*param):
command_hyphenated = command.replace('_', '-')
cmd = MPV_Message._construct_command(self, command_hyphenated, *param, request_id=None)
self.s.sendall(cmd.encode())
return recieve()
return send
@staticmethod
def _construct_command(self, *args, request_id):
message = {"command": args}
if request_id:
message["request_id"] = request_id
return json.dumps(message) + "\n"