All pastes #128184 Raw Edit

Anonymous

public text v1 · immutable
#128184 ·published 2006-08-12 20:00 UTC
rendered paste body
#!/usr/bin/env python

from ircbot import SimpleIRCClient 
import irclib
import os
import feedparser
import re
import urllib
import time
import sys

class HorseNews(SimpleIRCClient):
	basenick = 'HorseNews' 
	trigger = '!horse'
	username = "penisbird"
	channels = ["#test", "#chirp"]
	feedurl = 'http://news.google.com/news?hl=en&ned=us&ie=UTF-8&q=HORSE+-trojan&btnG=Search+News&output=rss'
	burst = 1
	
	nick = basenick
	d = ""
	linkcache = []
	lasthorse = {}
	lasthorseupdate = time.time() + 999999

	def __init__(self):
		SimpleIRCClient.__init__(self)
		self.connect()
		while 1:
			if time.time() - self.lasthorseupdate >= 300:
				self.Horse()
				self.lasthorseupdate = time.time()
				if self.burst == 0:
					self.burst = 1;
					continue
				self.MsgHorseUpdates()
			self.ircobj.process_once(.2)
	
	def Horse(self):
		self.d = feedparser.parse(self.feedurl)

	def cleanURL(self, dirtyURL):
		clean = urllib.unquote(dirtyURL)
		whinny = re.search("^http://news.+url=(http://.+)&cid=.+&ei=.+$",\
		clean)
		if(whinny):
			clean = whinny.group(1)
		return clean
	
	def cleanTitle(self, dirtyTitle):
		clean = re.sub(" -[^-]+$", "", dirtyTitle)
		return clean

	def MsgHorseUpdates(self):
		newupdates = []
                self.d = feedparser.parse(self.feedurl)
                for entry in self.d.entries:
                        incache = 0
                        for cacheentry in self.linkcache:
                                if self.cleanURL(entry.link) == cacheentry:
                                        incache = 1
                                        break;
                                else:
                                        incache = 0
			if incache == 0:
				newupdates.append(self.cleanTitle(entry.title) + " -> " +\
				self.cleanURL(entry.link))
				self.linkcache.append(self.cleanURL(entry.link))

		if len(newupdates) > 0:
			for channel in self.channels:
				self.connection.privmsg\
				(channel, "7,5/!!!\ BREAKING " + self.basenick +" /!!!\")
	
				for update in newupdates:
					self.connection.privmsg(channel, update.encode("UTF-8"))

	def connect(self):
		SimpleIRCClient.connect(self, "irc.horsehater.org", 6667, self.nick, "", self.username)
	
	def on_disconnect(self, c, e):
		print "Warning: Disconnected, attempting reconnect"
		self.connect()				
	
	def on_ctcp(self, c, e):
		if e.arguments()[0] == "VERSION":
			c.ctcp_reply(irclib.nm_to_n(e.source()), "VERSION " + self.get_version())
		elif e.arguments()[0] == "PING":
			if len(e.arguments()) > 1:
				c.ctcp_reply(nm_to_n(e.source()),"PING " + e.arguments()[1])

	def on_welcome(self, c, e):
		for chan in self.channels:
			c.join(chan)
			self.lasthorse[chan] = time.time()-30
	
	def on_pubmsg(self, c, e):
		# if "!horse"
		if e.arguments()[0].lower() == self.trigger.lower()\
		and time.time() - self.lasthorse[e.target().lower()] >= 30:

			if time.time() - self.lasthorseupdate >= 300:
				self.Horse()
				self.lasthorseupdate = time.time()
			self.lasthorse[e.target().lower()] = time.time()
			self.MsgHorses(e.target(), -1)
			return
		#if !horse [digits]
		neigh = re.search('^'+self.trigger.lower()+' ([0-9]+)$',\
		e.arguments()[0].lower())
		if neigh:
			self.MsgHorses(e.target(), int(neigh.group(1)))

	def MsgHorses(self, target, index):
		if index == -1:
			i = 0
			for entry in self.d.entries:
				self.connection.privmsg(target, str(i) + ". " + \
				self.cleanTitle(entry.title).encode("UTF-8"))
				i = i + 1
		else:
			try:
				self.connection.privmsg\
				(target, self.cleanURL(self.d.entries[index].link).encode("UTF-8"))
			except AttributeError:
				return
			except IndexError:
				return

	def on_join(self, c, e):
		for channel in self.channels:
			if e.target().lower() == channel.lower():
				self.lasthorse[e.target().lower()] = time.time()
				c.mode(e.target() + " +qo", c.get_nickname() + " " + c.get_nickname())
				if channel == self.channels[len(self.channels)-1]:
					self.lasthorseupdate = time.time()-300
				return
		c.part(e.target())

	def get_version(self):
		return sys.argv[0] + "v1.0 running on " + os.uname()[0] + " " +  os.uname()[1] \
		+ " " + os.uname()[2] + " " +  os.uname()[3] + " " +  os.uname()[4]
		
Horse = HorseNews()