monitor.py 4.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#encoding: utf-8
"""
Monitoring methods.
"""

import feedparser
import sqlite3
import time

import config
from logs import Log

13
class Monitor():
14
    def __init__(self, name, url):
15
        # Monitor's name
16 17 18 19
        self.name = name
        # base url
        self.url = url
        # default last_entry_published
20
        self.last_entry_updated = time.strptime("2000-01-01 00:00:00 CET", "%Y-%m-%d %H:%M:%S %Z")
21 22 23
        # See if there is a later last_entry_published for wiki
        connection = sqlite3.connect(config.sqlite_db)
        for row in connection.execute(
24
            "SELECT last_entry_updated FROM monitors WHERE name=?",
25 26 27 28 29 30
            (self.name,)
        ):
            self.last_entry_updated = time.strptime(
                row[0].encode("utf-8"),
                "%Y-%m-%d %H:%M:%S %Z"
            )
31 32 33 34 35 36 37 38 39
        # 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))
40 41 42 43 44 45 46 47 48

    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(
49
            "UPDATE monitors SET last_entry_updated=? WHERE name=?",
50 51 52 53
            (last_entry_updated, self.name)
        )
        connection.commit()

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    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():
108
    def __init__(self):
109 110 111 112 113 114 115
        # 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"]))
116

117
    def update(self):
118
        """
119
        This method loops over each monitor to see if something has changed.
120 121
        """
        messages = []
122 123 124 125
        for monitor in self.monitors:
            message = monitor.update()
            if message is not None:
                messages.append(message)
126
        return messages