forked from codyfish/ulauncher-mpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MpdController.py
137 lines (110 loc) · 4.15 KB
/
MpdController.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
from mpd import MPDClient
from mpd import MPDError
import os
import QueryParser
from MpdData import Action
from MpdData import CommandData
import AlbumArtCache
import Results
import MpdHelper
class MpdController:
def __init__(self):
self.__client = MPDClient()
self.__hostname = "localhost"
self.__port = 6600
#music_dir = os.environ['XDG_MUSIC_DIR']
#print("initial music dir: " + music_dir)
#self.__album_art_cache = AlbumArtCache.init_default_cache(music_dir)
# connect later anyways
# self.__client.connect("localhost", 6600)
def get_mpd_version(self):
return self.__client.mpd_version
def set_mpd_host(self, hostname):
self.__hostname = hostname
def set_mpd_port(self, port):
self.__port = int(port)
def set_music_dir(self, music_dir):
print("music dir: " + music_dir)
self.__album_art_cache = AlbumArtCache.init_default_cache(music_dir)
def ensure_connection(self):
# Alternative: Async thread pinging server
try:
self.__client.ping()
return True
except MPDError:
try:
self.__client.connect(self.__hostname, self.__port)
return True
except:
return False
def query(self, query):
if query is None:
return Results.list_commands()
# print query
query_segments = query.lower().split(" ", 1)
command = query_segments[0]
if len(query_segments) == 2:
args = query_segments[1]
else:
args = None
command_suggestions = QueryParser.parse_command(command)
print(command_suggestions)
if args is not None and len(command_suggestions) == 1:
if not self.ensure_connection():
return
return Results.list_music(self.__client, self.__album_art_cache, command_suggestions[0], args)
else:
return Results.list_commands(command_suggestions)
def execute(self, item_data):
if not self.ensure_connection():
return
action = item_data.action
args = item_data.data
playlist_length = self.__client.status()['playlistlength']
print(playlist_length)
if action is Action.NONE:
# Wait for user input
pass
elif action is Action.TOGGLE_PLAY:
self.__client.pause()
elif action is Action.PLAY:
self.__client.pause(0)
elif action is Action.PAUSE:
self.__client.pause(1)
elif action is Action.NEXT:
self.__client.next()
elif action is Action.PREVIOUS:
self.__client.previous()
elif action is Action.CLEAR:
self.__client.clear()
# return in order not to play invalid index as cleared queue has status "stop"
return
elif action is Action.ADD_SONG:
self.__client.add(args)
elif action is Action.ADD_ALBUM:
self.__client.findadd('album', args)
elif action is Action.ADD_ARTIST:
self.__client.findadd('artist', args)
elif action is Action.ADD_FOLDER:
self.__client.add(args)
elif action is Action.ADD_PLAYLIST:
self.__client.load(args)
elif action is Action.INSERT_SONG:
MpdHelper.insert_song(self.__client, args)
elif action is Action.INSERT_ALBUM:
MpdHelper.insert_album(self.__client, args)
elif action is Action.INSERT_ARTIST:
MpdHelper.insert_artist(self.__client, args)
elif action is Action.INSERT_FOLDER:
MpdHelper.insert_folder(self.__client, args)
elif action is Action.INSERT_PLAYLIST:
MpdHelper.insert_playlist(self.__client, args)
elif action is Action.ADD_TO_PLAYLIST:
song = self.__client.currentsong()
if song:
self.__client.playlistadd(args, song['file'])
# Avoid playing
return
# start playing if not playing
if self.__client.status()['state'] == "stop":
self.__client.play(playlist_length)