Annotation of comics/status_scan.py, revision 1.1

1.1     ! nick        1: #!/usr/bin/python
        !             2: # -*- coding: utf-8 -*-
        !             3: 
        !             4: import json
        !             5: import smtplib
        !             6: import socket
        !             7: import urllib
        !             8: 
        !             9: from email.mime.text import MIMEText
        !            10: 
        !            11: 
        !            12: def sendMail(subject, body, address):
        !            13:     _from = "comics@%s" % socket.getfqdn()
        !            14:     msg = MIMEText(body)
        !            15:     msg['Subject'] = subject
        !            16:     msg['To'] = address
        !            17:     msg['From'] = _from
        !            18: 
        !            19:     s = smtplib.SMTP('localhost')
        !            20:     s.sendmail(_from, [address], msg.as_string())
        !            21: 
        !            22: 
        !            23: def generateMessageBody(report):
        !            24:     body = 'The following comic(s) are reporting errors:\n'
        !            25:     for error in report['comics']:
        !            26:         if error['error']:
        !            27:             body += '  %-25s%s\n' % (error['comicName'], error['error'])
        !            28:     return body
        !            29: 
        !            30: 
        !            31: def notifyErrors(report):
        !            32:     to = 'nick@demandred.dyndns.org'
        !            33:     subject = 'Error found in daily comics'
        !            34:     body = generateMessageBody(report)
        !            35:     sendMail(subject, body, to)
        !            36: 
        !            37: 
        !            38: def fetchStatusReport(source):
        !            39:     response = urllib.urlopen(source)
        !            40:     return json.loads(response.read())
        !            41: 
        !            42: 
        !            43: def main():
        !            44:     status_report = 'http://demandred.dyndns.org/daily/comics/status_report.json'
        !            45:     report = fetchStatusReport(status_report)
        !            46:     if report.get('totalErrors') and report['totalErrors'] > 0:
        !            47:         notifyErrors(report)
        !            48:         exit(1)
        !            49: 
        !            50: 
        !            51: if __name__ == '__main__':
        !            52:     main()

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>