-
Notifications
You must be signed in to change notification settings - Fork 1
/
playmp3.py
78 lines (61 loc) · 1.82 KB
/
playmp3.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
# -*- coding: utf-8-*-
#Written by Silvério Santos
import re
#import json
from mpd import MPDClient
WORDS = ["PLAY"]
PRIORITY = 1
def handle(text, mic, profile):
mpdCl = get_mpdClient()
# User decides to play or stop
mic.say("What do you want to do: play, stop or update?")
action = mic.activeListen()
if action == "PLAY":
#Search for artist and title
found = mpd_search(mpdCl, mic)
if len(found) == 0:
# If not found: error message
mic.say("No titles found.")
else:
# If found: play it
mpd_play(mpdCl, mic, found)
elif action == "UPDATE":
mpd_update(mpdCl, mic)
else:
mpd_stop(mpdCl, mic)
close_mpdmpdCl(mpdCl)
def get_mpdClient():
mpdCl = MPDClient()
mpdCl.timeout = 10
mpdCl.idletimeout = None
mpdCl.connect("localhost", 6600)
return mpdCl
def mpd_search(mpdCl, mic):
# User to say the artist
mic.say("What artist would you like to hear music from?")
artist = mic.activeListen()
# User to say the title
mic.say("What title would you like to hear?")
title = mic.activeListen()
mic.say("Searching for title " + title + " from " + artist)
return mpdCl.search("artist", artist, "title", title)
def mpd_play(mpdCl, mic, found):
if len(found) > 0:
mic.say("Playing")
mpdCl.clear()
mpdCl.playid(mpdCl.addid(found[0]['file']))
def mpd_update(mpdCl, mic):
mic.say("Updating song database...")
mpdCl.update()
def mpd_stop(mpdCl, mic):
mpdCl.stop()
mic.say("Stopped mp3 playback")
def close_mpdmpdCl(mpdCl):
mpdCl.close()
mpdCl.disconnect()
def isValid(text):
play = bool(re.search(r'\bPlay\b',text, re.IGNORECASE))
if play:
return True
else:
return False