Commit 7ead8819 authored by Mindiell's avatar Mindiell

Wantzel can now monitor mediawikis and gitlab groups

parent cebf2f42
...@@ -10,25 +10,33 @@ import time ...@@ -10,25 +10,33 @@ import time
import config import config
from logs import Log from logs import Log
class Wiki(): class Monitor():
def __init__(self, name, url): def __init__(self, name, url):
# wiki's name # Monitor's name
self.name = name self.name = name
# base url # base url
self.url = url self.url = url
# default last_entry_published # default last_entry_published
self.last_entry_updated = time.strptime("2000-01-01", "%Y-%m-%d") self.last_entry_updated = time.strptime("2000-01-01 00:00:00 CET", "%Y-%m-%d %H:%M:%S %Z")
# See if there is a later last_entry_published for wiki # See if there is a later last_entry_published for wiki
connection = sqlite3.connect(config.sqlite_db) connection = sqlite3.connect(config.sqlite_db)
for row in connection.execute( for row in connection.execute(
"SELECT last_entry_updated FROM wikis WHERE name=?", "SELECT last_entry_updated FROM monitors WHERE name=?",
(self.name,) (self.name,)
): ):
self.last_entry_updated = time.strptime( self.last_entry_updated = time.strptime(
row[0].encode("utf-8"), row[0].encode("utf-8"),
"%Y-%m-%d %H:%M:%S %Z" "%Y-%m-%d %H:%M:%S %Z"
) )
Log.debug("Dernière mise à jour du wiki: %s" % self.last_entry_updated) # In case there is no entry for this monitor in database
if self.last_entry_updated==time.strptime("2000-01-01 00:00:00 CET", "%Y-%m-%d %H:%M:%S %Z"):
last_entry_updated = time.strftime(
"%Y-%m-%d %H:%M:%S %Z",
self.last_entry_updated
)
connection.execute("INSERT INTO monitors VALUES (?,?)", (last_entry_updated, self.name))
connection.commit()
Log.debug("Dernière mise à jour de %s: %s" % (self.name, self.last_entry_updated))
def set_last_entry_updated(self, last_entry_updated): def set_last_entry_updated(self, last_entry_updated):
self.last_entry_updated = last_entry_updated self.last_entry_updated = last_entry_updated
...@@ -38,44 +46,81 @@ class Wiki(): ...@@ -38,44 +46,81 @@ class Wiki():
) )
connection = sqlite3.connect(config.sqlite_db) connection = sqlite3.connect(config.sqlite_db)
connection.execute( connection.execute(
"UPDATE wikis SET last_entry_updated=? WHERE name=?", "UPDATE monitors SET last_entry_updated=? WHERE name=?",
(last_entry_updated, self.name) (last_entry_updated, self.name)
) )
connection.commit() connection.commit()
class Monitor(): def update(self):
pass
class Mediawiki(Monitor):
def update(self):
url = self.url + "api.php?days=1&limit=50&translations=filter&action=feedrecentchanges&feedformat=atom"
now = time.localtime()
today = time.strptime("%s-%s-%s %s" % (
now.tm_year,
now.tm_mon,
now.tm_mday,
time.tzname[0]
), "%Y-%m-%d %Z")
entries = feedparser.parse(url)['entries']
for entry in entries:
# if date of update is greater than today midnight
if today < entry.updated_parsed:
if self.last_entry_updated < entry.updated_parsed:
# Save last_entry_published
self.set_last_entry_updated(entry.updated_parsed)
# Sending monitoring on working chan
return """[%s] %s a mis à jour la page %s => %s""" % (
self.name,
entry.author.encode("utf-8"),
entry.title.encode("utf-8"),
entry.link.encode("utf-8"),
)
class GitlabGroup(Monitor):
def update(self):
url = self.url[:-1] + ".atom"
now = time.localtime()
today = time.strptime("%s-%s-%s %s" % (
now.tm_year,
now.tm_mon,
now.tm_mday,
time.tzname[0]
), "%Y-%m-%d %Z")
entries = feedparser.parse(url)['entries']
for entry in entries:
# if date of update is greater than today midnight
if today < entry.updated_parsed:
if self.last_entry_updated < entry.updated_parsed:
# Save last_entry_published
self.set_last_entry_updated(entry.updated_parsed)
# Sending monitoring on working chan
return """[%s] %s => %s""" % (
self.name,
entry.title.encode("utf-8"),
entry.link.encode("utf-8"),
)
class Monitoring():
def __init__(self): def __init__(self):
# List of wikis to monitor # List of objects to monitor
self.wikis = [] self.monitors = []
for wiki in config.wikis["mediawiki"]: for monitor in config.monitors:
self.wikis.append(Wiki(wiki["name"], wiki["url"])) if monitor["type"]=="mediawiki":
self.monitors.append(Mediawiki(monitor["name"], monitor["url"]))
elif monitor["type"]=="gitlab_group":
self.monitors.append(GitlabGroup(monitor["name"], monitor["url"]))
def wiki_updates(self): def update(self):
""" """
This method loops over each wiki to monitor. This method loops over each monitor to see if something has changed.
""" """
messages = [] messages = []
for wiki in self.wikis: for monitor in self.monitors:
url = wiki.url + "api.php?days=1&limit=50&translations=filter&action=feedrecentchanges&feedformat=atom" message = monitor.update()
now = time.localtime() if message is not None:
today = time.strptime("%s-%s-%s %s" % ( messages.append(message)
now.tm_year,
now.tm_mon,
now.tm_mday,
time.tzname[0]
), "%Y-%m-%d %Z")
entries = feedparser.parse(url)['entries']
for entry in entries:
# if date of update is greater than today midnight
if today < entry.updated_parsed:
if wiki.last_entry_updated < entry.updated_parsed:
# Sending monitoring on working chan
messages.append("""[%s] %s a mis à jour la page %s => %s""" % (
wiki.name,
entry.author.encode("utf-8"),
entry.title.encode("utf-8"),
entry.link.encode("utf-8"),
))
# Save last_entry_published
wiki.set_last_entry_updated(entry.updated_parsed)
return messages return messages
...@@ -17,7 +17,7 @@ from logs import Log ...@@ -17,7 +17,7 @@ from logs import Log
# bot modules # bot modules
from admin import Admin from admin import Admin
from fun import fun from fun import fun
from monitor import Monitor from monitor import Monitoring
from rp import Rp from rp import Rp
class Wantzel(object): class Wantzel(object):
...@@ -29,7 +29,7 @@ class Wantzel(object): ...@@ -29,7 +29,7 @@ class Wantzel(object):
Initialization of all utility objects and bot over IRC. Initialization of all utility objects and bot over IRC.
""" """
self.admin = Admin() self.admin = Admin()
self.monitor = Monitor() self.monitor = Monitoring()
self.rp = Rp() self.rp = Rp()
# Connection to IRC # Connection to IRC
self.irc = IrcClientFactory(config) self.irc = IrcClientFactory(config)
...@@ -57,7 +57,7 @@ class Wantzel(object): ...@@ -57,7 +57,7 @@ class Wantzel(object):
if topic != self.topic: if topic != self.topic:
self.irc.client.topic(config.RP_CHANNEL, topic) self.irc.client.topic(config.RP_CHANNEL, topic)
# Tell on channel if a wiki was modified since last time # Tell on channel if a wiki was modified since last time
self.send_message("#lqdn-travail", self.monitor.wiki_updates()) self.send_message(config.MONITOR_CHANNEL, self.monitor.update())
# Cleaning points of mastering rp # Cleaning points of mastering rp
self.rp.clean_master_rp() self.rp.clean_master_rp()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment