scripts/mycroft/audio/services/mopidy/__init__.py
Go to the documentation of this file.
1 # Copyright 2017 Mycroft AI Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 #
15 import sys
16 import time
17 
18 from os.path import dirname, abspath
19 
20 from mycroft.audio.services import RemoteAudioBackend
21 from mycroft.messagebus.message import Message
22 from mycroft.util.log import LOG
23 
24 
25 sys.path.append(abspath(dirname(__file__)))
26 Mopidy = __import__('mopidypost').Mopidy
27 
28 
30  def _connect(self, message):
31  """
32  Callback method to connect to mopidy if server is not available
33  at startup.
34  """
35  url = 'http://localhost:6680'
36  if self.config is not None:
37  url = self.config.get('url', url)
38  try:
39  self.mopidy = Mopidy(url)
40  except Exception:
41  if self.connection_attempts < 1:
42  LOG.debug('Could not connect to server, will retry quietly')
43  self.connection_attempts += 1
44  time.sleep(10)
45  self.bus.emit(Message('MopidyServiceConnect'))
46  return
47 
48  LOG.info('Connected to mopidy server')
49 
50  def __init__(self, config, bus, name='mopidy'):
52  self.bus = bus
53  self.config = config
54  self.name = name
55 
56  self.mopidy = None
57  self.bus.on('MopidyServiceConnect', self._connect)
58  self.bus.emit(Message('MopidyServiceConnect'))
59 
60  def supported_uris(self):
61  """
62  Return supported uri's if mopidy server is found,
63  otherwise return empty list indicating this service
64  doesn't support anything.
65  """
66  if self.mopidy:
67  return ['file', 'http', 'https', 'local', 'spotify', 'gmusic']
68  else:
69  return []
70 
71  def clear_list(self):
72  self.mopidy.clear_list()
73 
74  def add_list(self, tracks):
75  self.mopidy.add_list(tracks)
76 
77  def play(self, repeat=False):
78  """ Start playback.
79 
80  TODO: Add repeat support.
81  """
82  self.mopidy.play()
83 
84  def stop(self):
85  if self.mopidy.is_playing():
86  self.mopidy.clear_list()
87  self.mopidy.stop()
88  return True
89  else:
90  return False
91 
92  def pause(self):
93  self.mopidy.pause()
94 
95  def resume(self):
96  self.mopidy.resume()
97 
98  def next(self):
99  self.mopidy.next()
100 
101  def previous(self):
102  self.mopidy.previous()
103 
104  def lower_volume(self):
105  self.mopidy.lower_volume()
106 
107  def restore_volume(self):
108  self.mopidy.restore_volume()
109 
110  def track_info(self):
111  info = self.mopidy.currently_playing()
112  ret = {}
113  ret['name'] = info.get('name', '')
114  if 'album' in info:
115  ret['artist'] = info['album']['artists'][0]['name']
116  ret['album'] = info['album'].get('name', '')
117  else:
118  ret['artist'] = ''
119  ret['album'] = ''
120  return ret
121 
122 
123 def load_service(base_config, bus):
124  backends = base_config.get('backends', [])
125  services = [(b, backends[b]) for b in backends
126  if backends[b]['type'] == 'mopidy' and
127  backends[b].get('active', True)]
128  instances = [MopidyService(s[1], bus, s[0]) for s in services]
129  return instances
def get(phrase, lang=None, context=None)


mycroft_ros
Author(s):
autogenerated on Mon Apr 26 2021 02:35:40