Skip to content

Instantly share code, notes, and snippets.

@moustaki
Created October 27, 2009 00:16
Show Gist options
  • Save moustaki/219164 to your computer and use it in GitHub Desktop.
Save moustaki/219164 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
from pyechonest import artist, config
from ircbot import SingleServerIRCBot
from irclib import nm_to_n, nm_to_h, irc_lower, ip_numstr_to_quad, ip_quad_to_numstr
from random import *
import urllib2
import socket
import twitter
timeout = 2
socket.setdefaulttimeout(timeout)
class TestBot(SingleServerIRCBot):
def __init__(self, channel, nickname, server, port=6667):
self.api = twitter.Api(username = 'matwod', password='password')
SingleServerIRCBot.__init__(self, [(server, port)], nickname, nickname)
self.channel = channel
self.random = Random()
def on_nicknameinuse(self, c, e):
c.nick(c.get_nickname() + "_")
def on_welcome(self, c, e):
c.join(self.channel)
def on_privmsg(self, c, e):
self.do_command(e, " ".join(e.arguments()))
def on_pubmsg(self, c, e):
cmd = e.arguments()[0]
a = e.arguments()[0].split(":", 1)
if (cmd.startswith('http://')):
self.do_command(e, cmd)
elif len(a) > 1 and irc_lower(a[0]) == irc_lower(self.connection.get_nickname()):
self.do_command(e, a[1])
def on_dccmsg(self, c, e):
c.privmsg("You said: " + e.arguments()[0])
def on_dccchat(self, c, e):
if len(e.arguments()) != 2:
return
args = e.arguments()[1].split()
if len(args) == 4:
try:
address = ip_numstr_to_quad(args[2])
port = int(args[3])
except ValueError:
return
self.dcc_connect(address, port)
def do_command(self, e, cmd):
status = self.api.PostUpdate(cmd)
self.connection.privmsg(self.channel, "Just tweeted: http://twitter.com/matwod/status/" + str(status.id))
def main():
import sys
if len(sys.argv) != 4:
print "Usage: testbot <server[:port]> <channel> <nickname>"
sys.exit(1)
s = sys.argv[1].split(":", 1)
server = s[0]
if len(s) == 2:
try:
port = int(s[1])
except ValueError:
print "Error: Erroneous port."
sys.exit(1)
else:
port = 6667
channel = sys.argv[2]
nickname = sys.argv[3]
bot = TestBot(channel, nickname, server, port)
bot.start()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment