-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmycowboy.15m.py
executable file
·355 lines (279 loc) · 28.4 KB
/
mycowboy.15m.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#!/usr/bin/env PYTHONIOENCODING=UTF-8 /opt/local/bin/python3
# -*- coding: utf-8 -*-
#
# <xbar.title>MyCowboy</xbar.title>
# <xbar.version>v1.0</xbar.version>
# <xbar.author>[email protected]</xbar.author>
# <xbar.author.github>pvdabeel</xbar.author.github>
# <xbar.desc>Control your Cowboy Bike from the MacOS menubar</xbar.desc>
# <xbar.dependencies>python</xbar.dependencies>
#
# Licence: GPL v3
# Installation instructions:
# --------------------------
# Execute in terminal.app before running :
# sudo easy_install keyring
# sudo easy_install pyobjc-framework-CoreLocation
#
# Ensure you have xbar installed https://github.com/matryer/xbar/releases/latest
# Copy this file to your xbar plugins folder and chmod +x the file from your terminal in that folder
# Run xbar
_DEBUG_ = False
# Disabled if you don't want your bike location to be tracked to a DB
_LOCATION_TRACKING_ = True
try: # Python 3 dependencies
from urllib.parse import urlencode, urljoin
from urllib.request import Request, urlopen, build_opener
from urllib.request import ProxyHandler, HTTPBasicAuthHandler, HTTPHandler, HTTPError, URLError
except: # Python 2 dependencies
from urllib import urlencode, urljoin
from urllib2 import Request, urlopen, build_opener
from urllib2 import ProxyHandler, HTTPBasicAuthHandler, HTTPHandler, HTTPError, URLError
import ast
import json
import sys
import datetime
import calendar
import base64
import math
import keyring # Cowboy access token is stored in OS X keychain
import getpass # Getting password without showing chars in terminal.app
import time
import os
import subprocess
import requests
import binascii
import CoreLocation as cl
from datetime import date
from tinydb import TinyDB # Keep track of location and cowboy states
from os.path import expanduser
from googlemaps import Client as googleclient # Reverse lookup of addresses based on coordinates
import library.cowboy as cowboy
# Location where to store state files
home = expanduser("~")
state_dir = home+'/.state/mycowboy'
if not os.path.exists(state_dir):
os.makedirs(state_dir)
# The full path to this file
cmd_path = os.path.realpath(__file__)
# Location tracking database
locationdb = TinyDB(state_dir+'/mycowboy-locations.json')
# Nice ANSI colors
CEND = '\33[0m'
CRED = '\33[31m'
CGREEN = '\33[32m'
CYELLOW = '\33[33m'
CBLUE = '\33[36m'
# Support for OS X Dark Mode
DARK_MODE=True if os.getenv('XBARDarkMode','false') == 'true' else False
# Convertor for distance
def convert_distance(distance_unit,distance):
if distance_unit == 'km':
return math.ceil(distance * 160.9344)/100
else:
return distance
# Pretty print bike lock state
def lock_state(locked):
if bool(locked):
return CGREEN + 'Locked' + CEND
else:
return CRED + 'Unlocked' + CEND
# Function to retrieve goole map & sat images for a given location
def retrieve_google_maps(latitude,longitude):
todayDate = datetime.date.today()
try:
with open(state_dir+'/mycowboy-location-map-'+todayDate.strftime("%Y%m")+'-'+latitude+'-'+longitude+'.png') as location_map:
my_img1 = base64.b64encode(location_map.read()).decode('utf-8')
location_map.close()
with open(state_dir+'/mycowboy-location-sat-'+todayDate.strftime("%Y%m")+'-'+latitude+'-'+longitude+'.png') as location_sat:
my_img2 = base64.b64encode(location_sat.read()).decode('utf-8')
location_sat.close()
except:
with open(state_dir+'/mycowboy-location-map-'+todayDate.strftime("%Y%m")+'-'+latitude+'-'+longitude+'.png','w') as location_map, open(state_dir+'/mycowboy-location-sat-'+todayDate.strftime("%Y%m")+'-'+latitude+'-'+longitude+'.png','w') as location_sat:
my_google_key = '&key=AIzaSyBrgHowqRH-ewRCNrhAgmK7EtFsuZCdXwk'
my_google_dark_style = ''
if bool(DARK_MODE):
my_google_dark_style = '&style=feature:all|element:labels|visibility:on&style=feature:all|element:labels.text.fill|saturation:36|color:0x000000|lightness:40&style=feature:all|element:labels.text.stroke|visibility:on|color:0x000000|lightness:16&style=feature:all|element:labels.icon|visibility:off&style=feature:administrative|element:geometry.fill|color:0x000000|lightness:20&style=feature:administrative|element:geometry.stroke|color:0x000000|lightness:17|weight:1.2&style=feature:administrative.country|element:labels.text.fill|color:0x838383&style=feature:administrative.locality|element:labels.text.fill|color:0xc4c4c4&style=feature:administrative.neighborhood|element:labels.text.fill|color:0xaaaaaa&style=feature:landscape|element:geometry|color:0x000000|lightness:20&style=feature:poi|element:geometry|color:0x000000|lightness:21|visibility:on&style=feature:poi.business|element:geometry|visibility:on&style=feature:road.highway|element:geometry.fill|color:0x6e6e6e|lightness:0&style=feature:road.highway|element:geometry.stroke|visibility:off&style=feature:road.highway|element:labels.text.fill|color:0xffffff&style=feature:road.arterial|element:geometry|color:0x000000|lightness:18&style=feature:road.arterial|element:geometry.fill|color:0x575757&style=feature:road.arterial|element:labels.text.fill|color:0xffffff&style=feature:road.arterial|element:labels.text.stroke|color:0x2c2c2c&style=feature:road.local|element:geometry|color:0x000000|lightness:16&style=feature:road.local|element:labels.text.fill|color:0x999999&style=feature:transit|element:geometry|color:0x000000|lightness:19&style=feature:water|element:geometry|color:0x000000|lightness:17'
my_google_size = '&size=360x315'
my_google_zoom = '&zoom=17'
my_url1 ='https://maps.googleapis.com/maps/api/staticmap?center='+latitude+','+longitude+my_google_key+my_google_dark_style+my_google_zoom+my_google_size+'&markers=color:red%7C'+latitude+','+longitude
my_url2 ='https://maps.googleapis.com/maps/api/staticmap?center='+latitude+','+longitude+my_google_key+my_google_zoom+my_google_size+'&maptype=hybrid&markers=color:red%7C'+latitude+','+longitude
s = requests.Session()
my_cnt1 = s.get(my_url1).content
my_cnt2 = s.get(my_url2).content
my_img1 = base64.b64encode(my_cnt1).decode('utf-8')
my_img2 = base64.b64encode(my_cnt2).decode('utf-8')
location_map.write(my_img1)
location_sat.write(my_img2)
location_map.close()
location_sat.close()
return [my_img1,my_img2]
# Logo for both dark mode and regular mode
def app_print_logo():
if bool(DARK_MODE):
print ('|image=iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAMTWlDQ1BJQ0MgUHJvZmlsZQAASImVlwdck0cbwO8dmSSsQARkhL1E2QSQEcKKICBTEJWQBBJGjAlBxU0pVbBuEQUXWhVQbLUCUidqnUVxW0dRikqlFqu4UPkuA2rtN37f/X733j/PPffc8zy5e987APRq+TJZAaoPQKG0SJ4YFcaanJ7BInUDEjACdDAOePIFChknISEWwDLc/r28ugEQVXvVTWXrn/3/tRgIRQoBAEgC5GyhQlAI+XsA8FKBTF4EAJEN5bazimQqzoRsJIcOQpapOFfDZSrO1nC1Wic5kQt5DwBkGp8vzwVAtxXKWcWCXGhH9xZkd6lQIgVAjww5WCDmCyFHQx5TWDhDxVAPOGV/Yif3bzazR2zy+bkjrIlFXcjhEoWsgD/n/0zH/y6FBcrhORxgpYnl0YmqmGHebuXPiFExDXKfNDsuHrIh5DcSoVofMkoVK6NTNPqouUDBhTkDTMjuQn54DGRzyJHSgrhYrTw7RxLJgwxXCDpbUsRL1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hPd0U2NPE0HR14MjzsLAwsecDAwO0df9/OXv27BmpVCryGMpn6JIl45AJ8t7e3kv2egp0IYP1icivE+yVMbtavUt/SUxMFIxtyIXoDQFeV1f34Pnz555mhdE5OTkp/wqFsIZMQakMuG+vUbvcYzwBjPoIVXvq1Ck2OzsL1YzJXGQihwbjCui1a9da4L0GT5XntHRF6oSADcOJvHH79u1cAiAO2dbWlsivV1ZWKL8+g+5HQtjKw6xBhAc/JDQ3N/+ErJEtLS0xMKxI6L29vUXOTAm8uRISEiIS/cXFRbEAwq+vr7Opqaks4PvMyRj3WTSIQFevXo3Dih/DE9LDhw+z1dVVRqulvwovLy8xIZhc6CN2pzYl+OQZMgSxyIhzUDrg8WLUt2AhYf5h1SASuXPnjldlZeV9NHNABQxcxUCebGNjQ0yM2BCekzhJ2CHpIfE3QoaNjY2R+HpZWVlha2trDwymGHx/BcyqgVc6oVEEN73p5PmBPGHkbp9+/Fck+xn7yaNsesh0SZjEFacsFvVT/HMla7XaQPyPzcOoxzhVQ3FxcT+DYK1emKY6D74PPPA+PfA3FalzMdf+MhMAAAAASUVORK5CYII=')
print('---')
# --------------------------
# The main function
# --------------------------
# The init function: Called to store your username and access_code in OS X Keychain on first launch
def init():
# Here we do the setup
# Store access_token in OS X keychain on first run
print ('Enter your Cowboy username:')
init_username = input()
print ('Enter your Cowboy password:')
init_password = getpass.getpass()
init_access_token = None
try:
c = cowboy.Cowboy.with_auth(init_username,init_password)
except HTTPError as e:
print ('Error contacting Cowboy servers. Try again later.')
print (e)
time.sleep(0.5)
return
except URLError as e:
print ('Error: Unable to connect. Check your connection settings.')
print (e)
return
except AttributeError as e:
print ('Error: Could not get an access token from Cowboy. Try again later.')
print (e)
return
keyring.set_password("mycowboy-bitbar","username",init_username)
keyring.set_password("mycowboy-bitbar","password",init_password)
init_password = ''
USERNAME = keyring.get_password("mycowboy-bitbar","username")
# --------------------------
# The main function
# --------------------------
def main(argv):
# CASE 1: init was called
if 'init' in argv:
init()
return
# CASE 2: init was not called, keyring not initialized
if bool(DARK_MODE):
color = '#FFFFFE'
info_color = '#C0C0C0'
else:
color = '#00000E'
info_color = '#616161'
if not USERNAME:
# restart in terminal calling init
app_print_logo()
print ('Login to Cowboy | refresh=true terminal=true shell="%s" param1="%s" color=%s' % (cmd_path, 'init', color))
return
# CASE 3a: check for internet connectivity
try:
requests.get('http://www.google.com',timeout=3)
except:
app_print_logo()
print ('No internet connection | refresh=true terminal=false shell="%s" param1="%s" color=%s' % (cmd_path, 'true', color))
return
# CASE 3b: init was not called, keyring initialized, no connection (access code not valid)
try:
True
# create connection to cowboy account
PASSWORD = keyring.get_password("mycowboy-bitbar","password")
bike = cowboy.Cowboy.with_auth(USERNAME,PASSWORD)
bike.refreshData()
except:
app_print_logo()
print ('Login to Cowboy | refresh=true terminal=true shell="%s" param1="%s" color=%s' % (cmd_path, 'init', color))
return
# CASE 4: all ok, specific command for a specific bike received
# if (len(sys.argv) > 1) and not('debug' in argv):
# # v = vehicles[int(sys.argv[1])]
#
#
# if sys.argv[2] == "wake_up":
# v.wake_up()
# else:
# if (len(sys.argv) == 2) and (sys.argv[2] != 'remote_start_drive'):
# True
# # argv is of the form: CMD + vehicleid + command
# # v.command(sys.argv[2])
# else:
# True
# # argv is of the form: CMD + vehicleid + command + key:value pairs
# # v.command(sys.argv[2],dict(map(lambda x: x.split(':'),sys.argv[3:])))
# return
# CASE 5: all ok, all other cases
app_print_logo()
prefix = ''
try:
bike_id = bike.getBike().getId()
bike_nickname = bike.getBike().getNickname()
bike_firmware = bike.getBike().getFirmwareVersion()
bike_position = bike.getBike().getPosition()
bike_charge = bike.getBike().getStateOfCharge()
bike_distance = bike.getBike().getTotalDistance()
bike_duration = bike.getBike().getTotalDuration()
bike_co2 = bike.getBike().getTotalCO2Saved() / 1000
bike_stolen = bike.getBike().isStolen()
bike_model = bike.getBike().getModel()
bike_mac = bike.getBike().getMacAddress()
bike_serial = bike.getBike().getSerial()
except:
return
# add location & battery here
distance_unit='km'
if _LOCATION_TRACKING_:
locationdb.insert({'date':str(datetime.datetime.now()),'bike_position':bike_position,'bike_charge':bike_charge,'bike_distance':bike_distance})
# --------------------------------------------------
# DEBUG MENU
# --------------------------------------------------
if 'debug' in argv:
print ('>>> id:\n%s\n' % bike_id)
print ('>>> nickname:\n%s\n' % bike_nickname)
print ('>>> firmware:\n%s\n' % bike_firmware)
print ('>>> position:\n%s\n' % bike_position)
print ('>>> charge:\n%s\n' % bike_charge)
print ('>>> distance:\n%s\n' % bike_distance)
print ('>>> duration:\n%s\n' % bike_duration)
print ('>>> co2:\n%s\n' % bike_co2)
print ('>>> mac:\n%s\n' % bike_mac)
print ('>>> serial:\n%s\n' % bike_serial)
print ('>>> model:\n%s\n' % bike_model)
print ('>>> stolen:\n%s\n' % bike_stolen)
return
# --------------------------------------------------
# MENU
# --------------------------------------------------
print ('%sBike:\t\t\t\t\t%s | color=%s' % (prefix, bike_nickname, color))
print ('%sBattery:\t\t\t\t\t%s%% | color=%s' % (prefix, bike_charge, color))
print ('%s---' % prefix)
print ('%sOdometer:\t\t\t\t%.2f km | color=%s' % (prefix, bike_distance, info_color))
print ('%sCO2 saved:\t\t\t\t%s kg | color=%s' % (prefix, bike_co2, info_color))
print ('%s---' % prefix)
print ('%sBike ID:\t\t\t\t\t#%s | color=%s' % (prefix, bike_id, info_color))
print ('%sModel:\t\t\t\t\t%s | color=%s' % (prefix, bike_model, info_color))
print ('%sFirmware:\t\t\t\t%s | color=%s' % (prefix, bike_firmware, info_color))
print ('%s---' % prefix)
print ('%sSecurity:\t\t\t\t\t%sNot Stolen%s | color=%s' % (prefix, CGREEN, CEND, info_color))
# --------------------------------------------------
# LOCATION MENU
# --------------------------------------------------
gmaps = googleclient('AIzaSyCtVR6-HQOVMYVGG6vOxWvPxjeggFz39mg')
bike_location_address = gmaps.reverse_geocode((str(bike_position['latitude']),str(bike_position['longitude'])))[0]['formatted_address']
print ('%s--Serial:\t\t\t%s| color=%s' % (prefix, bike_serial, info_color))
print ('%s--Mac address:\t\t%s| color=%s' % (prefix, bike_mac, info_color))
print ('%s-----' % prefix)
print ('%s--Address:\t\t\t%s| color=%s' % (prefix, bike_location_address, color))
print ('%s--Lat:\t\t\t\t%s| color=%s' % (prefix, bike_position['latitude'], info_color))
print ('%s--Lon:\t\t\t\t%s| color=%s' % (prefix, bike_position['longitude'], info_color))
print ('%s---' % prefix)
# --------------------------------------------------
# VEHICLE MAP MENU
# --------------------------------------------------
google_maps = retrieve_google_maps(str(bike_position['latitude']),str(bike_position['longitude']))
vehicle_location_map = google_maps[0]
vehicle_location_sat = google_maps[1]
print ('%s|image=%s href="https://maps.google.com?q=%s,%s" color=%s' % (prefix, vehicle_location_map, bike_position['latitude'],bike_position['longitude'],color))
print ('%s|image=%s alternate=true href="https://maps.google.com?q=%s,%s" color=%s' % (prefix, vehicle_location_sat, bike_position['latitude'],bike_position['longitude'],color))
print ('%s---' % prefix)
if __name__ == '__main__':
main(sys.argv)