mirror of
https://github.com/rembo10/headphones.git
synced 2026-07-21 00:14:02 +01:00
update Beets
This commit is contained in:
+73
-51
@@ -15,55 +15,73 @@
|
||||
"""
|
||||
|
||||
|
||||
from beets import ui, util, library, config
|
||||
from beets.plugins import BeetsPlugin
|
||||
|
||||
import subprocess
|
||||
import shutil
|
||||
import os
|
||||
import shutil
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
from beets import config, library, ui, util
|
||||
from beets.plugins import BeetsPlugin
|
||||
from beets.util import syspath
|
||||
|
||||
|
||||
class IPFSPlugin(BeetsPlugin):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.config.add({
|
||||
'auto': True,
|
||||
'nocopy': False,
|
||||
})
|
||||
self.config.add(
|
||||
{
|
||||
"auto": True,
|
||||
"nocopy": False,
|
||||
}
|
||||
)
|
||||
|
||||
if self.config['auto']:
|
||||
if self.config["auto"]:
|
||||
self.import_stages = [self.auto_add]
|
||||
|
||||
def commands(self):
|
||||
cmd = ui.Subcommand('ipfs',
|
||||
help='interact with ipfs')
|
||||
cmd.parser.add_option('-a', '--add', dest='add',
|
||||
action='store_true',
|
||||
help='Add to ipfs')
|
||||
cmd.parser.add_option('-g', '--get', dest='get',
|
||||
action='store_true',
|
||||
help='Get from ipfs')
|
||||
cmd.parser.add_option('-p', '--publish', dest='publish',
|
||||
action='store_true',
|
||||
help='Publish local library to ipfs')
|
||||
cmd.parser.add_option('-i', '--import', dest='_import',
|
||||
action='store_true',
|
||||
help='Import remote library from ipfs')
|
||||
cmd.parser.add_option('-l', '--list', dest='_list',
|
||||
action='store_true',
|
||||
help='Query imported libraries')
|
||||
cmd.parser.add_option('-m', '--play', dest='play',
|
||||
action='store_true',
|
||||
help='Play music from remote libraries')
|
||||
cmd = ui.Subcommand("ipfs", help="interact with ipfs")
|
||||
cmd.parser.add_option(
|
||||
"-a", "--add", dest="add", action="store_true", help="Add to ipfs"
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
"-g", "--get", dest="get", action="store_true", help="Get from ipfs"
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
"-p",
|
||||
"--publish",
|
||||
dest="publish",
|
||||
action="store_true",
|
||||
help="Publish local library to ipfs",
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
"-i",
|
||||
"--import",
|
||||
dest="_import",
|
||||
action="store_true",
|
||||
help="Import remote library from ipfs",
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
"-l",
|
||||
"--list",
|
||||
dest="_list",
|
||||
action="store_true",
|
||||
help="Query imported libraries",
|
||||
)
|
||||
cmd.parser.add_option(
|
||||
"-m",
|
||||
"--play",
|
||||
dest="play",
|
||||
action="store_true",
|
||||
help="Play music from remote libraries",
|
||||
)
|
||||
|
||||
def func(lib, opts, args):
|
||||
if opts.add:
|
||||
for album in lib.albums(ui.decargs(args)):
|
||||
if len(album.items()) == 0:
|
||||
self._log.info('{0} does not contain items, aborting',
|
||||
album)
|
||||
self._log.info(
|
||||
"{0} does not contain items, aborting", album
|
||||
)
|
||||
|
||||
self.ipfs_add(album)
|
||||
album.store()
|
||||
@@ -96,7 +114,7 @@ class IPFSPlugin(BeetsPlugin):
|
||||
|
||||
jlib = self.get_remote_lib(lib)
|
||||
player = PlayPlugin()
|
||||
config['play']['relative_to'] = None
|
||||
config["play"]["relative_to"] = None
|
||||
player.album = True
|
||||
player.play_music(jlib, player, args)
|
||||
|
||||
@@ -107,15 +125,15 @@ class IPFSPlugin(BeetsPlugin):
|
||||
return False
|
||||
try:
|
||||
if album.ipfs:
|
||||
self._log.debug('{0} already added', album_dir)
|
||||
self._log.debug("{0} already added", album_dir)
|
||||
# Already added to ipfs
|
||||
return False
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
self._log.info('Adding {0} to ipfs', album_dir)
|
||||
self._log.info("Adding {0} to ipfs", album_dir)
|
||||
|
||||
if self.config['nocopy']:
|
||||
if self.config["nocopy"]:
|
||||
cmd = "ipfs add --nocopy -q -r".split()
|
||||
else:
|
||||
cmd = "ipfs add -q -r".split()
|
||||
@@ -123,7 +141,7 @@ class IPFSPlugin(BeetsPlugin):
|
||||
try:
|
||||
output = util.command_output(cmd).stdout.split()
|
||||
except (OSError, subprocess.CalledProcessError) as exc:
|
||||
self._log.error('Failed to add {0}, error: {1}', album_dir, exc)
|
||||
self._log.error("Failed to add {0}, error: {1}", album_dir, exc)
|
||||
return False
|
||||
length = len(output)
|
||||
|
||||
@@ -164,21 +182,26 @@ class IPFSPlugin(BeetsPlugin):
|
||||
cmd.append(_hash)
|
||||
util.command_output(cmd)
|
||||
except (OSError, subprocess.CalledProcessError) as err:
|
||||
self._log.error('Failed to get {0} from ipfs.\n{1}',
|
||||
_hash, err.output)
|
||||
self._log.error(
|
||||
"Failed to get {0} from ipfs.\n{1}", _hash, err.output
|
||||
)
|
||||
return False
|
||||
|
||||
self._log.info('Getting {0} from ipfs', _hash)
|
||||
imp = ui.commands.TerminalImportSession(lib, loghandler=None,
|
||||
query=None, paths=[_hash])
|
||||
self._log.info("Getting {0} from ipfs", _hash)
|
||||
imp = ui.commands.TerminalImportSession(
|
||||
lib, loghandler=None, query=None, paths=[_hash]
|
||||
)
|
||||
imp.run()
|
||||
shutil.rmtree(_hash)
|
||||
# This uses a relative path, hence we cannot use util.syspath(_hash,
|
||||
# prefix=True). However, that should be fine since the hash will not
|
||||
# exceed MAX_PATH.
|
||||
shutil.rmtree(syspath(_hash, prefix=False))
|
||||
|
||||
def ipfs_publish(self, lib):
|
||||
with tempfile.NamedTemporaryFile() as tmp:
|
||||
self.ipfs_added_albums(lib, tmp.name)
|
||||
try:
|
||||
if self.config['nocopy']:
|
||||
if self.config["nocopy"]:
|
||||
cmd = "ipfs add --nocopy -q ".split()
|
||||
else:
|
||||
cmd = "ipfs add -q ".split()
|
||||
@@ -236,7 +259,7 @@ class IPFSPlugin(BeetsPlugin):
|
||||
return False
|
||||
|
||||
def ipfs_list(self, lib, args):
|
||||
fmt = config['format_album'].get()
|
||||
fmt = config["format_album"].get()
|
||||
try:
|
||||
albums = self.query(lib, args)
|
||||
except OSError:
|
||||
@@ -260,8 +283,7 @@ class IPFSPlugin(BeetsPlugin):
|
||||
return library.Library(path)
|
||||
|
||||
def ipfs_added_albums(self, rlib, tmpname):
|
||||
""" Returns a new library with only albums/items added to ipfs
|
||||
"""
|
||||
"""Returns a new library with only albums/items added to ipfs"""
|
||||
tmplib = library.Library(tmpname)
|
||||
for album in rlib.albums():
|
||||
try:
|
||||
@@ -280,10 +302,10 @@ class IPFSPlugin(BeetsPlugin):
|
||||
except AttributeError:
|
||||
pass
|
||||
item_path = os.path.basename(item.path).decode(
|
||||
util._fsencoding(), 'ignore'
|
||||
util._fsencoding(), "ignore"
|
||||
)
|
||||
# Clear current path from item
|
||||
item.path = f'/ipfs/{album.ipfs}/{item_path}'
|
||||
item.path = f"/ipfs/{album.ipfs}/{item_path}"
|
||||
|
||||
item.id = None
|
||||
items.append(item)
|
||||
@@ -292,4 +314,4 @@ class IPFSPlugin(BeetsPlugin):
|
||||
self._log.info("Adding '{0}' to temporary library", album)
|
||||
new_album = tmplib.add_album(items)
|
||||
new_album.ipfs = album.ipfs
|
||||
new_album.store()
|
||||
new_album.store(inherit=False)
|
||||
|
||||
Reference in New Issue
Block a user