mopidypost.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 json
16 from copy import copy
17 
18 import requests
19 
20 
21 MOPIDY_API = '/mopidy/rpc'
22 
23 _base_dict = {'jsonrpc': '2.0', 'id': 1, 'params': {}}
24 
25 
26 class Mopidy:
27  def __init__(self, url):
28  self.is_playing = False
29  self.url = url + MOPIDY_API
30  self.volume = None
31  self.clear_list(force=True)
32  self.volume_low = 3
33  self.volume_high = 100
34 
35  def find_artist(self, artist):
36  d = copy(_base_dict)
37  d['method'] = 'core.library.search'
38  d['params'] = {'artist': [artist]}
39  r = requests.post(self.url, data=json.dumps(d))
40  return r.json()['result'][1]['artists']
41 
42  def get_playlists(self, filter=None):
43  d = copy(_base_dict)
44  d['method'] = 'core.playlists.as_list'
45  r = requests.post(self.url, data=json.dumps(d))
46  if filter is None:
47  return r.json()['result']
48  else:
49  return [l for l in r.json()['result'] if filter + ':' in l['uri']]
50 
51  def find_album(self, album, filter=None):
52  d = copy(_base_dict)
53  d['method'] = 'core.library.search'
54  d['params'] = {'album': [album]}
55  r = requests.post(self.url, data=json.dumps(d))
56  lst = [res['albums'] for res in r.json()['result'] if 'albums' in res]
57  if filter is None:
58  return lst
59  else:
60  return [i for sl in lst for i in sl if filter + ':' in i['uri']]
61 
62  def find_exact(self, uris='null'):
63  d = copy(_base_dict)
64  d['method'] = 'core.library.find_exact'
65  d['params'] = {'uris': uris}
66  r = requests.post(self.url, data=json.dumps(d))
67  return r.json()
68 
69  def browse(self, uri):
70  d = copy(_base_dict)
71  d['method'] = 'core.library.browse'
72  d['params'] = {'uri': uri}
73  r = requests.post(self.url, data=json.dumps(d))
74  if 'result' in r.json():
75  return r.json()['result']
76  else:
77  return None
78 
79  def clear_list(self, force=False):
80  if self.is_playing or force:
81  d = copy(_base_dict)
82  d['method'] = 'core.tracklist.clear'
83  r = requests.post(self.url, data=json.dumps(d))
84  return r
85 
86  def add_list(self, uri):
87  d = copy(_base_dict)
88  d['method'] = 'core.tracklist.add'
89  if isinstance(uri, str):
90  d['params'] = {'uri': uri}
91  elif type(uri) == list:
92  d['params'] = {'uris': uri}
93  else:
94  return None
95  r = requests.post(self.url, data=json.dumps(d))
96  return r
97 
98  def play(self):
99  self.is_playing = True
100  self.restore_volume()
101  d = copy(_base_dict)
102  d['method'] = 'core.playback.play'
103  r = requests.post(self.url, data=json.dumps(d))
104 
105  def next(self):
106  if self.is_playing:
107  d = copy(_base_dict)
108  d['method'] = 'core.playback.next'
109  r = requests.post(self.url, data=json.dumps(d))
110 
111  def previous(self):
112  if self.is_playing:
113  d = copy(_base_dict)
114  d['method'] = 'core.playback.previous'
115  r = requests.post(self.url, data=json.dumps(d))
116 
117  def stop(self):
118  if self.is_playing:
119  d = copy(_base_dict)
120  d['method'] = 'core.playback.stop'
121  r = requests.post(self.url, data=json.dumps(d))
122  self.is_playing = False
123 
124  def currently_playing(self):
125  if self.is_playing:
126  d = copy(_base_dict)
127  d['method'] = 'core.playback.get_current_track'
128  r = requests.post(self.url, data=json.dumps(d))
129  return r.json()['result']
130  else:
131  return None
132 
133  def set_volume(self, percent):
134  if self.is_playing:
135  d = copy(_base_dict)
136  d['method'] = 'core.mixer.set_volume'
137  d['params'] = {'volume': percent}
138  r = requests.post(self.url, data=json.dumps(d))
139 
140  def lower_volume(self):
141  self.set_volume(self.volume_low)
142 
143  def restore_volume(self):
144  self.set_volume(self.volume_high)
145 
146  def pause(self):
147  if self.is_playing:
148  d = copy(_base_dict)
149  d['method'] = 'core.playback.pause'
150  r = requests.post(self.url, data=json.dumps(d))
151 
152  def resume(self):
153  if self.is_playing:
154  d = copy(_base_dict)
155  d['method'] = 'core.playback.resume'
156  r = requests.post(self.url, data=json.dumps(d))
157 
158  def get_items(self, uri):
159  d = copy(_base_dict)
160  d['method'] = 'core.playlists.get_items'
161  d['params'] = {'uri': uri}
162  r = requests.post(self.url, data=json.dumps(d))
163  if 'result' in r.json():
164  return [e['uri'] for e in r.json()['result']]
165  else:
166  return None
167 
168  def get_tracks(self, uri):
169  tracks = self.browse(uri)
170  ret = [t['uri'] for t in tracks if t['type'] == 'track']
171 
172  sub_tracks = [t['uri'] for t in tracks if t['type'] != 'track']
173  for t in sub_tracks:
174  ret = ret + self.get_tracks(t)
175  return ret
176 
177  def get_local_albums(self):
178  p = self.browse('local:directory?type=album')
179  return {e['name']: e for e in p if e['type'] == 'album'}
180 
181  def get_local_artists(self):
182  p = self.browse('local:directory?type=artist')
183  return {e['name']: e for e in p if e['type'] == 'artist'}
184 
185  def get_local_genres(self):
186  p = self.browse('local:directory?type=genre')
187  return {e['name']: e for e in p if e['type'] == 'directory'}
188 
190  p = self.get_playlists('m3u')
191  return {e['name']: e for e in p}
192 
194  p = self.get_playlists('spotify')
195  return {e['name'].split('(by')[0].strip().lower(): e for e in p}
196 
197  def get_gmusic_albums(self):
198  p = self.browse('gmusic:album')
199  p = {e['name']: e for e in p if e['type'] == 'directory'}
200  return {e.split(' - ')[1]: p[e] for e in p}
201 
203  p = self.browse('gmusic:artist')
204  return {e['name']: e for e in p if e['type'] == 'directory'}
205 
206  def get_gmusic_radio(self):
207  p = self.browse('gmusic:radio')
208  return {e['name']: e for e in p if e['type'] == 'directory'}
def find_album(self, album, filter=None)
Definition: mopidypost.py:51


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