How the HELL is oncall ? (the oncall reminder script)

When you have oncall often sometimes is easy to forget hows oncall and when you are not. So for the last time wonder how is oncall and ask some python for some help,

 

The script

#!/usr/bin/env python
#
# Mattias Hemmingsson
# matte@elino.se
#
# Script for reminder friend when to bet
# Uses and csv file and send email to remind when its time to bet.
#
# 
import csv
import smtplib
from datetime import datetime, timedelta, date

#Get users and send email to users
sender = 'noreply@elino.se'
emails =[]
better = ""
week = 2
def save_to_file(name):
 '''
 Saves the oncall to file
 '''
 f = open("oncall.txt", "w")
 f.write(name)
 f.close()
def read_file():
 '''
 read the oncall to file
 '''
 f = open("oncall.txt", "r")
 return int(f.readline()) + 1
 f.close()
def send_oncall():
 '''
 Send an reminder ho is oncall
 '''
#Get cont on oncall staff and next oncall
 numeroncall = str(int(len(open("people.csv").readlines())) +1)
 nextoncall = str(read_file())

 #Loop to begning of file 
 if nextoncall == numeroncall:
 nextoncall = '1'

 #Send email to oncall staff
 emails=[]
#Get how is oncall
 with open('people.csv', 'rb') as f:
 reader = csv.reader(f)
 for row in reader:
 emails.append(row[2])
 if row[0] == nextoncall:
 oncall = row[1]
 #Save oncall to file
 save_to_file(row[0])
 

 message = """From: SYCO <noreply@elino.se>
 To: SYSOP
 Subject: {0} IS NOW ONCALL

 This is an reminder that {0} is now oncall.
 """.format(oncall)
send_email(emails,message)

#Sending the email
def send_email(emails,message):
 sender = "sycoreply@elino.se"
 '''
 Send the email
 '''

 try:
 smtpObj = smtplib.SMTP('localhost')
 smtpObj.sendmail(sender, emails, message) 
 print "Successfully sent email"
 except SMTPException:
 print "Error: unable to send email"

 print emails
 print sender + message
send_oncall()

And then a file with the oncall staff

1,Mattias Hemmingsson,matte@elino.se
2,oncall one,oncall@elino.se
3,oncall two,oncall2@elino.se
4,oncall tre,oncall3@elino.se

 

More update in my github

https://github.com/mattiashem/Scripts/tree/master/reminer