-
Notifications
You must be signed in to change notification settings - Fork 0
/
LegacyBot.py
executable file
·145 lines (116 loc) · 5.58 KB
/
LegacyBot.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
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# This file is part of GrooveBot.
#
# GrooveBot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GrooveBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GrooveBot. If not, see <http://www.gnu.org/licenses/>.
from twisted.internet import reactor, threads, utils
from twisted.internet.task import LoopingCall
from api.grooveshark import GrooveApi
from JlewBot import JlewBotFactory
from VolBot import VolBot
import util
class GrooveBot(VolBot):
# Replace with proper locations
api_inst = GrooveApi('currentSong.txt', 'shortcutAction.txt')
bot_name = "foss_groovebot"
last_msg = ""
song_request_db = {}
def setup(self, f):
# super() for classic classes:
VolBot.setup(self, f)
for command in ['add','remove','show','pause','resume','skip','status','dump','radio']:
f.register_command(command, self.request_queue_song)
def _file_status(self, s, bot, channel):
if s:
msg = "%s: \"%s\" by \"%s\" on \"%s\"" % \
(s['status'], s['title'], s['artist'], s['album'])
if s['status'] == 'stopped':
threads.deferToThread(self.api_inst.auto_play).addErrback(self.err_console)
global last_msg
last_msg = msg
bot.me( channel, msg )
def check_status(self, irc_bot, channel):
if hasattr(irc_bot, 'active_bot') and irc_bot.active_bot:
irc_bot = irc_bot.active_bot
else:
return
threads.deferToThread(api_inst.get_file_status).addCallback(_file_status, irc_bot, channel).addErrback(self.err_console)
def _add_lookup_cb(self, song, responder, user):
if not song:
responder("API Threw Exception, try again")
return
if len(song) == 0:
responder("API returned empty set")
elif song['SongID'] in self.api_inst.played:
responder("Error \"%s\" by \"%s\" has been played" % (\
song['SongName'], song['ArtistName']))
elif song['SongID'] in self.api_inst.queue:
responder('Error "%s" by "%s" is in queue' % (\
song['SongName'], song['ArtistName']))
else:
responder('Queueing %s: "%s" by "%s" on "%s"' % (\
song['SongID'], song['SongName'],
song['ArtistName'], song['AlbumName']))
self.song_request_db[song['SongID']] = user
threads.deferToThread(self.api_inst.queue_song, song['SongID']) \
.addErrback(util.err_chat, responder)
def request_queue_song(self, responder, user, channel, command, msg):
if channel == self.bot_name and command not in ['show', 'dump', 'status', 'vol']:
responder("Let's talk to the class")
return
if command == "add":
responder("Got Request, processing")
threads.deferToThread(self.api_inst.request_song_from_api, msg).addCallback(self._add_lookup_cb, responder, user).addErrback(util.err_chat, responder)
elif command == "remove":
try:
id = int(msg)
if self.api_inst.remove_queue(id):
responder("Removed %s" % id)
else:
responder( "Could not remove %s" % msg )
except:
responder( "Must get an id" )
elif command == "show":
songNames = []
song_db = self.api_inst.song_db
for song_id in self.api_inst.queue:
song = song_db[song_id]
songNames.append('"%s" by "%s"' %(song['SongName'], song['ArtistName']))
responder(', '.join(songNames))
elif command == "dump":
song_db = self.api_inst.song_db
for song_id in self.api_inst.queue:
song = song_db[song_id]
responder( '%d [%s]: "%s" by "%s" on "%s"' % ( id,
self.song_request_db[song_id], song['SongName'],
song['ArtistName'], song['AlbumName']))
elif command == "pause":
threads.deferToThread(self.api_inst.api_pause).addCallback(util.ok, responder).addErrback(util.err_chat, responder)
elif command == "resume":
threads.deferToThread(self.api_inst.api_play).addCallback(util.ok, responder).addErrback(util.err_chat, responder)
elif command == "skip":
threads.deferToThread(self.api_inst.api_next).addCallback(util.ok, responder).addErrback(util.err_chat, responder)
elif command == "radio":
if msg == "on":
threads.deferToThread(self.api_inst.api_radio_on).addCallback(util.ok, responder).addErrback(util.err_chat, responder)
elif msg == "off":
threads.deferToThread(self.api_inst.api_radio_off).addCallback(util.ok, responder).addErrback(util.err_chat, responder)
elif command == "status":
responder(last_msg)
if __name__ == '__main__':
bot = GrooveBot()
f = JlewBotFactory(protocol=GrooveBot)
bot.setup(f)
reactor.connectTCP("irc.freenode.net", 6667, f)
lc = LoopingCall(check_status, f, "#rit-groove").start( 2 )
reactor.run()