Commit 7ead8819 authored by Mindiell's avatar Mindiell
Browse files

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,25 +46,18 @@ class Wiki(): ...@@ -38,25 +46,18 @@ 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):
def __init__(self): pass
# List of wikis to monitor
self.wikis = []
for wiki in config.wikis["mediawiki"]:
self.wikis.append(Wiki(wiki["name"], wiki["url"]))
def wiki_updates(self):
""" class Mediawiki(Monitor):
This method loops over each wiki to monitor. def update(self):
""" url = self.url + "api.php?days=1&limit=50&translations=filter&action=feedrecentchanges&feedformat=atom"
messages = []
for wiki in self.wikis:
url = wiki.url + "api.php?days=1&limit=50&translations=filter&action=feedrecentchanges&feedformat=atom"
now = time.localtime() now = time.localtime()
today = time.strptime("%s-%s-%s %s" % ( today = time.strptime("%s-%s-%s %s" % (
now.tm_year, now.tm_year,
...@@ -68,14 +69,58 @@ class Monitor(): ...@@ -68,14 +69,58 @@ class Monitor():
for entry in entries: for entry in entries:
# if date of update is greater than today midnight # if date of update is greater than today midnight
if today < entry.updated_parsed: if today < entry.updated_parsed:
if wiki.last_entry_updated < 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 # Sending monitoring on working chan
messages.append("""[%s] %s a mis à jour la page %s => %s""" % ( return """[%s] %s a mis à jour la page %s => %s""" % (
wiki.name, self.name,
entry.author.encode("utf-8"), entry.author.encode("utf-8"),
entry.title.encode("utf-8"), entry.title.encode("utf-8"),
entry.link.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 # Save last_entry_published
wiki.set_last_entry_updated(entry.updated_parsed) 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):
# List of objects to monitor
self.monitors = []
for monitor in config.monitors:
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 update(self):
"""
This method loops over each monitor to see if something has changed.
"""
messages = []
for monitor in self.monitors:
message = monitor.update()
if message is not None:
messages.append(message)
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()
......
Supports Markdown
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