2

i been trying to code an IRC bot, while i have succeed. I am having problems implementing something i want to do. the code works fine, but i have issues in the following:

since the bot uses a While loop to read commands from the IRC when i add a second While with a time.sleep(seconds) the bot does not connect because it reads my second loop and pauses the connection not in time to response the :PING do it disconnects. i been searching but the mor ei search the more confused i get because i don't know what should i try.

stackless, multithreads, subprocess. there are so many results that i just get more confused. so what would be the best method i am trying an RSS bot the bot works fine if i use the command !rss in the IRC channel but i need it to check for new ones ever 10 minutes and if use a sleep command the main loop messes up.

here is my code:

#!/usr/bin/python

import socket, sys, string, time, feedparser, hashlib
port = 6667
nick = "RSSbot"
host = 'irc.server.com'
name =  "RSSBOT"
channel = '#debug'
ident = 'rssbot'
irc = socket.socket()
irc.connect ( (host, port) )
irc.send ( 'NICK ' + nick + '\r\n' )
irc.send ( 'USER ' + ident + ' ' +  ident + ' ' + ident + ' :rssbot\r\n' )

def readRss():
    feedurl = feedparser.parse("http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=username")
    newest = feedurl['items'][0].title
    newest = newest.replace("username:","")
    msg = newest.split("http://")
    title = msg[0]
    url = msg[1]
    url = "http://" + url
    e = feedurl.entries[2]
    threadurl = e.link
    id = hashlib.md5(url + title).hexdigest()
    irc.send ("PRIVMSG #debug :Tittle:%s\r\n" % newest)
    irc.send ("PRIVMSG #debug :URL: %s\r\n" % url)
    irc.send ("PRIVMSG #debug :MD5: %s\r\n" % id)
while 1:
    data = irc.recv ( 1024 )
    print(data)

    if data.find ( '376' ) != -1:
        irc.send( 'JOIN ' + channel + '\r\n' )
    if data.find ( 'PING' ) != -1:
        irc.send( 'PONG ' + data.split() [1] + '\r\n')
    if data.find ( '!rss' ) != -1:
        feedurl = feedparser.parse("http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=username")
        newest = feedurl['items'][0].title
        newest = newest.replace("username:","")
        msg = newest.split("http://")
        title = msg[0]
        url = msg[1]
        url = "http://" + url
        #e = feedurl.entries[2]
        #threadurl = e.link
        id = hashlib.md5(url + title).hexdigest()
        irc.send ("PRIVMSG #debug :Tittle:%s\r\n" % newest)
        irc.send ("PRIVMSG #debug :URL: %s\r\n" % url)
        irc.send ("PRIVMSG #debug :MD5: %s\r\n" % id)
    while true:
        readRss()
        time.sleep(300)

if i add a while :true inside the while 1: with a time.sleep(300) the sleep command conflicts with the while 1: loop which i need to do something similar so i could check for new feeds every x minutes. what could i do?

Slightz
  • 67
  • 9

2 Answers2

3

Instead of a new loop, use a separate timer.

import time
last_update = time.time()

while 1:
   # the rest of your while loop as usual
   now = time.time()
   if now - last_update > 300:
       # you've waited 300 seconds
       # check feeds or whatever 
       last_update = now
agf
  • 171,228
  • 44
  • 289
  • 238
  • i tried that, but it did not work i tried with 10 seconds to see how it go, but it seems it never go to read it and execute it. or perhaps i did it wrong, maybe. – Slightz Apr 06 '12 at 15:31
  • @Slightz Try it, and it if doesn't work, post the full code where yo tried it in your question, and comment here, so I can take a look and show you the problem. This method definitely works if you do it right – agf Apr 06 '12 at 15:47
  • Yes it works now, thanks sorry am new posting here i don't know how to put a code properly, thought i tried 10 secs but it actually takes like 36 secs but is fine. – Slightz Apr 06 '12 at 16:06
  • @Slightz if `data = irc.recv ( 1024 )` blocks, then it can't happen while that is going, so you're really setting the minimum, and the maximum is the time plus the timeout of the `recv` call. – agf Apr 06 '12 at 16:11
  • so what should i do? increase the blocks? because since i need the bot to read commands on IRC such as !rss for manually retrieve data. while automatically check on the background. – Slightz Apr 06 '12 at 16:15
  • @Slightz If you need two things happening at the same time, you need two separate threads or processes. If you just want more control over the delays, look at the socket documentation, specifically [socket.nonblocking](http://docs.python.org/library/socket.html#socket.socket.setblocking) and [socket.settimeout](http://docs.python.org/library/socket.html#socket.socket.settimeout). – agf Apr 06 '12 at 16:18
0

I handled that with threading module on my irc bot check the project this may help you https://github.com/mouuff/MouBot (I called the fonction ircloop this will answer server pings and do botting stuff :)

Arnaud Aliès
  • 1,079
  • 13
  • 26