forked from bokovhu/secret-santa-generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret-santa.py
98 lines (70 loc) · 2.94 KB
/
secret-santa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import googleapiclient.discovery as gacd
import oauth2client as o2c
import oauth2client.file as o2cf
import oauth2client.tools as o2ct
import oauth2client.client as o2cc
import httplib2 as hl2
import os
import re
import email.mime.text
import base64
import random
OAUTH_SCOPES = 'https://mail.google.com/'
OAUTH_TOKEN_STORE_FILENAME = 'token.json'
"""
Visit https://developers.google.com/gmail/api/quickstart/python,
and click "ENABLE THE GMAIL API". You can download the JSON file
from the dialog.
"""
OAUTH_CREDENTIALS_FILENAME = '_env/credentials.json'
"""
People file example:
John Doe <john.doe@mail.com>
Jack Brown <jack.brown@mail.com>
Jane Smith <jane.smith@mail.com>
"""
PEOPLE_DATABASE_FILENAME = '_env/people'
PERSON_REGEX = '(?P<name>[a-zA-Z0-9 ]+)[<](?P<email>.*)[>]'
def authenticate ():
oauthTokenStore = o2cf.Storage ('token.json')
oauthCredentials = oauthTokenStore.get ()
if not oauthCredentials or oauthCredentials.invalid:
if not os.path.exists (OAUTH_CREDENTIALS_FILENAME) or not os.path.isfile (OAUTH_CREDENTIALS_FILENAME):
raise FileNotFoundError (OAUTH_CREDENTIALS_FILENAME + ' does not exist!')
oauthLoginFlow = o2cc.flow_from_clientsecrets (OAUTH_CREDENTIALS_FILENAME, OAUTH_SCOPES)
oauthCredentials = o2ct.run_flow (oauthLoginFlow, oauthTokenStore)
return oauthCredentials
def makeSecretSantaMail (senderEmailAddress, person, secretSantaOf):
emailText = f"""
Dear {person ["name"]},<br>
You are the secret santa of {secretSantaOf ["name"]}.<br>
<i>- Secret Santa Generator by Botond János Kovács <botondjanoskovacs@gmail.com></i>
"""
mimeMail = email.mime.text.MIMEText (emailText, 'html')
mimeMail ['from'] = senderEmailAddress
mimeMail ['to'] = f'{person ["name"]} <{person ["email"]}>'
mimeMail ['subject'] = 'Secret Santa'
return base64.urlsafe_b64encode ( mimeMail.as_bytes () ).decode ('utf8')
def readPeople ():
people = []
with open (PEOPLE_DATABASE_FILENAME, 'r') as peopleDatabaseFile:
for line in peopleDatabaseFile:
match = re.search (PERSON_REGEX, line)
people.append ( { 'name': match.group ('name'), 'email': match.group ('email') } )
return people
def main ():
people = readPeople ()
credentials = authenticate ()
gmailService = gacd.build ('gmail', 'v1', http = credentials.authorize (hl2.Http ()))
senderEmailAddress = gmailService.users ().getProfile (userId = 'me').execute () ['emailAddress']
for i in range (1000):
random.shuffle (people)
for i in range (len (people)):
thisPerson = people [i]
secretSantaOf = people [(i + 1) % len (people)]
gmailApiRequestData = {
'raw': makeSecretSantaMail (senderEmailAddress, thisPerson, secretSantaOf)
}
messageSendResponse = gmailService.users ().messages ().send (userId = 'me', body = gmailApiRequestData).execute ()
if __name__ == '__main__':
main ()