-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
executable file
·199 lines (182 loc) · 7.81 KB
/
bot.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# kongo 140602
# hallberg 141008
import sys
import socket
import string
import time
import threading
import serial.serialposix
import mpd
HOST="efnet.port80.se"
PORT=6667
NICK="MrCoffee"
IDENT="mrcoffee"
REALNAME="Mr. Coffee"
CHANNEL="#etf"
def cutetime(epoch):
st = time.localtime(epoch)
ast = time.gmtime(time.time() - epoch)
ago = []
if time.time() - epoch > 60*60*24*28:
ago.append('en evighet')
else:
if ast.tm_mday > 2:
ago.append('%d dagar' % (ast.tm_mday-1))
elif ast.tm_mday == 2:
ago.append('1 dag')
if ast.tm_hour > 1:
ago.append('%d timmar' % ast.tm_hour)
elif ast.tm_hour == 1:
ago.append('1 timme')
if ast.tm_min > 1:
ago.append('%d minuter' % ast.tm_min)
elif ast.tm_min == 1:
ago.append('1 minut')
if len(ago) == 0:
ago = 'typ nu'
elif len(ago) == 1:
ago = ago[0] + ' sedan'
else:
ago = ', '.join(ago[:-1]) + ' och ' + ago[-1] + ' sedan'
if time.time() - epoch > 60*60*24:
t = time.strftime('%d/%m %H:%M', st)
else:
t = time.strftime('%H:%M', st)
return t + ', dvs. ' + ago
def reply(s, line, msg):
if line[2] == CHANNEL:
dst = CHANNEL
else:
dst = line[0][1:].split('!')[0]
s.send('PRIVMSG %s :%s\r\n' % (dst, msg))
class CoffeeThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.sock = None
self.last_start = 0
self.last_stop = 0
self.daemon = True
def run(self):
s = serial.serialposix.PosixSerial(0)
s.setDTR(True)
s.setRTS(False)
time.sleep(30)
last = s.getRI()
if self.sock:
self.sock.send("PRIVMSG %s :%s\r\n" % (CHANNEL, "herro everyone!"))
while True:
if last ^ s.getRI():
if s.getRI():
msg = "it's on!"
self.last_start = time.time()
else :
self.last_stop = time.time()
msg = "kaffet e klart! (det tog %d sekunder)" % (self.last_stop-self.last_start)
try:
if self.sock:
self.sock.send("PRIVMSG %s :%s\r\n" % (CHANNEL, msg))
except:
self.sock = None
last = s.getRI()
time.sleep(2)
if __name__ == '__main__':
c = CoffeeThread()
c.start()
l = file('/tmp/bot.log','w')
while True:
readbuffer=""
try:
l.write('Connecting to %s %s\n'%(HOST,PORT))
l.flush()
s = socket.socket( )
s.connect((HOST, PORT))
s.send("NICK %s\r\n" % NICK)
s.send("USER %s %s bla :%s\r\n" % (IDENT, HOST, REALNAME))
s.send("JOIN %s\r\n" % CHANNEL)
c.sock = s
while True:
readbuffer=readbuffer+s.recv(1024)
temp=string.split(readbuffer, "\n")
readbuffer=temp.pop( )
for line in temp:
line=string.rstrip(line)
line=string.split(line)
#print line
l.write('%s\n' % line)
l.flush()
if line[0] == 'PING':
s.send("PONG %s\r\n" % line[1])
elif len(line) >= 4 and line[1] == 'PRIVMSG':
if line[3] == ':!kaffe':
if c.last_start == 0:
msg = 'vilket kaffe? hurr durr.'
elif c.last_start > c.last_stop:
msg = 'snart nytt kaffe... (startad %s)' % cutetime(c.last_start)
else:
msg = 'senaste kaffet bryggdes %s. det tog %d sekunder.' % (cutetime(c.last_stop), c.last_stop - c.last_start)
reply(s, line, msg)
elif line[3] == ':!what':
try:
m = mpd.MPDConn()
stat = m.query('status', True)
reply(s, line, 'state: %s' % (stat['state'] if 'state' in stat else 'N/A'))
cs = m.query('currentsong', True)
if 'Title' in cs:
reply(s, line, 'title: %s' % cs['Title'])
if 'Name' in cs:
reply(s, line, 'name: %s' % cs['Name'])
del m
except Exception as e:
reply(s, line, 'det gick fel.')
file('/tmp/bot.last','w').write(str(e))
elif line[3] == ':!url':
try:
m = mpd.MPDConn()
m.cmd('clear')
m.cmd('add ' + line[4])
m.cmd('play')
time.sleep(2)
cs = m.query('currentsong', True)
reply(s, line, 'spelas nu: ' + (cs['Name'] if 'Name' in cs else '<wtf>'))
del m
except Exception as e:
reply(s, line, 'attans.')
file('/tmp/bot.last','w').write(str(e))
elif line[3] == ':!help':
reply(s, line, '!kaffe, !play, !pause, !stop, !what, !rt <kanal>, !di <kanal>, !sr <kanal>, !blipblop, !jul, !awesome, !kohina, !slay, !url <url>')
elif line[3][2:] in ('stop','play','pause'):
try:
mpd.MPDConn().cmd(line[3][2:])
except Exception as e:
reply(s, line, 'aw.')
file('/tmp/bot.last','w').write(str(e))
elif line[3][2:] in ('rt','di','sr','blipblop','awesome','kohina','slay','jul'):
try:
streambase = {'rt': 'pub2.radiotunes.com/radiotunes_',
'di': 'pub6.di.fm/di_',
'sr': 'http-live.sr.se/',
'blipblop': 'radio.mojt.net/blop.php?',
'jul': 'radio.mojt.net/merjul.php?',
'awesome': 'radio.mojt.net/awesome.php?',
'kohina': 'kohina.radio.ethz.ch:8000/kohina.ogg?',
'slay': 'relay1.slayradio.org:8000/?'}
m = mpd.MPDConn()
m.cmd('clear')
m.cmd('add http://%s%s%s' % (streambase[line[3][2:]], line[4] if len(line) >= 5 else '', '-mp3-192' if 'sr.se/' in streambase[line[3][2:]] else ''))
m.cmd('play')
time.sleep(2)
cs = m.query('currentsong', True)
reply(s, line, 'spelas nu: ' + (cs['Name'] if 'Name' in cs else '<wtf>'))
del m
except Exception as e:
reply(s, line, 'attans.')
file('/tmp/bot.last','w').write(str(e))
except Exception as e:
c.sock = None
print 'Exception in connection loop, restarting'
l.write('Exception in connection loop, restarting: %s\n'%e)
l.flush()
#pass #yeah!
time.sleep(30)