-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from GDG-GANDHINAGAR/sendgrid
Add Sendgrid way
- Loading branch information
Showing
7 changed files
with
201 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
from __future__ import print_function | ||
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 sendgrid import SendGridAPIClient | ||
from sendgrid.helpers.mail import Mail, Attachment, Content | ||
|
||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
|
||
API_KEY = os.getenv('SG_APIKEY') | ||
ORG_EMAIL = os.getenv('ORG_EMAIL') | ||
ORG_NAME = os.getenv('ORG_NAME') | ||
FONT = os.getenv('FONT') | ||
|
||
sg = SendGridAPIClient(API_KEY) | ||
|
||
# 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()) | ||
|
||
message = Mail( | ||
from_email=(ORG_EMAIL, "GDG Gandhinagar"), | ||
subject="[ID Card] GDG Gandhinagar - DevFest 2019", | ||
to_emails=[(email, name)], | ||
html_content=Content("text/html", mail_temp(name, email))) | ||
|
||
attachment = Attachment() | ||
attachment.file_content = base64Value.decode() | ||
attachment.file_type = "image/png" | ||
attachment.file_name = "{}.png".format(name) | ||
attachment.disposition = "attachment" | ||
|
||
message.add_attachment(attachment) | ||
|
||
print("\tSending mail to " + name + "...") | ||
|
||
result = sg.client.mail.send.post(message.get()) | ||
if result.status_code == 200: | ||
print("\t\tMail sent.") | ||
else: | ||
print("\t\tMail not sent.") | ||
|
||
print('All cards sent successfully.') | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters