| 216 | |
| 217 | |
| 218 | class MPDWrapper(object): |
| 219 | def __init__(self, server="localhost", port=6600): |
| 220 | """ |
| 221 | Prepare the client and music variables |
| 222 | """ |
| 223 | self.server = server |
| 224 | self.port = port |
| 225 | |
| 226 | # prepare client |
| 227 | self.client = mpd.MPDClient() |
| 228 | self.client.timeout = None |
| 229 | self.client.idletimeout = None |
| 230 | self.client.connect(self.server, self.port) |
| 231 | |
| 232 | # gather playlists |
| 233 | self.playlists = [x["playlist"] for x in self.client.listplaylists()] |
| 234 | |
| 235 | # gather songs |
| 236 | self.client.clear() |
| 237 | for playlist in self.playlists: |
| 238 | self.client.load(playlist) |
| 239 | |
| 240 | self.songs = [] # may have duplicates |
| 241 | # capitalized strings |
| 242 | self.song_titles = [] |
| 243 | self.song_artists = [] |
| 244 | |
| 245 | soup = self.client.playlist() |
| 246 | for i in range(0, len(soup) / 10): |
| 247 | index = i * 10 |
| 248 | id = soup[index].strip() |
| 249 | title = soup[index + 3].strip().upper() |
| 250 | artist = soup[index + 2].strip().upper() |
| 251 | album = soup[index + 4].strip().upper() |
| 252 | |
| 253 | self.songs.append(Song(id, title, artist, album)) |
| 254 | |
| 255 | self.song_titles.append(title) |
| 256 | self.song_artists.append(artist) |
| 257 | |
| 258 | @reconnect |
| 259 | def play(self, songs=False, playlist_name=False): |
| 260 | """ |
| 261 | Plays the current song or accepts a song to play. |
| 262 | |
| 263 | Arguments: |
| 264 | songs -- a list of song objects |
| 265 | playlist_name -- user-defined, something like "Love Song Playlist" |
| 266 | """ |
| 267 | if songs: |
| 268 | self.client.clear() |
| 269 | for song in songs: |
| 270 | try: # for some reason, certain ids don't work |
| 271 | self.client.add(song.id) |
| 272 | except: |
| 273 | pass |
| 274 | |
| 275 | if playlist_name: |