-
Notifications
You must be signed in to change notification settings - Fork 6
/
idgen_mailjet.py
186 lines (150 loc) · 5.86 KB
/
idgen_mailjet.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
from __future__ import print_function
from mailjet_rest import Client
import json
import os
import base64
from io import BytesIO
import requests
import math
from PIL import Image, ImageDraw, ImageFont
import qrcode.image.svg
import qrcode
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from mail_temp import mail_temp
from dotenv import load_dotenv
load_dotenv()
API_KEY = os.getenv('MJ_APIKEY_PUBLIC')
API_SECRET = os.getenv('MJ_APIKEY_PRIVATE')
ORG_EMAIL = os.getenv('ORG_EMAIL')
ORG_NAME = os.getenv('ORG_NAME')
FONT = os.getenv('FONT')
mailjet = Client(auth=(API_KEY, API_SECRET), version='v3.1')
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# ID of spreadsheet : https://docs.google.com/spreadsheets/d/<THIS-PART-IS-ID>/edit#gid=0
SAMPLE_SPREADSHEET_ID = os.getenv('SHEET_ID_TEST')
SAMPLE_RANGE_NAME = 'A2:V2'
def main():
print("Getting data from the sheet...")
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
# Call the Sheets API
sheet = service.spreadsheets()
result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
range=SAMPLE_RANGE_NAME).execute()
values = result.get('values', [])
if not values:
print('No data found.')
else:
# We can access the sheet values here
print("Data received.")
for row in values:
reg_id = row[0]
name = row[1] + ' ' + row[2]
email = row[3]
designation = row[18]
inst = row[19]
org = row[21]
print('Generating card for %s...' % (name))
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_M,
box_size=10,
border=2,
)
qr.add_data(reg_id)
qr.make(fit=True)
img = qr.make_image()
# Resize the QR code to 150px X 150px
img.thumbnail((221, 221), Image.ANTIALIAS)
img.save(os.path.join('qrcodes', email + '.png'))
template = Image.open('template.png')
# Paste QR code
template.paste(img, (205, 360))
# Write the name
draw = ImageDraw.Draw(template)
font = ImageFont.truetype(FONT, 55)
x, y = font.getsize(name)
draw.text(((321 - x / 2), (710 - y / 2)),
name, font=font, fill='black')
# Write the designation
if designation != 'NA':
draw = ImageDraw.Draw(template)
font = ImageFont.truetype(FONT, 26)
x, y = font.getsize(designation)
draw.text(((321 - x / 2), (770 - y / 2)),
designation, font=font, fill='black')
if org != 'NA':
draw = ImageDraw.Draw(template)
font = ImageFont.truetype(FONT, 30)
x, y = font.getsize(org)
draw.text(((321 - x / 2), (810 - y / 2)),
org, font=font, fill='black')
elif inst != 'NA':
draw = ImageDraw.Draw(template)
font = ImageFont.truetype(FONT, 30)
x, y = font.getsize(inst)
draw.text(((321 - x / 2), (810 - y / 2)),
inst, font=font, fill='black')
# Add abstract element
element = Image.open('element.png')
element.thumbnail((59, 59), Image.ANTIALIAS)
template.paste(element, (407, 557), element)
# Save the card
template.save(os.path.join('cards', email + '.png'))
buffer = BytesIO()
template.save(buffer, format="PNG")
base64Value = base64.b64encode(buffer.getvalue())
# SEND THE CARD AS AN EMAIL
data = {
'Messages': [
{
"From": {
"Email": ORG_EMAIL,
"Name": ORG_NAME,
},
"To": [
{
"Email": email,
"Name": name
}
],
"Subject": "[ID Card] GDG Gandhinagar - DevFest 2019",
"HTMLPart": mail_temp(name, email),
"Attachments": [
{
"Filename": name + '.png',
"ContentType": "image/png",
"Base64Content": base64Value.decode("utf-8")
}
]
}
]
}
print("\tSending mail to " + name + "...")
result = mailjet.send.create(data=data)
if result.status_code == 200:
print("\t\tMail sent.")
else:
print("\t\tMail not sent.")
print('All cards sent successfully.')
if __name__ == '__main__':
main()