mopidy-beets 1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ Copyright (C) 2013 <dz0ny at shortmail dot com>
2
+
3
+ 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:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,4 @@
1
+ include LICENSE
2
+ include MANIFEST.in
3
+ include README.rst
4
+ include mopidy_beets/ext.conf
@@ -0,0 +1,53 @@
1
+ Metadata-Version: 1.1
2
+ Name: Mopidy-Beets
3
+ Version: 1.0
4
+ Summary: Beets extension for Mopidy
5
+ Home-page: http://github.com/dz0ny/mopidy-beets/
6
+ Author: dz0ny
7
+ Author-email: dz0ny@shortmail.com
8
+ License: MIT
9
+ Description: Mopidy-Beets
10
+ =================
11
+
12
+ Mopidy http://www.mopidy.com/ extension for playing music from
13
+ Beets http://www.beets.com
14
+
15
+ Usage
16
+ -----
17
+
18
+ Install by running::
19
+
20
+ sudo pip install mopidy-beets
21
+
22
+
23
+ Setup your beets host
24
+
25
+ [beets]
26
+ host = "http://yourserver:port"
27
+
28
+
29
+
30
+ Project resources
31
+ -----------------
32
+
33
+ - Source code https://github.com/dz0ny/mopidy-beets_
34
+ - Issue tracker https://github.com/mopidy/mopidy-beets/issues
35
+ - Download development snapshot https://github.com/dz0ny/mopidy-beets/tarball/develop#egg=mopidy-beets-dev
36
+
37
+ License
38
+ -------
39
+
40
+ Copyright (C) 2013 <dz0ny at shortmail dot com>
41
+
42
+ 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:
43
+
44
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47
+ Platform: UNKNOWN
48
+ Classifier: Environment :: No Input/Output (Daemon)
49
+ Classifier: Intended Audience :: End Users/Desktop
50
+ Classifier: License :: OSI Approved :: MIT License
51
+ Classifier: Operating System :: OS Independent
52
+ Classifier: Programming Language :: Python :: 2
53
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Players
@@ -0,0 +1,16 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.rst
4
+ setup.py
5
+ Mopidy_Beets.egg-info/PKG-INFO
6
+ Mopidy_Beets.egg-info/SOURCES.txt
7
+ Mopidy_Beets.egg-info/dependency_links.txt
8
+ Mopidy_Beets.egg-info/entry_points.txt
9
+ Mopidy_Beets.egg-info/not-zip-safe
10
+ Mopidy_Beets.egg-info/requires.txt
11
+ Mopidy_Beets.egg-info/top_level.txt
12
+ mopidy_beets/__init__.py
13
+ mopidy_beets/actor.py
14
+ mopidy_beets/client.py
15
+ mopidy_beets/ext.conf
16
+ mopidy_beets/library.py
@@ -0,0 +1,3 @@
1
+ [mopidy.ext]
2
+ beets = mopidy_beets:BeetsExtension
3
+
@@ -0,0 +1,3 @@
1
+ setuptools
2
+ Mopidy
3
+ requests
@@ -0,0 +1 @@
1
+ mopidy_beets
@@ -0,0 +1,53 @@
1
+ Metadata-Version: 1.1
2
+ Name: Mopidy-Beets
3
+ Version: 1.0
4
+ Summary: Beets extension for Mopidy
5
+ Home-page: http://github.com/dz0ny/mopidy-beets/
6
+ Author: dz0ny
7
+ Author-email: dz0ny@shortmail.com
8
+ License: MIT
9
+ Description: Mopidy-Beets
10
+ =================
11
+
12
+ Mopidy http://www.mopidy.com/ extension for playing music from
13
+ Beets http://www.beets.com
14
+
15
+ Usage
16
+ -----
17
+
18
+ Install by running::
19
+
20
+ sudo pip install mopidy-beets
21
+
22
+
23
+ Setup your beets host
24
+
25
+ [beets]
26
+ host = "http://yourserver:port"
27
+
28
+
29
+
30
+ Project resources
31
+ -----------------
32
+
33
+ - Source code https://github.com/dz0ny/mopidy-beets_
34
+ - Issue tracker https://github.com/mopidy/mopidy-beets/issues
35
+ - Download development snapshot https://github.com/dz0ny/mopidy-beets/tarball/develop#egg=mopidy-beets-dev
36
+
37
+ License
38
+ -------
39
+
40
+ Copyright (C) 2013 <dz0ny at shortmail dot com>
41
+
42
+ 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:
43
+
44
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
45
+
46
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
47
+ Platform: UNKNOWN
48
+ Classifier: Environment :: No Input/Output (Daemon)
49
+ Classifier: Intended Audience :: End Users/Desktop
50
+ Classifier: License :: OSI Approved :: MIT License
51
+ Classifier: Operating System :: OS Independent
52
+ Classifier: Programming Language :: Python :: 2
53
+ Classifier: Topic :: Multimedia :: Sound/Audio :: Players
@@ -0,0 +1,38 @@
1
+ Mopidy-Beets
2
+ =================
3
+
4
+ Mopidy http://www.mopidy.com/ extension for playing music from
5
+ Beets http://www.beets.com
6
+
7
+ Usage
8
+ -----
9
+
10
+ Install by running::
11
+
12
+ sudo pip install mopidy-beets
13
+
14
+
15
+ Setup your beets host
16
+
17
+ [beets]
18
+ host = "http://yourserver:port"
19
+
20
+
21
+
22
+ Project resources
23
+ -----------------
24
+
25
+ - Source code https://github.com/dz0ny/mopidy-beets_
26
+ - Issue tracker https://github.com/mopidy/mopidy-beets/issues
27
+ - Download development snapshot https://github.com/dz0ny/mopidy-beets/tarball/develop#egg=mopidy-beets-dev
28
+
29
+ License
30
+ -------
31
+
32
+ Copyright (C) 2013 <dz0ny at shortmail dot com>
33
+
34
+ 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:
35
+
36
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
37
+
38
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,60 @@
1
+ from __future__ import unicode_literals
2
+
3
+ import os
4
+ from mopidy import ext, config
5
+ from mopidy.exceptions import ExtensionError
6
+
7
+
8
+ __doc__ = """A extension for playing music from Beets.
9
+
10
+ This extension handles URIs starting with ``beets:`` and enables you,
11
+ to play music from Beets web service.
12
+
13
+ See https://github.com/dz0ny/mopidy-beets/ for further instructions on
14
+ using this extension.
15
+
16
+ **Issues:**
17
+
18
+ https://github.com/dz0ny/mopidy-beets/issues
19
+
20
+ **Dependencies:**
21
+
22
+ requests
23
+
24
+ **Default config**
25
+
26
+ .. code-block:: ini
27
+
28
+ """
29
+
30
+ __version__ = '1.0'
31
+
32
+
33
+ class BeetsExtension(ext.Extension):
34
+
35
+ dist_name = 'Mopidy-Beets'
36
+ ext_name = 'beets'
37
+ version = __version__
38
+
39
+ def get_default_config(self):
40
+ conf_file = os.path.join(os.path.dirname(__file__), 'ext.conf')
41
+ return config.read(conf_file)
42
+
43
+ def get_config_schema(self):
44
+ schema = super(BeetsExtension, self).get_config_schema()
45
+ schema['hostname'] = config.String()
46
+ return schema
47
+
48
+ def validate_config(self, config):
49
+ if not config.getboolean('beets', 'enabled'):
50
+ return
51
+
52
+ def validate_environment(self):
53
+ try:
54
+ import requests # noqa
55
+ except ImportError as e:
56
+ raise ExtensionError('Library requests not found', e)
57
+
58
+ def get_backend_classes(self):
59
+ from .actor import BeetsBackend
60
+ return [BeetsBackend]
@@ -0,0 +1,32 @@
1
+ from __future__ import unicode_literals
2
+
3
+ import logging
4
+ import pykka
5
+
6
+ from mopidy.backends import base
7
+
8
+ from .library import BeetsLibraryProvider
9
+ from .client import BeetsRemoteClient
10
+
11
+ logger = logging.getLogger('mopidy.backends.beets')
12
+
13
+
14
+ class BeetsBackend(pykka.ThreadingActor, base.Backend):
15
+
16
+ def __init__(self, config, audio):
17
+ super(BeetsBackend, self).__init__()
18
+ self.beets_api = BeetsRemoteClient(config['beets']['hostname'])
19
+ self.library = BeetsLibraryProvider(backend=self)
20
+ self.playback = BeetsPlaybackProvider(audio=audio, backend=self)
21
+ self.playlists = None
22
+
23
+ self.uri_schemes = ['beets']
24
+
25
+
26
+ class BeetsPlaybackProvider(base.BasePlaybackProvider):
27
+
28
+ def play(self, track):
29
+ id = track.uri.split(';')[1]
30
+ logger.info('Getting info for track %s with id %s' % (track.uri, id))
31
+ track = self.backend.beets_api.get_track(id, True)
32
+ return super(BeetsPlaybackProvider, self).play(track)
@@ -0,0 +1,187 @@
1
+ #!/usr/local/bin/python
2
+ # -*- coding: utf-8 -*-
3
+ #
4
+ from __future__ import unicode_literals
5
+
6
+ import logging
7
+ import requests
8
+ import time
9
+
10
+ from requests.exceptions import RequestException
11
+ from mopidy.models import Track, Album, Artist
12
+
13
+ logger = logging.getLogger('mopidy.backends.beets.client')
14
+
15
+
16
+ class cache(object):
17
+ # TODO: merge this to util library
18
+
19
+ def __init__(self, ctl=8, ttl=3600):
20
+ self.cache = {}
21
+ self.ctl = ctl
22
+ self.ttl = ttl
23
+ self._call_count = 1
24
+
25
+ def __call__(self, func):
26
+ def _memoized(*args):
27
+ self.func = func
28
+ now = time.time()
29
+ try:
30
+ value, last_update = self.cache[args]
31
+ age = now - last_update
32
+ if self._call_count >= self.ctl or \
33
+ age > self.ttl:
34
+ self._call_count = 1
35
+ raise AttributeError
36
+
37
+ self._call_count += 1
38
+ return value
39
+
40
+ except (KeyError, AttributeError):
41
+ value = self.func(*args)
42
+ self.cache[args] = (value, now)
43
+ return value
44
+
45
+ except TypeError:
46
+ return self.func(*args)
47
+ return _memoized
48
+
49
+
50
+ class BeetsRemoteClient(object):
51
+
52
+ def __init__(self, endpoint):
53
+ super(BeetsRemoteClient, self).__init__()
54
+ self.api = requests.Session()
55
+ self.has_connection = False
56
+ self.api_endpoint = endpoint
57
+ logger.info('Connecting to Beets remote library %s', endpoint)
58
+ try:
59
+ self.api.get(self.api_endpoint)
60
+ self.has_connection = True
61
+ except RequestException as e:
62
+ logger.error('Beets error: %s' % e)
63
+
64
+ @cache()
65
+ def get_tracks(self):
66
+ track_ids = self._get('/item/').get('item_ids')
67
+ tracks = []
68
+ for track_id in track_ids:
69
+ tracks.append(self.get_track(track_id))
70
+ return tracks
71
+
72
+ @cache(ctl=16)
73
+ def get_track(self, id, remote_url=False):
74
+ return self._convert_json_data(self._get('/item/%s' % id), remote_url)
75
+
76
+ @cache()
77
+ def get_item_by(self, name):
78
+ res = self._get('/item/query/%s' % name).get('results')
79
+ try:
80
+ return self._parse_query(res)
81
+ except Exception:
82
+ return False
83
+
84
+ @cache()
85
+ def get_album_by(self, name):
86
+ res = self._get('/album/query/%s' % name).get('results')
87
+ try:
88
+ return self._parse_query(res[0]['items'])
89
+ except Exception:
90
+ return False
91
+
92
+ def _get(self, url):
93
+ try:
94
+ url = self.api_endpoint + url
95
+ logger.debug('Requesting %s' % url)
96
+ req = self.api.get(url)
97
+ if req.status_code != 200:
98
+ raise logger.error('Request %s, failed with status code %s' % (
99
+ url, req.status_code))
100
+
101
+ return req.json()
102
+ except Exception as e:
103
+ return False
104
+ logger.error('Request %s, failed with error %s' % (
105
+ url, e))
106
+
107
+ def _parse_query(self, res):
108
+ if len(res) > 0:
109
+ tracks = []
110
+ for track in res:
111
+ tracks.append(self._convert_json_data(track))
112
+ return tracks
113
+ return None
114
+
115
+ def _convert_json_data(self, data, remote_url=False):
116
+ if not data:
117
+ return
118
+ # NOTE kwargs dict keys must be bytestrings to work on Python < 2.6.5
119
+ # See https://github.com/mopidy/mopidy/issues/302 for details.
120
+
121
+ track_kwargs = {}
122
+ album_kwargs = {}
123
+ artist_kwargs = {}
124
+ albumartist_kwargs = {}
125
+
126
+ if 'track' in data:
127
+ track_kwargs[b'track_no'] = int(data['track'])
128
+
129
+ if 'tracktotal' in data:
130
+ album_kwargs[b'num_tracks'] = int(data['tracktotal'])
131
+
132
+ if 'artist' in data:
133
+ artist_kwargs[b'name'] = data['artist']
134
+ albumartist_kwargs[b'name'] = data['artist']
135
+
136
+ if 'albumartist' in data:
137
+ albumartist_kwargs[b'name'] = data['albumartist']
138
+
139
+ if 'album' in data:
140
+ album_kwargs[b'name'] = data['album']
141
+
142
+ if 'title' in data:
143
+ track_kwargs[b'name'] = data['title']
144
+
145
+ if 'date' in data:
146
+ track_kwargs[b'date'] = data['date']
147
+
148
+ if 'mb_trackid' in data:
149
+ track_kwargs[b'musicbrainz_id'] = data['mb_trackid']
150
+
151
+ if 'mb_albumid' in data:
152
+ album_kwargs[b'musicbrainz_id'] = data['mb_albumid']
153
+
154
+ if 'mb_artistid' in data:
155
+ artist_kwargs[b'musicbrainz_id'] = data['mb_artistid']
156
+
157
+ if 'mb_albumartistid' in data:
158
+ albumartist_kwargs[b'musicbrainz_id'] = (
159
+ data['mb_albumartistid'])
160
+
161
+ if 'album_id' in data:
162
+ album_art_url = '%s/album/%s/art' % (
163
+ self.api_endpoint, data['album_id'])
164
+ album_kwargs[b'images'] = [album_art_url]
165
+
166
+ if artist_kwargs:
167
+ artist = Artist(**artist_kwargs)
168
+ track_kwargs[b'artists'] = [artist]
169
+
170
+ if albumartist_kwargs:
171
+ albumartist = Artist(**albumartist_kwargs)
172
+ album_kwargs[b'artists'] = [albumartist]
173
+
174
+ if album_kwargs:
175
+ album = Album(**album_kwargs)
176
+ track_kwargs[b'album'] = album
177
+
178
+ if remote_url:
179
+ track_kwargs[b'uri'] = '%s/item/%s/file' % (
180
+ self.api_endpoint, data['id'])
181
+ else:
182
+ track_kwargs[b'uri'] = 'beets:track;%s' % data['id']
183
+ track_kwargs[b'length'] = int(data.get('length', 0)) * 1000
184
+
185
+ track = Track(**track_kwargs)
186
+
187
+ return track
@@ -0,0 +1,4 @@
1
+ [beets]
2
+ enabled = True
3
+ hostname =
4
+
@@ -0,0 +1,64 @@
1
+ from __future__ import unicode_literals
2
+
3
+ import logging
4
+
5
+ from mopidy.backends import base
6
+ from mopidy.models import SearchResult
7
+
8
+ logger = logging.getLogger('mopidy.backends.beets')
9
+
10
+
11
+ class BeetsLibraryProvider(base.BaseLibraryProvider):
12
+ def __init__(self, *args, **kwargs):
13
+ super(BeetsLibraryProvider, self).__init__(*args, **kwargs)
14
+ self.remote = self.backend.beets_api
15
+
16
+ def find_exact(self, query=None, uris=None):
17
+ return self.search(query=query, uris=uris)
18
+
19
+ def search(self, query=None, uris=None):
20
+ if not self.remote.has_connection:
21
+ return []
22
+
23
+ if not query:
24
+ # Fetch all data(browse library)
25
+ return SearchResult(
26
+ uri='beets:search',
27
+ tracks=self.remote.get_tracks())
28
+
29
+ self._validate_query(query)
30
+
31
+ for (field, val) in query.iteritems():
32
+ if field == "album":
33
+ return SearchResult(
34
+ uri='beets:search',
35
+ tracks=self.remote.get_album_by(val[0]) or [])
36
+ elif field == "artist":
37
+ return SearchResult(
38
+ uri='beets:search',
39
+ tracks=self.remote.get_item_by(val[0]) or [])
40
+ elif field == "any":
41
+ return SearchResult(
42
+ uri='beets:search',
43
+ tracks=self.remote.get_item_by(val[0]) or [])
44
+ else:
45
+ raise LookupError('Invalid lookup field: %s' % field)
46
+
47
+ return []
48
+
49
+ def lookup(self, uri):
50
+ try:
51
+ id = uri.split(";")[1]
52
+ logger.debug(u'Beets track id for "%s": %s', id, uri)
53
+ return [self.remote.get_track(id, True)]
54
+ except Exception as error:
55
+ logger.debug(u'Failed to lookup "%s": %s', uri, error)
56
+ return []
57
+
58
+ def _validate_query(self, query):
59
+ for (_, values) in query.iteritems():
60
+ if not values:
61
+ raise LookupError('Missing query')
62
+ for value in values:
63
+ if not value:
64
+ raise LookupError('Missing query')
@@ -0,0 +1,5 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+ tag_svn_revision = 0
5
+
@@ -0,0 +1,42 @@
1
+ from __future__ import unicode_literals
2
+
3
+ import re
4
+ from setuptools import setup, find_packages
5
+
6
+
7
+ def get_version(filename):
8
+ content = open(filename).read()
9
+ metadata = dict(re.findall("__([a-z]+)__ = '([^']+)'", content))
10
+ return metadata['version']
11
+
12
+ setup(
13
+ name='Mopidy-Beets',
14
+ version=get_version('mopidy_beets/__init__.py'),
15
+ url='http://github.com/dz0ny/mopidy-beets/',
16
+ license='MIT',
17
+ author='dz0ny',
18
+ author_email='dz0ny@shortmail.com',
19
+ description='Beets extension for Mopidy',
20
+ long_description=open('README.rst').read(),
21
+ packages=find_packages(exclude=['tests', 'tests.*']),
22
+ zip_safe=False,
23
+ include_package_data=True,
24
+ install_requires=[
25
+ 'setuptools',
26
+ 'Mopidy',
27
+ 'requests',
28
+ ],
29
+ entry_points={
30
+ b'mopidy.ext': [
31
+ 'beets = mopidy_beets:BeetsExtension',
32
+ ],
33
+ },
34
+ classifiers=[
35
+ 'Environment :: No Input/Output (Daemon)',
36
+ 'Intended Audience :: End Users/Desktop',
37
+ 'License :: OSI Approved :: MIT License',
38
+ 'Operating System :: OS Independent',
39
+ 'Programming Language :: Python :: 2',
40
+ 'Topic :: Multimedia :: Sound/Audio :: Players',
41
+ ],
42
+ )