forked from beetbox/beets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpdupdate.py
129 lines (107 loc) · 4.03 KB
/
mpdupdate.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# This file is part of beets.
# Copyright 2016, Adrian Sampson.
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
"""Updates an MPD index whenever the library is changed.
Put something like the following in your config.yaml to configure:
mpd:
host: localhost
port: 6600
password: seekrit
"""
import os
import socket
from beets import config
from beets.plugins import BeetsPlugin
# No need to introduce a dependency on an MPD library for such a
# simple use case. Here's a simple socket abstraction to make things
# easier.
class BufferedSocket:
"""Socket abstraction that allows reading by line."""
def __init__(self, host, port, sep=b"\n"):
if host[0] in ["/", "~"]:
self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.sock.connect(os.path.expanduser(host))
else:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((host, port))
self.buf = b""
self.sep = sep
def readline(self):
while self.sep not in self.buf:
data = self.sock.recv(1024)
if not data:
break
self.buf += data
if self.sep in self.buf:
res, self.buf = self.buf.split(self.sep, 1)
return res + self.sep
else:
return b""
def send(self, data):
self.sock.send(data)
def close(self):
self.sock.close()
class MPDUpdatePlugin(BeetsPlugin):
def __init__(self):
super().__init__()
config["mpd"].add(
{
"host": os.environ.get("MPD_HOST", "localhost"),
"port": int(os.environ.get("MPD_PORT", 6600)),
"password": "",
}
)
config["mpd"]["password"].redact = True
# For backwards compatibility, use any values from the
# plugin-specific "mpdupdate" section.
for key in config["mpd"].keys():
if self.config[key].exists():
config["mpd"][key] = self.config[key].get()
self.register_listener("database_change", self.db_change)
def db_change(self, lib, model):
self.register_listener("cli_exit", self.update)
def update(self, lib):
self.update_mpd(
config["mpd"]["host"].as_str(),
config["mpd"]["port"].get(int),
config["mpd"]["password"].as_str(),
)
def update_mpd(self, host="localhost", port=6600, password=None):
"""Sends the "update" command to the MPD server indicated,
possibly authenticating with a password first.
"""
self._log.info("Updating MPD database...")
try:
s = BufferedSocket(host, port)
except OSError as e:
self._log.warning("MPD connection failed: {0}", str(e.strerror))
return
resp = s.readline()
if b"OK MPD" not in resp:
self._log.warning("MPD connection failed: {0!r}", resp)
return
if password:
s.send(b'password "%s"\n' % password.encode("utf8"))
resp = s.readline()
if b"OK" not in resp:
self._log.warning("Authentication failed: {0!r}", resp)
s.send(b"close\n")
s.close()
return
s.send(b"update\n")
resp = s.readline()
if b"updating_db" not in resp:
self._log.warning("Update failed: {0!r}", resp)
s.send(b"close\n")
s.close()
self._log.info("Database updated.")