From 584242bf0a450d3dd94d6796d4677144dd8c10e5 Mon Sep 17 00:00:00 2001 From: sqozz Date: Wed, 12 Jun 2019 12:22:57 +0200 Subject: [PATCH] Add initial working version --- README.md | 11 +++++++++++ auto_import.sh | 13 +++++++++++++ funkwhaleimport.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 README.md create mode 100755 auto_import.sh create mode 100644 funkwhaleimport.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..7a316c7 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +``` +1. Copy the beets plugin into your plugin folder (e.g. /usr/lib64/python2.7/site-packages/beetsplug/) +2. Deploy the virtual-env wrapper (auto_deploy.sh) e.g. to /opt/funkwhale/auto_deploy.sh and adjust paths in it +3. Adjust your beets config for the plugin: + +plugins: funkwhaleimport +funkwhale: + library_id: "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee" + importer: "/opt/funkwhale/auto_import.sh" + +``` diff --git a/auto_import.sh b/auto_import.sh new file mode 100755 index 0000000..bc76581 --- /dev/null +++ b/auto_import.sh @@ -0,0 +1,13 @@ +#!/bin/bash +USER=$(id -un) +if [ "$USER" != "funkwhale" ]; then + exec sudo -u funkwhale $0 "$@" +elif [ "$USER" == "funkwhale" ]; then + echo "activating venv" + cd /opt/funkwhale + source virtualenv/bin/activate + export $(cat prod.env) + echo "venv activated. Importing now" + echo "$@" + exec /opt/funkwhale/api/manage.py "$@" +fi diff --git a/funkwhaleimport.py b/funkwhaleimport.py new file mode 100644 index 0000000..fa65ba7 --- /dev/null +++ b/funkwhaleimport.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- + +"""Updates an Funkwhale intance whenever the filesystem changes. + +Put something like the following in your config.yaml to configure: + funkwhale: + library_id: id_to_import_into + importer: path_to_importer_script +""" +from __future__ import division, absolute_import, print_function + +from beets.plugins import BeetsPlugin +import os +from beets import config +from subprocess import call + +class FunkwhaleUpdatePlugin(BeetsPlugin): + def __init__(self): + super(FunkwhaleUpdatePlugin, self).__init__() + config['funkwhale'].add({ + "library_id": "", + "importer": "" + }) + + self.register_listener('import_task_files', self.task_done) + + def task_done(self, task, session): + self._log.info(u'Database updated.') + folders = [] + for item in task.items: + path = os.path.dirname(item.path) #get path of every imported file + folders.append(path) + folders = list(set(folders)) + folders = map(lambda f: os.path.join(f, "*"), folders) + command = [ + str(config["funkwhale"]["importer"]), + "import_files", + str(config["funkwhale"]["library_id"]), + " ".join(map(lambda f: "{}".format(f),folders)), + "--recursive", + "--replace", + "--noinput" + ] + call(command)