WatchWebsite module: can alert when a site is up or down

This commit is contained in:
Némunaire 2012-12-07 21:18:21 +01:00
parent eada0c71e7
commit ab557dc8e8
2 changed files with 13 additions and 5 deletions

View File

@ -48,8 +48,7 @@ def cmd_isup(msg):
res.append(Response(msg.sender, "%s n'est pas une URL valide" % url, channel=msg.channel)) res.append(Response(msg.sender, "%s n'est pas une URL valide" % url, channel=msg.channel))
return res return res
else: else:
return Response(msg.sender, "Indiquer une URL a verifier !", channel=msg.channel) return Response(msg.sender, "Indiquer une URL à vérifier !", channel=msg.channel)
def traceURL(url, timeout=5, stack=None): def traceURL(url, timeout=5, stack=None):
"""Follow redirections and return the redirections stack""" """Follow redirections and return the redirections stack"""

View File

@ -40,9 +40,9 @@ def getPageContent(url):
print_debug("Get page %s" % url) print_debug("Get page %s" % url)
try: try:
raw = urlopen(url, timeout=15) raw = urlopen(url, timeout=15)
return raw.read().decode()
except socket.timeout: except socket.timeout:
return None return None
return raw.read().decode()
def start_watching(site): def start_watching(site):
o = urlparse(site["url"], "http") o = urlparse(site["url"], "http")
@ -85,7 +85,7 @@ def del_site(msg):
return Response(msg.sender, "je ne surveillais pas cette URL pour vous.", return Response(msg.sender, "je ne surveillais pas cette URL pour vous.",
channel=msg.channel, nick=msg.nick) channel=msg.channel, nick=msg.nick)
def add_site(msg): def add_site(msg, diffType="diff"):
if len(msg.cmds) <= 1: if len(msg.cmds) <= 1:
return Response(msg.sender, "quel site dois-je surveiller ?", return Response(msg.sender, "quel site dois-je surveiller ?",
msg.channel, msg.nick) msg.channel, msg.nick)
@ -102,7 +102,7 @@ def add_site(msg):
if url not in DATAS.index: if url not in DATAS.index:
watch = ModuleState("watch") watch = ModuleState("watch")
watch["type"] = "diff" watch["type"] = diffType
watch["url"] = url watch["url"] = url
watch["time"] = 123 watch["time"] = 123
DATAS.addChild(watch) DATAS.addChild(watch)
@ -125,6 +125,15 @@ def format_response(site, link='%s', title='%s', categ='%s'):
def alert_change(content, site): def alert_change(content, site):
"""Alert when a change is detected""" """Alert when a change is detected"""
if site["type"] == "updown":
if site["lastcontent"] is None:
site["lastcontent"] = content is not None
if content is None != site.getInt("lastcontent"):
format_response(site, link=site["url"])
site["lastcontent"] = content is not None
return
if content is None: if content is None:
start_watching(site) start_watching(site)
return return