-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeejay.py
81 lines (61 loc) · 2.47 KB
/
deejay.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
from ibid.plugins import Processor, match
from ibid.utils import human_join
try:
import json
except ImportError:
import simplejson as json
import urllib2
features = {}
features['deejay'] = {
'description': u'Get current playing song on the foulab music player.',
'categories': ('fun',),
}
class Deejay(Processor):
usage = u'!dj'
features = ('deejay',)
addressed = False
def get_title(self):
stream_data = {"jsonrpc": "2.0", "id": 1, "method": "core.playback.get_stream_title"}
track_data = {"jsonrpc": "2.0", "id": 1, "method": "core.playback.get_current_track"}
track_info = u'Well that was unfortunate...'
req = urllib2.Request('http://melody.lan:6680/mopidy/rpc')
req.add_header('Content-Type', 'application/json')
#Try first requesting for stream song info
try:
response = urllib2.urlopen(req, json.dumps(stream_data))
except urllib2.URLError, e:
return u'Melody is currently not available.'
result = response.read()
response.close()
result = json.loads(result)
#Check for track title if stream info empty
if result['result'] == None:
del(req)
del(response)
req = urllib2.Request('http://melody.lan:6680/mopidy/rpc')
req.add_header('Content-Type', 'application/json')
try:
response = urllib2.urlopen(req, json.dumps(track_data))
except urllib2.URLError, e:
return u'Melody is currently not available.'
result = response.read()
response.close()
result = json.loads(result)
if result['result'] == None:
track_info = u'Nothing is currently playing.'
else:
info = u'Well that was unfortunate...'
result_keys = result['result'].keys()
if 'artists' in result_keys:
info = u'%s - %s' % ( result['result']['name'], result['result']['artists'][0]['name'] )
else:
info = result['result']['name']
track_info = u'Curently playing: %s' % info
else:
result = result['result']
track_info = u'Curently playing: %s' % result
return track_info
@match(r'^!dj$')
def dj_playing(self, event):
response = self.get_title()
event.addresponse(response)