-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.py
212 lines (184 loc) · 7.22 KB
/
utility.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
'''
Created on 31 mag 2017
@author: Geko
'''
from math import radians, asin, sin, cos, sqrt
from future.utils import bytes_to_native_str
from motionless import DecoratedMap, LatLonMarker
import six
from telegram.inlinekeyboardbutton import InlineKeyboardButton
import numpy as np
import pandas as pd
import matplotlib as mpl
import smtplib
mpl.use('Agg')
import matplotlib.pyplot as plt
from bot.models import User
from smartBot import settings
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def getEmoticon(b):
return bytes_to_native_str(b)
''' web source: '''
''' https://github.com/python-telegram-bot/python-telegram-bot/blob/master/telegram/emoji.py '''
ZERO_KEYCAP = getEmoticon(b'\x30\xE2\x83\xA3')
ONE_KEYCAP = getEmoticon(b'\x31\xE2\x83\xA3')
TWO_KEYCAP = getEmoticon(b'\x32\xE2\x83\xA3')
THREE_KEYCAP = getEmoticon(b'\x33\xE2\x83\xA3')
FOUR_KEYCAP = getEmoticon(b'\x34\xE2\x83\xA3')
FIVE_KEYCAP = getEmoticon(b'\x35\xE2\x83\xA3')
SIX_KEYCAP = getEmoticon(b'\x36\xE2\x83\xA3')
SEVEN_KEYCAP = getEmoticon(b'\x37\xE2\x83\xA3')
EIGHT_KEYCAP = getEmoticon(b'\x38\xE2\x83\xA3')
NINE_KEYCAP = getEmoticon(b'\x39\xE2\x83\xA3')
def haversine(lon1, lat1, lon2, lat2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
def getDecoratedMap(bot, update, closestParkings, data, distances):
road_styles = [{
'feature': 'road.highway',
'element': 'geomoetry',
'rules': {
'visibility': 'simplified',
'color': '#c280e9'
}
}, {
'feature': 'transit.line',
'rules': {
'visibility': 'simplified',
'color': '#bababa'
}
}]
dmap = DecoratedMap(style=road_styles)
utente = User.objects.get(chat_id=update.message.chat_id)
lat = utente.lat
lon = utente.lon
dmap.add_marker(LatLonMarker(lat, lon, label='S', color='blue'))
i = 0
keyboard = [[],[],[]]
table = []
for p in closestParkings:
if "parking" in utente.lastCommand:
dmap.add_marker(LatLonMarker(lat=data[p]['geometry']['coordinates'][1], lon=data[p]['geometry']['coordinates'][0],label=str(i+1)))
textButton = createDetailsButtonTextParking(data[p], i)
row = createRowParking(data[p], distances[i], i)
elif "chargePoint" in utente.lastCommand:
dmap.add_marker(LatLonMarker(lat=data[p]['geometry']['coordinates'][1], lon=data[p]['geometry']['coordinates'][0],label=str(i+1)))
textButton = createDetailsButtonTextChargePoint(data[p], i)
row = createRowChargePoint(data[p], distances[i], i)
table.append(row)
keyboard[i].append(InlineKeyboardButton(text=textButton, callback_data= str(p), resize_keyboard=True))
i += 1
url = dmap.generate_url()
npArray = np.array(table)
df = pd.DataFrame(npArray)
df.columns = ['N.', 'Parking Name', 'Free Slots', 'Distance']
ax = render_mpl_table(df, header_columns=0, col_width=1.5)
fileName = str(update.message.chat_id) + '.png'
plt.savefig(fileName, bbox_inches='tight')
baseDir = settings.BASE_DIR
picture = open(baseDir + '/' + fileName, 'rb')
#img=urllib.request.urlopen(baseDir + '\foo.png').read()
bot.sendPhoto(chat_id=update.message.chat_id, photo = picture)
print(ax)
return url, keyboard
def createDetailsButtonTextParking(p, i):
emoticons = [ONE_KEYCAP, TWO_KEYCAP, THREE_KEYCAP]
text = str(p['properties']['title'])
text = text[7:]
text = emoticons[i] + ' - ' + text
return text
def createDetailsButtonTextChargePoint(p, i):
emoticons = [ONE_KEYCAP, TWO_KEYCAP, THREE_KEYCAP]
text = str(p['properties']['title'])
text = emoticons[i] + ' - ' + text
return text
def createRowParking(p, distance, index):
row = []
parkingName = p['properties']['title']
row.append(index + 1)
row.append(parkingName[7:])
try:
row.append(str(p['properties']['layers']['parking.garage']['data']['FreeSpaceShort']))
except KeyError:
row.append("No Info")
row.append(str(float("{0:.2f}".format(distance))) + ' KM')
return row
def createRowChargePoint(p, distance, index):
row = []
chargePointName = p['properties']['title']
row.append(index + 1)
row.append(chargePointName)
try:
row.append(str(p['properties']['layers']['parking.garage']['data']['FreeSpaceShort']))
except KeyError:
row.append("No Info")
row.append(str(float("{0:.2f}".format(distance))) + ' KM')
return row
def render_mpl_table(data, col_width=1.0, row_height=0.625, font_size=10,
header_color='#40466e', row_colors=['#f1f1f2', 'w'], edge_color='#414144',
bbox=[0, 0, 1, 1], header_columns=0,
ax=None, **kwargs):
if ax is None:
size = (np.array(data.shape[::-1]) + np.array([0, 1])) * np.array([col_width, row_height])
fig, ax = plt.subplots(figsize=size)
ax.axis('off')
mpl_table = ax.table(cellText=data.values, bbox=bbox, colLabels=data.columns, cellLoc='center', **kwargs)
mpl_table.auto_set_font_size(False)
mpl_table.set_fontsize(font_size)
for k, cell in six.iteritems(mpl_table._cells):
text = cell.get_text().get_text()
cell.set_edgecolor(edge_color)
if k[0] == 0 or k[1] < header_columns:
print('Test width: ' + text)
cell.set_text_props(weight='bold', color='w')
cell.set_facecolor(header_color)
else:
cell.set_facecolor(row_colors[k[0]%len(row_colors) ])
if k[1] == 0:
cell.set_width(0.15)
elif k[1] == 1:
cell.set_width(0.6)
elif k[1] == 2:
cell.set_width(0.3)
else:
cell.set_width(0.3)
return ax
def sendMail(name , email, message):
staff_1 = "lamorte.gerardo@gmail.com"
staff_2 = "giovinazzorocco@gmail.com"
me = "smartbotstaff@gmail.com"
# Create message container - the correct MIME type is multipart/alternative.
msg = MIMEMultipart('alternative')
msg['Subject'] = "SMARTBOT NOTIFICATION"
msg['From'] = email
msg['To'] = me
# Create the body of the message (a plain-text and an HTML version).
text = message
# Record the MIME types of both parts - text/plain and text/html.
part1 = MIMEText(text, 'plain')
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
msg.attach(part1)
# Send the message via local SMTP server.
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login('smartbotstaff@gmail.com','lacunacoil')
# sendmail function takes 3 arguments: sender's address, recipient's address
# and message to send - here it is sent as one string.
s.sendmail(me, staff_1, msg.as_string())
s.sendmail(me, staff_2, msg.as_string())
print("mail sent")
s.quit()