/usr/share/pyshared/gplugs/shoutcast.py is in gozerbot 0.99.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | # plugs/shoutcast.py
#
#
""" shoutcast watcher """
__copyright__ = 'this file is in the public domain'
from gozerbot.persist.persist import Persist
from gozerbot.commands import cmnds
from gozerbot.generic import geturl, striphtml, rlog, lockdec
from gozerbot.datadir import datadir
from gozerbot.fleet import fleet
from gozerbot.examples import examples
from gozerbot.threads.thr import start_new_thread
from gozerbot.plughelp import plughelp
from gozerbot.persist.persistconfig import PersistConfig
import os, time, thread
plughelp.add('shoutcast', 'query a shoutcast server or periodically watch \
them')
cfg = PersistConfig()
cfg.define('scwatch', 0)
cfg.define('nodes', [])
sclock = thread.allocate_lock()
locked = lockdec(sclock)
class SCwatcher(object):
def __init__(self):
self.stop = False
self.songsplayed = []
def run(self):
self.starttime = int(time.time())
res = ""
while cfg.get('scwatch') and not self.stop:
time.sleep(1)
if self.stop:
break
godo = []
for botname, channel, name, node, polltime in cfg.get('nodes'):
if not (int(time.time()) - self.starttime) % int(polltime):
godo.append((botname, channel, name, node))
continue
if godo:
rlog(0, 'shoutcast', 'running scan: %s' % str(godo))
start_new_thread(self.doscan, (godo, ))
@locked
def doscan(self, scanlist):
for botname, channel, name, node in scanlist:
try:
result = geturl('http://%s/7.html' % node)
except Exception, ex:
rlog(10, 'shoutcast', "can't get %s shoutcast data: %s" % \
(node, str(ex)))
continue
try:
res = result.split(',')[6]
except IndexError:
rlog(10, 'shoutcast', "can't match %s shoutcast data" % node)
continue
song = striphtml(res).strip().replace('\n', '')
bot = fleet.byname(botname)
if bot and channel in bot.state['joinedchannels']:
got = False
for ttime, played in self.songsplayed:
if played == song:
got = True
if not got:
self.songsplayed.append((time.time(), song))
bot.say(channel, "now playing on %s: %s" % (name, song))
else:
for ttime, played in self.songsplayed:
if time.time() - ttime > 1800:
self.songsplayed.remove((ttime, played))
scwatcher = SCwatcher()
def init():
if cfg.get('scwatch'):
start_new_thread(scwatcher.run, ())
return 1
def shutdown():
rlog(10, 'shoutcast', 'shutting down watcher')
scwatcher.stop = True
def handle_sc(bot, ievent):
try:
server = ievent.args[0]
except IndexError:
ievent.missing('<server>')
return
try:
result = geturl('http://%s/7.html' % server)
except Exception, ex:
ievent.reply("can't get shoutcast data: %s" % str(ex))
return
try:
res = result.split(',')[6]
except IndexError:
ievent.reply("can't extract shoutcast data")
return
ievent.reply(striphtml(res).strip())
cmnds.add('sc', handle_sc, 'USER')
examples.add('sc', 'sc <host:port> .. ask server:port for currently running \
song', 'sc stream1.jungletrain.net:8000')
def handle_sclist(bot, ievent):
ievent.reply("shoutcast nodes: %s" % cfg.get('nodes'))
cmnds.add('sc-list', handle_sclist, 'OPER')
examples.add('sc-list', 'show list of watched shoutcast servers', 'sc-list')
def handle_scadd(bot, ievent):
try:
name, node, polltime = ievent.args
except ValueError:
ievent.missing('<name> <host:port> <polltime>')
return
try:
polltime = int(polltime)
except ValueError:
ievent.reply('polltime needs to be an integer')
return
if polltime < 60:
ievent.reply('polltime in min. 60 seconds')
return
if not cfg.get('scwatch'):
cfg.set('scwatch', 1)
scwatcher.stop = False
start_new_thread(scwatcher.run, ())
cfg.append('nodes', [bot.name, ievent.channel, name, node, polltime])
cfg.save()
ievent.reply('%s added' % node)
cmnds.add('sc-add', handle_scadd, 'OPER')
examples.add('sc-add', 'sc-add <server:port> .. add server to watcher', \
'sc-add jungletrain stream1.jungletrain.net 180')
def handle_scdel(bot, ievent):
try:
name = ievent.args[0].lower()
except IndexError:
ievent.missing('<name>')
return
got = 0
nodes = cfg.get('nodes')
for i in range(len(nodes)-1, -1, -1):
if nodes[i][2].lower() == name:
del nodes[i]
got = 1
if got:
cfg.save()
ievent.reply('%s deleted' % name)
else:
ievent.reply('%s is not in nodeslist' % name)
cmnds.add('sc-del', handle_scdel, 'OPER')
examples.add('sc-del', 'sc-del <name> .. remove node <name> from watcher', \
'sc-del jungletrain')
def handle_scsetpolltime(bot, ievent):
try:
name, sec = ievent.args
sec = int(sec)
except ValueError:
ievent.missing('<name> <secondstosleep>')
return
if sec < 60:
ievent.reply('minimun is 60 seconds')
return
name = name.lower()
got = False
for i in cfg.get('nodes'):
if i[2].lower() == name:
i[4] = sec
got = True
if got:
cfg.save()
ievent.reply('poll time set to %s' % sec)
else:
ievent.reply('%s is not in nodes list' % name)
cmnds.add('sc-setpolltime', handle_scsetpolltime, 'OPER')
examples.add('sc-setpolltime', 'set poll time of the shoutcast watcher', \
'sc-setpolltime jungletrain 120')
def handle_scpolltime(bot, ievent):
try:
name = ievent.args[0].lower()
except IndexError:
ievent.missing('<name>')
return
for i in cfg.get('nodes'):
if i[2].lower() == name:
ievent.reply("polltime of %s is %s" % (name, i[4]))
return
cmnds.add('sc-polltime', handle_scpolltime, 'OPER')
examples.add('sc-polltime', 'get polltime of <name>', 'sc-polltime \
jungletrain')
def handle_scstartwatch(bot, ievent):
if cfg.get('scwatch'):
ievent.reply('shoutcast watcher is already running')
return
scwatcher.stop = False
cfg.set('scwatch', 1)
start_new_thread(scwatcher.run, ())
ievent.reply('watcher started')
cmnds.add('sc-startwatch', handle_scstartwatch, 'OPER')
examples.add('sc-startwatch', 'start the shoutcast watcher', 'sc-startwatch')
def handle_scstopwatch(bot, ievent):
scwatcher.stop = True
cfg.set('scwatch', 0)
ievent.reply('watcher stopped')
cmnds.add('sc-stopwatch', handle_scstopwatch, 'OPER')
examples.add('sc-stopwatch', 'stop the shoutcast watcher', 'sc-stopwatch')
|