IRC bot make loop sleep without interrup the main loop
-
29-05-2021 - |
سؤال
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?
المحلول
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
نصائح أخرى
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 :)