mirror of
https://github.com/beetbox/beets.git
synced 2025-12-09 10:05:35 +01:00
159 lines
4.5 KiB
Python
Executable file
159 lines
4.5 KiB
Python
Executable file
#!/usr/bin/env python
|
|
|
|
# This file is part of beets.
|
|
# Copyright 2009, Adrian Sampson.
|
|
#
|
|
# Beets is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# Beets is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with beets. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
from optparse import OptionParser
|
|
from beets import Library
|
|
from beets import autotag
|
|
from ConfigParser import SafeConfigParser
|
|
import os
|
|
|
|
CONFIG_DEFAULTS = {
|
|
# beets
|
|
'library': 'library.blb',
|
|
|
|
# bpd
|
|
'host': '',
|
|
'port': '6600',
|
|
'password': '',
|
|
}
|
|
|
|
CONFIG_FILE = os.path.expanduser('~/.beetsrc')
|
|
|
|
def _print(txt):
|
|
"""Print the text encoded using UTF-8."""
|
|
print txt.encode('utf-8')
|
|
|
|
def add(lib, config, paths):
|
|
for path in paths:
|
|
lib.add(path)
|
|
lib.save()
|
|
|
|
def ls(lib, config, criteria):
|
|
q = ' '.join(criteria)
|
|
if not q.strip():
|
|
q = None # no criteria => match anything
|
|
for item in lib.get(q):
|
|
_print(item.artist + ' - ' + item.album + ' - ' + item.title)
|
|
|
|
def imp(lib, config, paths):
|
|
for path in paths:
|
|
lib.add(path, copy=True)
|
|
lib.save()
|
|
|
|
def option(lib, config, options):
|
|
(key, value) = options
|
|
lib.options[key] = value
|
|
lib.save()
|
|
|
|
def remove(lib, config, criteria):
|
|
q = ' '.join(criteria)
|
|
if not q.strip():
|
|
raise ValueError('must provide some criteria for removing')
|
|
for item in lib.get(q):
|
|
_print("removing " + item.path)
|
|
item.remove()
|
|
lib.save()
|
|
|
|
def delete(lib, config, criteria):
|
|
q = ' '.join(criteria)
|
|
if not q.strip():
|
|
raise ValueError('must provide some criteria for deleting')
|
|
for item in lib.get(q):
|
|
_print("deleting " + item.path)
|
|
item.delete()
|
|
lib.save()
|
|
|
|
def read(lib, config, criteria):
|
|
q = ' '.join(criteria)
|
|
if not q.strip():
|
|
q = None
|
|
for item in lib.get(q):
|
|
item.read()
|
|
item.store()
|
|
lib.save()
|
|
|
|
def tagalbum(lib, config, paths):
|
|
for path in paths:
|
|
autotag.tag_album_dir(os.path.expanduser(path), lib)
|
|
lib.save()
|
|
|
|
def bpd(lib, config, opts):
|
|
host = opts.pop(0) if opts else None
|
|
host = host if host else config.get('bpd', 'host')
|
|
port = int(opts.pop(0)) if opts else None
|
|
port = port if port else config.getint('bpd', 'port')
|
|
password = config.get('bpd', 'password')
|
|
|
|
from beets.player.bpd import Server
|
|
Server(lib, host, port, password).run()
|
|
|
|
if __name__ == "__main__":
|
|
# parse options
|
|
usage = """usage: %prog [options] command
|
|
command is one of: add, remove, update, write, list, bpd, tagalbum, help"""
|
|
op = OptionParser(usage=usage)
|
|
op.add_option('-l', '--library', dest='libpath', metavar='PATH',
|
|
default=None,
|
|
help='work on the specified library file')
|
|
op.remove_option('--help')
|
|
opts, args = op.parse_args()
|
|
|
|
# make sure we have a command
|
|
if len(args) < 1:
|
|
op.error('no command specified')
|
|
cmd = args.pop(0)
|
|
|
|
# read defaults from config file
|
|
config = SafeConfigParser(CONFIG_DEFAULTS)
|
|
config.read(CONFIG_FILE)
|
|
for sec in ('beets', 'bpd'):
|
|
if not config.has_section(sec):
|
|
config.add_section(sec)
|
|
if not opts.libpath:
|
|
opts.libpath = config.get('beets', 'library')
|
|
|
|
lib = Library(os.path.expanduser(opts.libpath))
|
|
|
|
# make a "help" command
|
|
def help(*args): op.print_help()
|
|
|
|
# choose which command to invoke
|
|
avail_commands = [
|
|
(add, ['add']),
|
|
(imp, ['import', 'im', 'imp']),
|
|
(remove, ['remove', 'rm']),
|
|
(delete, ['delete', 'del']),
|
|
(tagalbum, ['tagalbum', 'tag']),
|
|
|
|
(read, ['read', 'r']),
|
|
#(write, ['write', 'wr', 'w']),
|
|
|
|
(ls, ['list', 'ls']),
|
|
|
|
(option, ['set']),
|
|
(help, ['help', 'h']),
|
|
|
|
(bpd, ['bpd']),
|
|
]
|
|
for test_command in avail_commands:
|
|
if cmd in test_command[1]:
|
|
(test_command[0])(lib, config, args)
|
|
op.exit()
|
|
|
|
# no command matched
|
|
op.error('invalid command "' + cmd + '"')
|