Guest User

assignChores.py

a guest
Dec 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. #! python3
  2. #
  3. # assignChores.py - email a random list of chores to a list of emails from an excel spreadsheet
  4. #
  5. # Write a program that takes a list of people’s email addresses and a list of chores
  6. # that need to be done and randomly assigns chores to people. Email each person their
  7. # assigned chores.
  8. #
  9. # If you’re feeling ambitious, keep a record of each person’s previously assigned
  10. # chores so that you can make sure the program avoids assigning anyone the same chore
  11. # they did last time.
  12. #
  13. # For another possible feature, schedule the program to run once a week automatically.
  14. #
  15. # Note:
  16. # - smtp_info file has each item on separate line
  17. # - use input() for password to prevent storing in unencrypted file
  18. # - it may be easier to:
  19. #   - setup a crontab to run weekly
  20. #   - store saved_time and prev_chores in a shelve database
  21. #
  22. # This program is part of a conversation about a python book on reddit at:
  23. # https://www.reddit.com/r/inventwithpython/comments/8ykq1i/automate_the_boring_stuff_with_python_corrections/
  24. #
  25. # -/u/JoseALerma
  26. #
  27.  
  28. import openpyxl, random, smtplib, datetime
  29.  
  30. # Open the spreadsheet and get the lists of data.
  31. wb = openpyxl.load_workbook('choresList.xlsx')
  32. sheet = wb['Sheet1']
  33.  
  34. names, emails, chores, prev_chores = [], [], [], []
  35.  
  36. for row in range(2, sheet.max_row + 1):  # skip title row
  37.     name = sheet['A' + str(row)].value
  38.     email = sheet['B' + str(row)].value
  39.     chore = sheet['C' + str(row)].value
  40.     prev_chore = sheet['D' + str(row)].value
  41.  
  42.     names.append(name)
  43.     emails.append(email)
  44.     chores.append(chore)
  45.     prev_chores.append(prev_chore)
  46.  
  47. # Run weekly
  48. saved_time = sheet['E2'].value
  49. interval = datetime.timedelta(days=7)
  50. now = datetime.datetime.now()
  51. if saved_time is None:
  52.     saved_time = now - interval  # First run, so it's been a week
  53. timedelta = saved_time + interval
  54. if timedelta > now:
  55.     time_left = round((timedelta - now).total_seconds()/60, 2)
  56.     print(f"RuntimeError: Need to wait {time_left} minutes before running again.")
  57.     raise RuntimeError
  58. else:
  59.     sheet['E2'].value = now  # save to spreadsheet
  60.  
  61. # Log in to email account.
  62. with open('../smtp_info') as config:
  63.     myEmail, password, server, port = config.read().splitlines()
  64.  
  65. smtpObj = smtplib.SMTP_SSL(server, port)  # Using port 465
  66. smtpObj.ehlo()
  67. smtpObj.login(myEmail, password)
  68.  
  69. # Randomly assign chores
  70. for i in range(0, len(names)):
  71.     random_chore = random.choice(chores)
  72.     # Check previous chore before assignment
  73.     while random_chore == prev_chores[i] and len(chores) > 1:
  74.         random_chore = random.choice(chores)
  75.     # Keep track of chores assigned
  76.     sheet['D' + str(i + 2)].value = random_chore
  77.     chores.remove(random_chore)  # remove assigned chore from pool
  78.  
  79.     # Send email.
  80.     body = "Subject: Chore for the Week: %s.\nDear %s,\n\nThis week, you're in charge of:\n%s. " \
  81.            "\n\nThank you in advance for your efforts!" % (random_chore, names[i], random_chore)
  82.     print(f'Sending email to {emails[i]}...')
  83.     sendmailStatus = smtpObj.sendmail(myEmail, emails[i], body)
  84.     if sendmailStatus != {}:
  85.         print(f'There was a problem sending email to {emails[i]}: {sendmailStatus}')
  86. smtpObj.quit()
  87. wb.save('choresList.xlsx')
Add Comment
Please, Sign In to add comment