| 1 |
Web Plugin |
| 2 |
========== |
| 3 |
|
| 4 |
The ``web`` plugin is a very basic alternative interface to beets that |
| 5 |
supplements the CLI. It can't do much right now, and the interface is a little |
| 6 |
clunky, but you can use it to query and browse your music and---in browsers that |
| 7 |
support HTML5 Audio---you can even play music. |
| 8 |
|
| 9 |
While it's not meant to replace the CLI, a graphical interface has a number of |
| 10 |
advantages in certain situations. For example, when editing a tag, a natural CLI |
| 11 |
makes you retype the whole thing---common GUI conventions can be used to just |
| 12 |
edit the part of the tag you want to change. A graphical interface could also |
| 13 |
drastically increase the number of people who can use beets. |
| 14 |
|
| 15 |
Install |
| 16 |
------- |
| 17 |
|
| 18 |
To use the ``web`` plugin, first enable it in your configuration (see |
| 19 |
:ref:`using-plugins`). Then, install ``beets`` with ``web`` extra |
| 20 |
|
| 21 |
.. code-block:: bash |
| 22 |
|
| 23 |
pip install "beets[web]" |
| 24 |
|
| 25 |
Run the Server |
| 26 |
-------------- |
| 27 |
|
| 28 |
Then just type ``beet web`` to start the server and go to |
| 29 |
http://localhost:8337/. This is what it looks like: |
| 30 |
|
| 31 |
.. image:: beetsweb.png |
| 32 |
|
| 33 |
You can also specify the hostname and port number used by the Web server. These |
| 34 |
can be specified on the command line or in the ``[web]`` section of your |
| 35 |
:doc:`configuration file </reference/config>`. |
| 36 |
|
| 37 |
On the command line, use ``beet web [HOSTNAME] [PORT]``. Or the configuration |
| 38 |
options below. |
| 39 |
|
| 40 |
Usage |
| 41 |
----- |
| 42 |
|
| 43 |
Type queries into the little search box. Double-click a track to play it with |
| 44 |
HTML5 Audio. |
| 45 |
|
| 46 |
Configuration |
| 47 |
------------- |
| 48 |
|
| 49 |
To configure the plugin, make a ``web:`` section in your configuration file. The |
| 50 |
available options are: |
| 51 |
|
| 52 |
- **host**: The server hostname. Set this to 0.0.0.0 to bind to all interfaces. |
| 53 |
Default: Bind to 127.0.0.1. |
| 54 |
- **port**: The server port. Default: 8337. |
| 55 |
- **cors**: The CORS allowed origin (see :ref:`web-cors`, below). Default: CORS |
| 56 |
is disabled. |
| 57 |
- **cors_supports_credentials**: Support credentials when using CORS (see |
| 58 |
:ref:`web-cors`, below). Default: CORS_SUPPORTS_CREDENTIALS is disabled. |
| 59 |
- **reverse_proxy**: If true, enable reverse proxy support (see |
| 60 |
:ref:`reverse-proxy`, below). Default: false. |
| 61 |
- **include_paths**: If true, includes paths in item objects. Default: false. |
| 62 |
- **readonly**: If true, DELETE and PATCH operations are not allowed. Only GET |
| 63 |
is permitted. Default: true. |
| 64 |
|
| 65 |
Implementation |
| 66 |
-------------- |
| 67 |
|
| 68 |
The Web backend is built using a simple REST+JSON API with the excellent Flask_ |
| 69 |
library. The frontend is a single-page application written with Backbone.js_. |
| 70 |
This allows future non-Web clients to use the same backend API. |
| 71 |
|
| 72 |
.. _backbone.js: https://backbonejs.org |
| 73 |
|
| 74 |
Eventually, to make the Web player really viable, we should use a Flash fallback |
| 75 |
for unsupported formats/browsers. There are a number of options for this: |
| 76 |
|
| 77 |
- audio.js_ |
| 78 |
- html5media_ |
| 79 |
- MediaElement.js_ |
| 80 |
|
| 81 |
.. _audio.js: https://kolber.github.io/audiojs/ |
| 82 |
|
| 83 |
.. _html5media: https://html5media.info/ |
| 84 |
|
| 85 |
.. _mediaelement.js: https://www.mediaelementjs.com/ |
| 86 |
|
| 87 |
.. _web-cors: |
| 88 |
|
| 89 |
Cross-Origin Resource Sharing (CORS) |
| 90 |
------------------------------------ |
| 91 |
|
| 92 |
The ``web`` plugin's API can be used as a backend for an in-browser client. By |
| 93 |
default, browsers will only allow access from clients running on the same server |
| 94 |
as the API. (You will get an arcane error about ``XMLHttpRequest`` otherwise.) A |
| 95 |
technology called CORS_ lets you relax this restriction. |
| 96 |
|
| 97 |
If you want to use an in-browser client hosted elsewhere (or running from a |
| 98 |
different server on your machine), set the ``cors`` configuration option to the |
| 99 |
"origin" (protocol, host, and optional port number) where the client is served. |
| 100 |
Or set it to ``'*'`` to enable access from all origins. Note that there are |
| 101 |
security implications if you set the origin to ``'*'``, so please research this |
| 102 |
before using it. |
| 103 |
|
| 104 |
If the ``web`` server is behind a proxy that uses credentials, you might want to |
| 105 |
set the ``cors_supports_credentials`` configuration option to true to let |
| 106 |
in-browser clients log in. |
| 107 |
|
| 108 |
For example: |
| 109 |
|
| 110 |
:: |
| 111 |
|
| 112 |
web: |
| 113 |
host: 0.0.0.0 |
| 114 |
cors: 'http://example.com' |
| 115 |
|
| 116 |
.. _cors: https://en.wikipedia.org/wiki/Cross-origin_resource_sharing |
| 117 |
|
| 118 |
.. _reverse-proxy: |
| 119 |
|
| 120 |
Reverse Proxy Support |
| 121 |
--------------------- |
| 122 |
|
| 123 |
When the server is running behind a reverse proxy, you can tell the plugin to |
| 124 |
respect forwarded headers. Specifically, this can help when you host the plugin |
| 125 |
at a base URL other than the root ``/`` or when you use the proxy to handle |
| 126 |
secure connections. Enable the ``reverse_proxy`` configuration option if you do |
| 127 |
this. |
| 128 |
|
| 129 |
Technically, this option lets the proxy provide ``X-Script-Name`` and |
| 130 |
``X-Scheme`` HTTP headers to control the plugin's the ``SCRIPT_NAME`` and its |
| 131 |
``wsgi.url_scheme`` parameter. |
| 132 |
|
| 133 |
Here's a sample Nginx_ configuration that serves the web plugin under the /beets |
| 134 |
directory: |
| 135 |
|
| 136 |
:: |
| 137 |
|
| 138 |
location /beets { |
| 139 |
proxy_pass http://127.0.0.1:8080; |
| 140 |
proxy_set_header Host $host; |
| 141 |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 142 |
proxy_set_header X-Scheme $scheme; |
| 143 |
proxy_set_header X-Script-Name /beets; |
| 144 |
} |
| 145 |
|
| 146 |
.. _nginx: https://www.f5.com/products/nginx |
| 147 |
|
| 148 |
JSON API |
| 149 |
-------- |
| 150 |
|
| 151 |
``GET /item/`` |
| 152 |
~~~~~~~~~~~~~~ |
| 153 |
|
| 154 |
Responds with a list of all tracks in the beets library. |
| 155 |
|
| 156 |
:: |
| 157 |
|
| 158 |
{ |
| 159 |
"items": [ |
| 160 |
{ |
| 161 |
"id": 6, |
| 162 |
"title": "A Song", |
| 163 |
... |
| 164 |
}, { |
| 165 |
"id": 12, |
| 166 |
"title": "Another Song", |
| 167 |
... |
| 168 |
} |
| 169 |
... |
| 170 |
] |
| 171 |
} |
| 172 |
|
| 173 |
``GET /item/6`` |
| 174 |
~~~~~~~~~~~~~~~ |
| 175 |
|
| 176 |
Looks for an item with id *6* in the beets library and responds with its JSON |
| 177 |
representation. |
| 178 |
|
| 179 |
:: |
| 180 |
|
| 181 |
{ |
| 182 |
"id": 6, |
| 183 |
"title": "A Song", |
| 184 |
... |
| 185 |
} |
| 186 |
|
| 187 |
If there is no item with that id responds with a *404* status code. |
| 188 |
|
| 189 |
``DELETE /item/6`` |
| 190 |
~~~~~~~~~~~~~~~~~~ |
| 191 |
|
| 192 |
Removes the item with id *6* from the beets library. If the *?delete* query |
| 193 |
string is included, the matching file will be deleted from disk. |
| 194 |
|
| 195 |
Only allowed if ``readonly`` configuration option is set to ``no``. |
| 196 |
|
| 197 |
``PATCH /item/6`` |
| 198 |
~~~~~~~~~~~~~~~~~ |
| 199 |
|
| 200 |
Updates the item with id *6* and write the changes to the music file. The body |
| 201 |
should be a JSON object containing the changes to the object. |
| 202 |
|
| 203 |
Returns the updated JSON representation. |
| 204 |
|
| 205 |
:: |
| 206 |
|
| 207 |
{ |
| 208 |
"id": 6, |
| 209 |
"title": "A Song", |
| 210 |
... |
| 211 |
} |
| 212 |
|
| 213 |
Only allowed if ``readonly`` configuration option is set to ``no``. |
| 214 |
|
| 215 |
``GET /item/6,12,13`` |
| 216 |
~~~~~~~~~~~~~~~~~~~~~ |
| 217 |
|
| 218 |
Response with a list of tracks with the ids *6*, *12* and *13*. The format of |
| 219 |
the response is the same as for `GET /item/`_. It is *not guaranteed* that the |
| 220 |
response includes all the items requested. If a track is not found it is |
| 221 |
silently dropped from the response. |
| 222 |
|
| 223 |
This endpoint also supports *DELETE* and *PATCH* methods as above, to operate on |
| 224 |
all items of the list. |
| 225 |
|
| 226 |
``GET /item/path/...`` |
| 227 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 228 |
|
| 229 |
Look for an item at the given absolute path on the server. If it corresponds to |
| 230 |
a track, return the track in the same format as ``/item/*``. |
| 231 |
|
| 232 |
If the server runs UNIX, you'll need to include an extra leading slash: |
| 233 |
``http://localhost:8337/item/path//Users/beets/Music/Foo/Bar/Baz.mp3`` |
| 234 |
|
| 235 |
``GET /item/query/querystring`` |
| 236 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 237 |
|
| 238 |
Returns a list of tracks matching the query. The *querystring* must be a valid |
| 239 |
query as described in :doc:`/reference/query`. |
| 240 |
|
| 241 |
:: |
| 242 |
|
| 243 |
{ |
| 244 |
"results": [ |
| 245 |
{ "id" : 6, "title": "A Song" }, |
| 246 |
{ "id" : 12, "title": "Another Song" } |
| 247 |
] |
| 248 |
} |
| 249 |
|
| 250 |
Path elements are joined as parts of a query. For example, |
| 251 |
``/item/query/foo/bar`` will be converted to the query ``foo,bar``. To specify |
| 252 |
literal path separators in a query, use a backslash instead of a slash. |
| 253 |
|
| 254 |
This endpoint also supports *DELETE* and *PATCH* methods as above, to operate on |
| 255 |
all items returned by the query. |
| 256 |
|
| 257 |
``GET /item/6/file`` |
| 258 |
~~~~~~~~~~~~~~~~~~~~ |
| 259 |
|
| 260 |
Sends the media file for the track. If the item or its corresponding file do not |
| 261 |
exist a *404* status code is returned. |
| 262 |
|
| 263 |
Albums |
| 264 |
~~~~~~ |
| 265 |
|
| 266 |
For albums, the following endpoints are provided: |
| 267 |
|
| 268 |
- ``GET /album/`` |
| 269 |
- ``GET /album/5`` |
| 270 |
- ``GET /album/5/art`` |
| 271 |
- ``DELETE /album/5`` |
| 272 |
- ``GET /album/5,7`` |
| 273 |
- ``DELETE /album/5,7`` |
| 274 |
- ``GET /album/query/querystring`` |
| 275 |
- ``DELETE /album/query/querystring`` |
| 276 |
|
| 277 |
The interface and response format is similar to the item API, except replacing |
| 278 |
the encapsulation key ``"items"`` with ``"albums"`` when requesting ``/album/`` |
| 279 |
or ``/album/5,7``. In addition we can request the cover art of an album with |
| 280 |
``GET /album/5/art``. You can also add the '?expand' flag to get the individual |
| 281 |
items of an album. |
| 282 |
|
| 283 |
``DELETE`` is only allowed if ``readonly`` configuration option is set to |
| 284 |
``no``. |
| 285 |
|
| 286 |
``GET /stats`` |
| 287 |
~~~~~~~~~~~~~~ |
| 288 |
|
| 289 |
Responds with the number of tracks and albums in the database. |
| 290 |
|
| 291 |
:: |
| 292 |
|
| 293 |
{ |
| 294 |
"items": 5, |
| 295 |
"albums": 3 |
| 296 |
} |
| 297 |
|
| 298 |
.. _flask: https://flask.palletsprojects.com/en/stable/ |