-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaramispresso.py
executable file
·149 lines (127 loc) · 6.06 KB
/
naramispresso.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import csv
import re
import json
import numpy as np
import smtplib
import argparse
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
parser = argparse.ArgumentParser(
description='summarize ARAMIS LAB\'s Nespresso orders and send bills accordingly',
prog='naramispresso',
epilog='see https://github.com/jguillon/naramispresso for updates, bugs reports and answers')
parser.add_argument('typeform_csv', help='csv file exported from Typeform')
parser.add_argument('--send', help='send bills', action='store_true')
parser.add_argument('--date', help='reception date') # '03/08/2018 entre 9h00 et 18h00'
args, unknown_args = parser.parse_known_args()
with open('parameters.json') as f:
params = json.load(f)
if args.send:
if not args.date:
print('Please specify a reception date using the \'--date\' argument.')
exit(1)
with open('bill.mail.html', 'r') as html_file:
mail_html = html_file.read()
mail_html = mail_html.replace('{reception_date}', args.date)
mail_html = mail_html.replace('{paypal_id}', params['paypal_id'])
mail_html = mail_html.replace('{slack_url}', params['slack_url'])
mail_html = mail_html.replace('{contact_mail}', params['contact_mail'])
mail_html = mail_html.replace('{office_address}', params['office_address'])
html_file.close()
with open(args.typeform_csv, 'rU') as csv_file:
nespresso_report_reader = csv.reader(csv_file, delimiter=',', quotechar='"')
# Extract prices on first row
first_row = nespresso_report_reader.__next__()
brands = first_row[1:-8]
prices = [0 for i in range(len(brands))]
overall_quantities = [0 for i in range(len(brands))]
overall_total = 0
for i, brand in enumerate(brands):
m = re.search('[0-9]+,[0-9]+', brand)
prices[i] = float(m.group(0).replace(',','.'))
# Itarate over customers lines
for row in nespresso_report_reader:
## Information extraction
first_name = row[-8]
last_name = row[-7]
mail = row[-6]
quantities = [int(i) if i else 0 for i in row[1:-8]]
overall_quantities = [sum(x) for x in zip(overall_quantities, quantities)]
total = sum([quantities[i] * prices[i] for i in range(len(brands))])
overall_total = overall_total + total
print('\n' + first_name + ' ' + last_name + ' <' + mail + '> :')
## HTML table creation
command_details = ''
for i, quantity in enumerate(quantities):
if quantity > 0:
print(str(quantity) + ' x ' + brands[i])
command_details = command_details \
+ '\t\t\t\t<tr bgcolor="#f4ede8" style="color: #313131;">' \
+ '<td>' + brands[i] + '</td>' \
+ '<td>' + str(quantity) + ' x ' + str(prices[i]) + '€' + '</td>' \
+ '<td>' + str(quantity * prices[i]) + '€' + '</td>' \
+ '</tr>\n'
print("=> " + str(sum(quantities)) + ' BOXES (' + str(total) + "€)")
if args.send:
## Mail content writing
perso_mail_html = mail_html.replace('{first_name}',
first_name)
perso_mail_html = perso_mail_html.replace('{last_name}',
last_name)
perso_mail_html = perso_mail_html.replace('{first_name}',
first_name)
perso_mail_html = perso_mail_html.replace('{command_details}',
command_details)
perso_mail_html = perso_mail_html.replace('{total}',
str(total) )
perso_mail_text = 'Oops! Use a modern mail app!'
## Mail object building
msg = MIMEMultipart('related')
msg['Subject'] = 'Naramispresso : Confirmation de Commande'
msg['From'] = 'Naramispresso'
msg['To'] = mail
msg_alt = MIMEMultipart('alternative')
msg.attach(msg_alt)
part1 = MIMEText(perso_mail_text, 'plain', 'utf-8')
msg_alt.attach(part1)
part2 = MIMEText(perso_mail_html, 'html', 'utf-8')
msg_alt.attach(part2)
## Deal with images
fp = open('img/naramispresso-logotext-bw.png', 'rb')
image1 = MIMEImage(fp.read())
fp.close()
image1.add_header('Content-ID', '<naramispresso-logotext-bw>')
msg.attach(image1)
fp = open('img/naramispresso-mail-header.jpeg', 'rb')
image2 = MIMEImage(fp.read())
fp.close()
image2.add_header('Content-ID', '<naramispresso-mail-header>')
msg.attach(image2)
try:
print('Sending bill...')
## Mail sending
smtp_server = smtplib.SMTP_SSL(params['mail_server']['address'],
params['mail_server']['port'])
smtp_server.ehlo()
# smtp_server.starttls()
smtp_server.login(params['mail_server']['login'],
params['mail_server']['password'])
smtp_server.sendmail(msg['From'], msg['To'], msg.as_string())
smtp_server.sendmail(msg['From'], params['contact_mail'],
msg.as_string())
smtp_server.close()
print(' ...OK')
except Exception as e:
print(e)
print('Somehting went wrong when trying to send the mails.')
csv_file.close()
## Print recap'
print("\n============= TOTAL =============\n")
for i, quantity in enumerate(overall_quantities):
if quantity > 0:
print(str(quantity) + ' x ' + brands[i])
print("=> " + str(sum(overall_quantities)) + ' BOXES (' +
str(overall_total) + "€)")