From 35406d4fc7bf83c954b225747ac6c13a6aad2994 Mon Sep 17 00:00:00 2001 From: lassulus Date: Thu, 9 Jan 2014 20:27:00 +0100 Subject: ircbot: better looping --- ircbot/rssbot.py | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) (limited to 'ircbot') diff --git a/ircbot/rssbot.py b/ircbot/rssbot.py index 45af1dc3..79276a3a 100755 --- a/ircbot/rssbot.py +++ b/ircbot/rssbot.py @@ -41,26 +41,28 @@ class RssBot(irc.bot.SingleServerIRCBot): self.loop = False def updateloop(self): - try: - self.feed = feedparser.parse(self.url) - except: - print(self.name + ': rss timeout occured') - for entry in self.feed.entries: - self.oldnews.append(entry.link) + while True: + try: + self.feed = feedparser.parse(self.url) + for entry in self.feed.entries: + self.oldnews.append(entry.link) + break + except: + print(self.name + ': rss timeout occured') while self.loop: try: self.feed = feedparser.parse(self.url) + for entry in self.feed.entries: + if not entry.link in self.oldnews: + #try: + # self.send(entry.title + " " + entry.link + " com: " + entry.comments) + #except AttributeError: + shorturl = self.shortenurl(entry.link) + self.sendall(entry.title + ' ' + shorturl) + self.oldnews.append(entry.link) + self.lastnew = datetime.now() except: print(self.name + ': rss timeout occured') - for entry in self.feed.entries: - if not entry.link in self.oldnews: - #try: - # self.send(entry.title + " " + entry.link + " com: " + entry.comments) - #except AttributeError: - shorturl = self.shortenurl(entry.link) - self.send(entry.title + ' ' + shorturl) - self.oldnews.append(entry.link) - self.lastnew = datetime.now() sleep(self.to) def shortenurl(self, url): @@ -72,25 +74,27 @@ class RssBot(irc.bot.SingleServerIRCBot): print('url shortener error') sleep(1) - def last(self, num): - for feed in reversed([x for x in reversed(self.feed.entries)][:num]): - self.send(feed.title + ' ' + self.shortenurl(feed.link)) + def last(self, target, num): + for feed in [x for x in self.feed.entries][:num]: + self.send(target, feed.title + ' ' + self.shortenurl(feed.link)) + + def sendall(self, string): + for chan in self.channels: + self.send(chan, string) - def send(self, string): + def send(self, target, string): if self.connection.connected: for line in string.split('\n'): if len(line) < 450: - for chan in self.channels: - self.connection.privmsg(chan, line) + self.connection.privmsg(target, line) + sleep(1) else: space = 0 for x in range(math.ceil(len(line)/400)): oldspace = space space = line.find(" ", (x+1)*400, (x+1)*400+50) - for chan in self.channels: - self.connection.privmsg(chan, line[oldspace:space]) + self.connection.privmsg(target, line[oldspace:space]) sleep(1) - sleep(1) else: self.connection.reconnect() sleep(1) -- cgit v1.2.3