monitor.py 2.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
#encoding: utf-8
"""
Monitoring methods.
"""

import feedparser
import sqlite3
import time

import config
from logs import Log

class Wiki():
    def __init__(self, name, url):
        # wiki's name
        self.name = name
        # base url
        self.url = url
        # default last_entry_published
        self.last_entry_updated = time.strptime("2000-01-01", "%Y-%m-%d")
        # See if there is a later last_entry_published for wiki
        connection = sqlite3.connect(config.sqlite_db)
        for row in connection.execute(
            "SELECT last_entry_updated FROM wikis WHERE name=?",
            (self.name,)
        ):
            self.last_entry_updated = time.strptime(
                row[0].encode("utf-8"),
                "%Y-%m-%d %H:%M:%S %Z"
            )
        Log.debug("Dernière mise à jour du wiki: %s" % self.last_entry_updated)

    def set_last_entry_updated(self, last_entry_updated):
        self.last_entry_updated = last_entry_updated
        last_entry_updated = time.strftime(
            "%Y-%m-%d %H:%M:%S %Z",
            self.last_entry_updated
        )
        connection = sqlite3.connect(config.sqlite_db)
        connection.execute(
            "UPDATE wikis SET last_entry_updated=? WHERE name=?",
            (last_entry_updated, self.name)
        )
        connection.commit()

class Monitor():
    def __init__(self):
        # 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):
        """
        This method loops over each wiki to monitor.
        """
        messages = []
        for wiki in self.wikis:
            url = wiki.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 wiki.last_entry_updated < entry.updated_parsed:
                        # Ecriture de la mise à jour sur le canal de travail
73
                        messages.append("""%s a mis à jour la page "%s" sur le wiki (%s)""" % (
74 75 76 77 78 79 80
                            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