From fc1592adbee583bc1657606142c0ecc18fe1745f Mon Sep 17 00:00:00 2001 From: L Maffeo Date: Sat, 18 Aug 2018 14:17:00 +0200 Subject: [PATCH] Renamed plugin, added default configuration and comments --- beetsplug/subsonicupdate.py | 97 +++++++++++++++++++++++++++++++++ docs/changelog.rst | 3 +- docs/plugins/index.rst | 2 +- docs/plugins/subsonicupdate.rst | 41 ++++++++++++++ 4 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 beetsplug/subsonicupdate.py create mode 100644 docs/plugins/subsonicupdate.rst diff --git a/beetsplug/subsonicupdate.py b/beetsplug/subsonicupdate.py new file mode 100644 index 000000000..b78f49188 --- /dev/null +++ b/beetsplug/subsonicupdate.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- +# 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 Subsonic library on Beets import +Your Beets configuration file should contain +a "subsonic" section like the following: + subsonic: + host: 192.168.x.y (Subsonic server IP) + port: 4040 (default) + user: + pass: +""" +from __future__ import division, absolute_import, print_function + +from beets.plugins import BeetsPlugin +import requests +import string +import hashlib +import random + +__author__ = 'https://github.com/maffo999' + + +class SubsonicUpdate(BeetsPlugin): + def __init__(self): + super(SubsonicUpdate, self).__init__() + +# Set default configuration values + self.config['subsonic'].add({ + u'host': u'localhost', + u'port': 4040, + u'user': u'admin', + u'pass': u'admin', + }) + self.config['subsonic']['pass'].redact = True + self.register_listener('import', self.loaded) + + def loaded(self): + host = self.config['host'].as_str() + port = self.config['port'].as_str() + user = self.config['user'].as_str() + passw = self.config['pass'].as_str() + +# To avoid sending plaintext passwords, authentication will be performed via +# username, a token, and a 6 random letters/numbers sequence +# The token is the concatenation of your password and the 6 random +# letters/numbers (the salt) which is hashed with MD5. + +# Pick the random sequence and salt the password + r = string.ascii_letters + string.digits + salt = "".join([random.choice(r) for n in range(6)]) + t = passw + salt + +# Hash the password making sure it's UTF-8 format + token = hashlib.md5() + token.update(t.encode('utf-8')) + +# Put together the payload of the request to the server and the URL + payload = { + 'u': user, + 't': token.hexdigest(), + 's': salt, + 'v': '1.15.0', + 'c': 'beets' + } + url = "http://{}:{}/rest/startScan".format(host, port) + +# Send the request and store the response + response = requests.post(url, params=payload) + +# Log an eventual error reported by the server or success on status code 200 + if (response.status_code == 0): + self._log.error(u'Generic error, please try again later.') + elif (response.status_code == 30): + self._log.error(u'Subsonic server not compatible with plugin.') + elif (response.status_code == 40): + self._log.error(u'Wrong username or password.') + elif (response.status_code == 50): + self._log.error(u'User not allowed to perform the operation.') + elif (response.status_code == 60): + self._log.error(u'This feature requires Subsonic Premium.') + elif (response.status_code == 200): + self._log.info('Operation completed successfully!') + else: + self._log.error(u'Unknown error code returned from server.') diff --git a/docs/changelog.rst b/docs/changelog.rst index 2f40d26ed..d18c6c690 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -18,7 +18,8 @@ New features: :user:`jams2` * Automatically upload to Google Play Music library on track import. :user:`shuaiscott` -* Added Subsonic automatic library update plugin +* Added :doc:`/plugins/subsonicupdate` that can automatically update your Subsonic library. + :user:`maffo999` Fixes: diff --git a/docs/plugins/index.rst b/docs/plugins/index.rst index 8c23883c9..6bf50e227 100644 --- a/docs/plugins/index.rst +++ b/docs/plugins/index.rst @@ -89,7 +89,7 @@ like this:: smartplaylist sonosupdate spotify - subsonic + subsonicupdate the thumbnails types diff --git a/docs/plugins/subsonicupdate.rst b/docs/plugins/subsonicupdate.rst new file mode 100644 index 000000000..dd48b8381 --- /dev/null +++ b/docs/plugins/subsonicupdate.rst @@ -0,0 +1,41 @@ +Subsonic Plugin +================ + +``subsonic`` is a very simple plugin for beets that lets you automatically +update `Subsonic`_'s index whenever you change your beets library. + +.. _Subsonic: http://www.subsonic.org + +To use ``subsonic`` plugin, enable it in your configuration +(see :ref:`using-plugins`). +Then, you'll probably want to configure the specifics of your Subsonic server. +You can do that using an ``subsonic:`` section in your ``config.yaml``, +which looks like this:: + + subsonic: + host: X.X.X.X + port: 4040 + user: username + pass: password + +With that all in place, beets will send a Rest API to your Subsonic +server every time you change your beets library. + +This plugin requires Subsonic v6.1 or higher and an active Premium license (or trial). + +Configuration +------------- + +The available options under the ``subsonic:`` section are: + +- **host**: The Subsonic server name/IP. +- **port**: The Subsonic server port. +- **user**: The Subsonic user. +- **pass**: The Subsonic user password. + +This plugin sets the following default values: + +- **host**: localhost +- **port**: 4040 +- **user**: admin +- **pass**: admin \ No newline at end of file