reddit/slashdot RSS IRC Bot

Just wanted to separate the below post a bit and I pulled this bot script out to do so. It simply grabs the RSS feed from the given list of URLs and posts any changes to a given channel.

rbot.py

import os
import re
import select
import urllib
import sys
import time

import lbot

def fechwebs():
	readdata = ""
	urlnlist = ["www.reddit.com/.rss", "rss.slashdot.org/Slashdot/slashdot"]
	resllist = []
	
	xmlshead = ""
	xmlslink = ""
	
	for urlnitem in urlnlist:
		uobjdata = urllib.urlopen("http://" + urlnitem)
		readdata += uobjdata.read()
	
	readdata = readdata.replace("\r", "")
	readdata = readdata.replace("\n", "")
	readdata = readdata.replace("<", "\n<")
	
	datalist = readdata.split("\n")
	
	for dataitem in datalist:
		regxobjc = re.match("<title>(.*)", dataitem)
		
		if (regxobjc):
			xmlshead = ("[" + regxobjc.group(1) + "]")
		
		regxobjc = re.match("<link>(.*)", dataitem)
		
		if (regxobjc):
			xmlslink = regxobjc.group(1)
		
		if ((xmlshead != "") and (xmlslink != "")):
			resllist.append(xmlshead + " " + xmlslink)
			
			xmlshead = ""
			xmlslink = ""
	
	if (len(resllist) == 0):
		return -1
	
	return resllist

def ircbmain():
	if (len(sys.argv) < 4):
		print("Usage: %s <nick> - <chan 0> ... <chan n>" % (sys.argv[0]))
		sys.exit(0)
	
	nickname = ""
	chanlist = []
	
	x = 1
	l = len(sys.argv)
	f = 0
	
	while (x < l):
		if (sys.argv[x] == "-"):
			f += 1
		elif (f == 0):
			nickname = sys.argv[x]
		elif (f == 1):
			chanlist.append(sys.argv[x])
		x += 1
	
	webstime = 0
	webswait = (5 * 60)
	pastdata = -1
	presdata = -1
	
	lbot.ircbinit(nickname, chanlist)
	
	while (1):
		prestime = time.time()
		difftime = (prestime - webstime)
		
		if (difftime > webswait):
			print(lbot.formtime() + " WEB [INFO] Checking site(s)...")
			presdata = fechwebs()
			
			if (type(pastdata) == type(-1)):
				pastdata = presdata
			
			webstime = prestime
		
		ircdstri = lbot.ircbmain(pastdata, presdata)
		
		if (type(ircdstri) == type(-1)):
			break
		
		if (type(presdata) != type(-1)):
			pastdata = presdata

ircbmain()

2 thoughts on “reddit/slashdot RSS IRC Bot

Leave a comment