Advertisement
lil_blizzard

shift_report

Sep 13th, 2019
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. import datetime
  2. import pickle
  3. import os.path
  4. from googleapiclient.discovery import build
  5. from apiclient import errors
  6. from google_auth_oauthlib.flow import InstalledAppFlow
  7. from google.auth.transport.requests import Request
  8. from email.mime.text import MIMEText
  9. import base64
  10.  
  11. # If modifying these scopes, delete the file token.pickle.
  12. SCOPES = ['https://www.googleapis.com/auth/gmail.send']
  13. def credentials():
  14.     creds = None
  15.     # The file token.pickle stores the user's access and refresh tokens, and is
  16.     # created automatically when the authorization flow completes for the first
  17.     # time.
  18.     if os.path.exists('token.pickle'):
  19.         with open('token.pickle', 'rb') as token:
  20.             creds = pickle.load(token)
  21.     # If there are no (valid) credentials available, let the user log in.
  22.     if not creds or not creds.valid:
  23.         if creds and creds.expired and creds.refresh_token:
  24.             creds.refresh(Request())
  25.         else:
  26.             flow = InstalledAppFlow.from_client_secrets_file(
  27.                 'credentials.json', SCOPES)
  28.             creds = flow.run_local_server(port=0)
  29.         # Save the credentials for the next run
  30.         with open('token.pickle', 'wb') as token:
  31.             pickle.dump(creds, token)
  32.             print('Creds dumped to file.')
  33.  
  34.     service = build('gmail', 'v1', credentials=creds)
  35.     return service
  36.  
  37. # get the input data
  38. def get_user_input():
  39.     overall_day_mood = input('How was today? ')
  40.  
  41.     accomplished_tasks = []
  42.     amount_of_tasks_required = 3
  43.     for _ in range(amount_of_tasks_required):
  44.         task = input('What did you accomplish today? ')
  45.         accomplished_tasks.append(task)
  46.  
  47.     tasks = overall_day_mood, accomplished_tasks
  48.     return tasks
  49.  
  50. #format the data into an spaced string
  51. def format_for_email(data):
  52.  
  53.     formatted_email_string = "Hey Erin! \n\n" \
  54.                              f"Today was {data[0]}. Here's a list of what I accomplished: \n\n" \
  55.                              f"   - {data[1][0]} \n" \
  56.                              f"   - {data[1][1]} \n" \
  57.                              f"   - {data[1][2]} \n\n" \
  58.                              f"Thanks,\nPatrick"
  59.     return formatted_email_string
  60.  
  61. # sender & destination
  62. my_email = 'me'
  63. erin_email = 'efallon@sas.upenn.edu'
  64.  
  65. # create our gmail message
  66. def create_message(sender, to, subject, message_text):
  67.     message = MIMEText(message_text)
  68.     message['to'] = to
  69.     message['from'] = sender
  70.     message['subject'] = subject
  71.     return {'raw': base64.urlsafe_b64encode(message.as_string().encode()).decode()}
  72.  
  73. # the the message we created
  74. def send_message(service, user_id, message):
  75.     try:
  76.         message = (service.users().messages().send(userId = user_id, body=message).execute())
  77.         print(f'Message sent successfully. ID: {message["id"]}')
  78.         return message
  79.     except errors.HttpError as error:
  80.         print(f'An error occurred: {error}')
  81.  
  82. shift_data = get_user_input()
  83. formatted_email = format_for_email(shift_data)
  84. print('.')
  85. print(formatted_email)
  86. print('.')
  87.  
  88. gmail = credentials()
  89. final_email = create_message(my_email,erin_email,f'Shift Report: {datetime.date.today()}',formatted_email)
  90.  
  91. user_wants_to_send = False
  92. while not user_wants_to_send:
  93.     if input('Do you want to send? ') == 'y':
  94.         send_message(gmail, 'me', final_email)
  95.         user_wants_to_send = True
  96.     else:
  97.         quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement