Annotation of comics/status_scan.py, revision 1.2

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

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